-
Notifications
You must be signed in to change notification settings - Fork 89
Basic Records
NN--- edited this page Apr 11, 2012
·
5 revisions
- Category: Defining Types
- Description: Records are concrete type definitions that hold data in an unordered, named structure.
- Code:
using System;
using System.Console;
using Nemerle;
[Record]
class Point
{
public x : double;
public y : double;
public override ToString() : string { $"{ x = $x, y = $y }" }
}
[Record]
class Triangle
{
public p1 : Point;
public p2 : Point;
public p3 : Point;
public override ToString() : string { $"p1 = $p1, p2 = $p2, p3 = $p3" }
}
[Record]
class Vector
{
public dx : double;
public dy : double;
public override ToString() : string { $"{ dx = $dx, dy = $dy }" }
}
def origin = Point(0.0, 0.0);
def onex = Point(1.0, 0.0);
def oney = Point(0.0, 1.0);
def diff(p1, p2) { Vector(p2.x - p1.x, p2.y - p1.y) }
def sides(tri) { (diff(tri.p2, tri.p1), diff(tri.p3, tri.p2), diff(tri.p1, tri.p3)) }
def triangle1 = Triangle(origin, onex, oney);
WriteLine($"triangle1 = $triangle1");
WriteLine($"sides(triangle1) = $(sides(triangle1))")
- Execution Result:
triangle1 = p1 = { x = 0, y = 0 }, p2 = { x = 1, y = 0 }, p3 = { x = 0, y = 1 }
sides(triangle1) = ({ dx = -1, dy = 0 }, { dx = 1, dy = -1 }, { dx = 0, dy = 1 })
[Copyright ©](Terms of use, legal notice)