-
Notifications
You must be signed in to change notification settings - Fork 89
Records with Members
NN--- edited this page Apr 18, 2012
·
4 revisions
- Category: Defining Types
- Description: Nemerle allows values to be associated with type names, i.e. the type name can be used as a container for 'members' of that type. These are treated the same way as .NET object model members.
- Code:
using System;
using System.Console;
using Nemerle;
[Record]
class PointWithMembers
{
public x: double;
public y: double;
public static Origin : PointWithMembers { get { PointWithMembers(0.0, 0.0) } }
public static @+(p : PointWithMembers, v : VectorWithMembers) : PointWithMembers
{
PointWithMembers(p.x + v.dx, p.y + v.dy)
}
public override ToString() : string { $"{ x = $x, y = $y }" }
}
[Record]
class VectorWithMembers
{
public dx : double;
public dy : double;
public static Zero : VectorWithMembers { get { VectorWithMembers(0.0, 0.0) } }
public static OneX : VectorWithMembers { get { VectorWithMembers(1.0, 0.0) } }
public static OneY : VectorWithMembers { get { VectorWithMembers(0.0, 1.0) } }
public static @+(v1 : VectorWithMembers, v2 : VectorWithMembers) : VectorWithMembers
{
VectorWithMembers(dx = v1.dx + v2.dx, dy = v1.dy + v2.dy)
}
public override ToString() : string { $"{ dx = $dx, dy = $dy }" }
}
module Test
{
Main() : void
{
WriteLine($"Point.Origin = $(PointWithMembers.Origin)");
WriteLine($"Point.Origin + Vector.OneX = $(PointWithMembers.Origin + VectorWithMembers.OneX)");
WriteLine($"Vector.OneX + Vector.OneY = $(VectorWithMembers.OneX + VectorWithMembers.OneY)");
}
}
- Execution Result:
Point.Origin = { x = 0, y = 0 }
Point.Origin + Vector.OneX = { x = 1, y = 0 }
Vector.OneX + Vector.OneY = { dx = 1, dy = 1 }
[Copyright ©](Terms of use, legal notice)