Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Variants should produce Properties instead of Fields #13326

Open
ssrmm opened this issue Nov 29, 2016 · 0 comments
Open

Suggestion: Variants should produce Properties instead of Fields #13326

ssrmm opened this issue Nov 29, 2016 · 0 comments

Comments

@ssrmm
Copy link

ssrmm commented Nov 29, 2016

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 ssrmm changed the title Suggestion: Variants should produce Properties instead of Attributes Suggestion: Variants should produce Properties instead of Fields Nov 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant