nemerle icon indicating copy to clipboard operation
nemerle copied to clipboard

Suggestion: Variants should produce Properties instead of Fields

Open ssrmm opened this issue 8 years ago • 0 comments

In the current implementation properties of variant options are translated to fields (see below example). I think it would be favorable to use .NET-Properties instead because:

  • Not using public fields is a best practice and properties are what should be used instead. Apparently some reasons for this (e.g. lazy evaluation or input checks) don't apply here: We have read-only data that's already there when the object is created. But I think the point still stays valid.
  • It's more likely that reflection-based code will require properties instead of fields. The concrete example that I see here would be Microsofts PropertyGrid, which is used for example in the Visualizer for Nitra.
  • The Performance overhead of using properties is neglectable.

Implementing this as the default behaviour would be a backward compatibility break if someone is using reflection on variants. Therefore I'm not quiet sure if it is reasonable to do that. A macro could be used instead to make the switch from fields to properties on a per variant basis.


namespace Test
{
  public variant Test
  {
    | Foo { Integer : int;   }
    | Bar { Float   : float; }
  }
}

Removing the boilerplate code this roughly translates to

namespace Test
{
  public abstract class Test
  {
    public class Foo : Test.Test
    {
      public Integer : int;

      public this(integer : int)
      {
        this.Integer = integer;
      }
    }

    public class Bar : Test.Test
    {
      public Float : float;

      public this(float : float)
      {
        this.Float = float;
      }
    }
  }
}

ssrmm avatar Nov 29 '16 10:11 ssrmm