Skip to content

Define a class with mutable variable

CodingUnit edited this page Dec 1, 2011 · 11 revisions

Define a class with mutable variable

  • Category: Defining Types
  • Description: Define a class with mutable variable
  • Code:
using System;
using System.Console;
using System.Diagnostics;
using Nemerle.Assertions;

variant BuilderState
{
  | None
  | Active
  | Completed
}

// open type for access its internal structure
using BuilderState;

class Builder
{
  mutable state : BuilderState = None();

  public CurrentState : BuilderState { get { state } }

  public Begin() : void
  {
    state = Active();
  }

  public End() : void
  {
    // complete the build process...
    state = Completed();

  }
}

    def print_state(b) {WriteLine($"current state = $(b.CurrentState)")}

    def b = Builder();
    print_state(b);
    b.Begin();
    print_state(b);
    b.End();
    print_state(b);
  • Execution Result:
current state = BuilderState+None
current state = BuilderState+Active
current state = BuilderState+Completed
Clone this wiki locally