Generic Type - vilinski/nemerle GitHub Wiki

Generic Type

  • Category: Defining Types and Functions

  • Description: Define a generic type

  • Code:

 using System; 
 using System.Console; 
 using Snippets; 
 using Nemerle.Utility;

[Record] 
class TrackedValue 
{

    mutable value : Kind; // define private mutable value

    [Accessor] // accessor macro attribute, automatically creates Reads public property for read this field
    [RecordIgnore] 
    mutable reads : int = 0; // define private mutable read counter
    [Accessor] 
	[RecordIgnore] 
	mutable writes : int = 0;

    public Value : Kind
    {
		get
		{
			reads++; 
			value
		}
		set
		{
			writes++; // increment counter on each write
			this.value = value
		}
    }
         
    public override ToString() : string
    {
		Value.ToString()
    }

}

module GenericSample1 
{

    Main() : void
    {
        def a = TrackedValue("Hello");
        def b = TrackedValue(20);
        def c = TrackedValue(30.0);
        WriteLine($"$a $b $c")
    }

} 
  • Execution Result:
 Hello 20 30 

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