1

I'm coming from C#/F#/Haskell so I'm trying to come up with solutions to programming problems I'm used to solving with types.

  1. class A where T : new() in C#, this is mainly so I can do new T() somewhere. This creates a malformed type error in Dart. Is there a reasonably idiomatic way to solve this? Perhaps with factories?

  2. I did some experiments with mixins, in the case of name conflicts for inherited mixin members, the last mixin wins. So for the following: abstract class mixA { void foo(); } abstract class mixB { void foo(); } class C extends Object with mixA, mixB {} new C().foo();

this would end up calling mixB.foo() whereas class C extends Object with mixB, mixA {} would end up calling mixA.foo() Is there anyway to access the members of the hidden mixins?

Suppose I mix 2 mixins with a field of the same name. Does the subclass instance have 2 fields at runtime (just 1 is inaccessible) or is the object like a dictionary and there is only 1 slot for each name?

jz87
  • 9,199
  • 10
  • 37
  • 42

1 Answers1

5

1 is not possible. You can't call new on a generic type (or variable for that matter). The most common workaround is to create a closure that allocates the object instead.

The answers for 2 fall out of the fact that Mixins can be seen as super-classes: A extends Object with B, C is basically equivalent to:

class B' extends Object { <copy over code inside B> }
class C' extends B' { <copy over code inside C> }
class D extends C' { ... }

With this in mind:

  • no. there is no way to access hidden super elements.
  • yes. you would end up with multiple fields.

Small note: the <copy over code inside X> part is not completely correct since that would change the library-scope. Code from mixin B is conceptually in the library the mixin is declared in.

Florian Loitsch
  • 7,698
  • 25
  • 30