Using interfaces - vilinski/nemerle GitHub Wiki

Using interfaces

  • Category: Defining Types and Functions

  • Description: A longer sample showing how to use interfaces to model ’data’ objects such as abstract points. Somewhat contrived, since multiple representations of points are unlikely practice, but for larger computational objects maintaining flexibility of representation through using interfaces or function values is often crucial.

  • Comment: For use this example you must include NewObjectMacro.dll reference (from Nemerle program directory)

  • Code:

 using System; 
 using System.Console; 
 using Snippets;

// interface declaration 
interface IPoint 
{

  X : double {get;}
  Y : double {get;}

}

/// Implementing an interface with a class 
// use record macro to automatic create initialize constructor 
[Record] 
class Point : IPoint 
{
  public X : double {get;}
  public Y : double {get;}

  // overrides print to string function
  public override ToString() : string { $"X:$X, Y:$Y" }
}

/// Implementing an interface with a class 
// use record macro to automatic create initialize constructor 
[Record] 
class MutablePoint : IPoint 
{
  public X : double {get;set;}
  public Y : double {get;set;}

  // overrides print to string function
  public override ToString() : string { $"X:$X, Y:$Y" }
}

/// This interface implementing one function. It represents 
/// a function from some variable to (X,Y) 
interface ILine 
{
  Get(_ : double) : IPoint;
}

/// Implementing an interface with a class 
/// 
/// Here a line is specified by gradient/intercept 
/// use record macro to automatic create initialize constructor 
[Record] 
class Line : ILine 
{

  public A : double;
  public B : double;

  public Get(x : double) : IPoint
  {
    def interpol(x) { A * x + B }

    Point(x, interpol(x))
  }

}

/// Implementing an interface with a class. 
/// 
/// Here a line is specified by gradient/intercept 
/// use record macro to automatic create initialize constructor 
[Record] 
class GradientInterceptLine : ILine 
{

  // Publish additional properties of the object
  public Gradient : double;
  public Intercept : double;

  public Get(x : double) : IPoint
  {
    def interpol(x) { Gradient * x + Intercept }
    Point(x, interpol(x))
  }

}

module InterfaceSample3 
{

  Main() : void
  {

  // Implementing an interface with an object expression. 
  // Here a line is specified by gradient/intercept
    def ObjectLine(a, c)
    {
      def y(x) { a * x + c } 
      def point(x) { Point(x, y(x)) }

      // create object using newobj macro
      newobj 
      {  
        ILine : { Get = point } // implement ILine interface and bind Get method to point function
      }
    }
     
    // This creates an object using constructor
    def line1 = Line(1.0, 0.344);

    // This creates a similar object
    def line2 = GradientInterceptLine(2.0, 1.5);
    def line3 = ObjectLine(0.7, 0.564);
    def origin =  Point(0.0, 0.0);
    def origin2 =  MutablePoint(0.5, 1.0); // create mutable point
    origin2.X += 0.3; // move x coordinate of it
    def point1 = line1.Get(-1.0);
    def point2 = line2.Get(0.0);
    def point3 = line3.Get(1.0);

    WriteLine($"origin = $origin");
    WriteLine($"origin2 = $origin2");
    WriteLine($"point1 = $point1");
    WriteLine($"point2 = $point2");
    WriteLine($"point3 = $point3");
  }

}
  • Execution Result:
 origin = X:0, Y:0 
 origin2 = X:0,8, Y:1 
 point1 = X:-1, Y:-0,656 
 point2 = X:0, Y:1,5 
 point3 = X:1, Y:1,264 

[Copyright ©](Terms of use, legal notice)