using System;
using System.Console;
using Snippets;
// interface declarationinterface 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 functionpublicoverride 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 functionpublicoverride 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 objectpublic 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/interceptdef 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 constructordef line1 = Line(1.0, 0.344);
// This creates a similar objectdef 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 itdef 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" завершено с ошибкой.
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" завершено с ошибкой.