Define Class - vilinski/nemerle GitHub Wiki

Define Class

  • Category: Defining Types
  • Description: define a class with multiple constructors
  • Code:
using Nemerle;
using System;
using System.Console;

// record macro creates constructor for each parameter
[Record]
class Complex
{
  real : double;
  img  : double;
  
  public this(r : double) { Complex (r, 0.0) }
  public this() { this(0.0, 0.0) }
  
  public Real : double
  {
    get
    {
      real
    }
  }
  
  public Img : double 
  {
    get
    {
      img
  }
  }
  
  public Multiply(other : Complex) : Complex
  {
    Complex(Real * other.Real - Img * other.Img,
            Real * other.Img  + Img * other.Real)
  }
  
  public Add(r : float) : Complex
  {
    Complex(real + r, img)
  }
  
  public Add(r : float, i : float) : Complex
  {
    Complex(real + r, img + i)
  }
  
  public override ToString() : string
  {
    $"$real, $img"
  }
}

    def a = Complex();
    def b = Complex(1.2, 2.5);
    def c = Complex(1.0);
    WriteLine($"a = $a, b = $b c = $c");

  • Execution Result:
a = 0, 0, b = 1,2, 2,5 c = 0, 0

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