Skip to content

This example works on Nemerle snippets version, but not worked on this #1

Open

Description

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 
      {  
        // implement ILine interface and bind Get method to point function
        ILine : { Get = point }
      }
    }

    // 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");
  }
}

I try rewrite example with new version in repo, open new namespace, compiler, but have error:

------ Построение начато: проект: ConsoleApplication1, Конфигурация: Debug Any CPU ------
E:\OpenSource\Nemerle\samples\ConsoleApplication1\ConsoleApplication1\Main.n(109,7): error : '_N_ObjectExpression_6145' does not implement interface member 'ILine.Get : double -> IPoint'
E:\OpenSource\Nemerle\samples\ConsoleApplication1\ConsoleApplication1\Main.n(105,18): error : in argument #1, needed a double, got object: System.Object is not a subtype of System.Double [simple require]
E:\OpenSource\Nemerle\samples\ConsoleApplication1\ConsoleApplication1\Main.n(127,18): error : there is no member named Get' in object with type ? E:\OpenSource\Nemerle\samples\ConsoleApplication1\ConsoleApplication1\Main.n(127,18): error : unbound nameGet' in `line3'
E:\OpenSource\Nemerle\samples\ConsoleApplication1\ConsoleApplication1\Main.n(109,7): error : expected IPoint, got double -> Point in function body: double -> Point is not a subtype of IPoint [simple require]
Построение проекта "ConsoleApplication1.nproj" завершено с ошибкой.

Activity

  1. luajalla commented on Nov 28, 2011

    @luajalla
    Owner

    Yep, the current implementation is not complete yet - that's just the first version.
    Your case is one of those things, because point is considered as method body:

    | <[ $(n: name) = $body ]> // todo: get lambdas back
    | <[ $(n: name)() = $body ]> => MethodInfo(n, body, null, null)
    

    The idea of this version of macro is that we don't need to declare functions - we can write a method body, use calls to base and that sort of thing. So as a temporary workaround you need something like

    ILine: { Get(x: double) = point(x) }  
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions