Structs - vilinski/nemerle GitHub Wiki

Structs

  • Category: Defining Types
  • Description: Structs are lightweight data structures, stored on the Stack. They use value equality comparison.
  • Code:
using System;
using System.Console;
using Nemerle.Extensions;

[Record]
struct StructBox
{
  public Width : double;
  public Height : double;
  public Depth : double;
}

[Record]
[StructuralEquality]
struct StructVector2D
{
  public DX : double;
  public DY : double;

  public Length : double { get { Math.Sqrt(DX * DX + DY * DY) } }
}

def structSample()
{    
    def b1 = StructBox(2.0, 3.0, 6.0);
    def v1 = StructVector2D(3.0, 4.0);   
    def v2 = StructVector2D(3.0, 4.0);

    WriteLine($"b1.Width = $(b1.Width)"); 
    WriteLine($"v1.Length = $(v1.Length)");
    WriteLine($"v1 = v2?\t$(v1 == v2)");
} 

structSample()
  • Execution Result:
b1.Width = 2
v1.Length = 5
v1 = v2?	True

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