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

A syntax for type signatures with mixins #1481

Closed
makoConstruct opened this issue Feb 28, 2021 · 3 comments
Closed

A syntax for type signatures with mixins #1481

makoConstruct opened this issue Feb 28, 2021 · 3 comments
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

Comments

@makoConstruct
Copy link

It is sometimes desirable to require a parameter to implement a class and a mixin, for instance:

mixin Indented {
   int get indentLevel;
}

class IndentedLine{
   Widget with Indented child;

But the expression Widget with Indented is not legal. There is no way to refer to that type.

I have tried

abstract class IndentedWidget extends Widget with Indented;

class IndentedLine{
   IndentedWidget child;

But this wont do here. When you actually create an IndentedWidget, you generally can't do it by extending IndentedWidget. You do it by extending StatelessWidget with Indented, which despite implementing both Widget and Indented, is not castable to IndentedWidget.

@makoConstruct makoConstruct added the feature Proposed language feature that solves one or more problems label Feb 28, 2021
@eernstg eernstg added the state-duplicate This issue or pull request already exists label Mar 1, 2021
@eernstg
Copy link
Member

eernstg commented Mar 1, 2021

You are asking for intersection types: If you could use a term like Widget with Indented as a type then it would have to mean "a maximal type T such that T is a subtype of Widget and T is a subtype of Indented". The word "maximal" just means that we can't choose a different value for T which is a supertype of the chosen one, so it gets "as close as possible to Widget and Indented". That's exactly how we'd define an intersection type. A typical syntax would use &, so you would then write this intersection type as Widget & Indented:

class IndentedLine {
   Widget & Indented child;
}

Intersection types could be added to Dart, but it is likely to be a major project. In particular, intersection types are directly related to union types (for instance, using | to specify union types, Function(S | T) <: Function(S) & Function(T) and Function(S) | Function(T) <: Function(S & T)), and this means that the addition of intersection types basically won't work unless we also add union types, and vice versa.

See #83 and #145 for further discussions about intersection and union types. Also note #1152 where the topic of intersection types is approached from a similar angle as in this issue.

I'll close this issue as a duplicate of those other discussions.

However, in the particular case where you wish to specify a subtype of Widget and Indented, it might be possible to simply eliminate the problem by making abstract class Indented ... implements Widget. The assumption is that you aren't actually going to work with any Indented objects that aren't also Widgets, and if this is true then you might as well just include Widget among the supertypes of Indented in the first place, and then you can simply do this:

class IndentedLine {
  Indented child;
}

@eernstg eernstg closed this as completed Mar 1, 2021
@makoConstruct makoConstruct changed the title A syntax for type declarations with mixins A syntax for type signatures with mixins Mar 1, 2021
@makoConstruct
Copy link
Author

makoConstruct commented Mar 1, 2021

Oh cool, I didn't know about implements, so many reasonable subtleties.

I ended up using runtime type checking (and if anything that doesn't implement Indent is passed in, it just defaults to indent:0), but it's nice to know that there's a way to do that.

@eernstg
Copy link
Member

eernstg commented Mar 1, 2021

Ah, makes sense! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants