language icon indicating copy to clipboard operation
language copied to clipboard

More concise syntax for defining sealed families

Open mraleph opened this issue 2 years ago • 12 comments

With sealed class families and pattern matching developers are understandably excited to use them for data modelling in their programs. However the syntax is overly verbose for simple families, e.g. modelling success / failure in the API requires writing something like this:

sealed class Result<T> {
  const Result();
}

class Ok<T> extends Result<T> {
   final T value;
   const Ok<T>(this.value);
}

class Error extends Result<Never> {
  final String message;
  const Error(this.message);
} 

With primary constructors this is reduced to

sealed class Result<T> {
  const Result();
}

class Ok<T>(final T value) extends Result<T>;
class Error(final String message) extends Result<Never>;

Which is much better but still has some repeated boilerplate here: repeated extends Result<*>.

I suggest expanding upon primary constructors and introduce a special syntactic sugar for simple sealed families like this

sealed class Result<T> {
  case Ok<T>(final T value);
  case Error(final String message);
} 

Here case C<T1, ..., Tn>(v0, ..., vN) { m1 ... mN }? occurring inside sealed class B<U1, ..., Un> gives raise to

class C<T1, ..., Tn>(v0, ..., vN) extends B<Forward({Ti}, {Ui})> {
  // Optional methods
  m1
  // ...
  mN
}

Forward(...) is defined as forwarding type parameters based on their names (or maybe indices - I am not sure what is better). If Ui does not have a corresponding Ti then Never is passed instead.

/cc @munificent @lrhn

mraleph avatar Apr 27 '23 13:04 mraleph

Why the "case" keyword instead of "class"?

rrousselGit avatar Apr 27 '23 14:04 rrousselGit

@rrousselGit it makes a clearer connection to the surrounding class. class within a class does not convey the same meaning. case makes it clearer.

mraleph avatar Apr 27 '23 14:04 mraleph

Talk about timing: @eernstg has just uploaded primary-constructors proposal (which I knew was cooking, but could not find anywhere to refer to).

I have now reworded original proposal to simply delegate to primary-constructors syntax.

mraleph avatar Apr 27 '23 14:04 mraleph

Like switch removed case:

switch(aInt) {
 1: print('1'),
 _: print('not 1'),
}

Couldn't we just omit case like:

sealed class Result<T> {
  Ok<T>(final T value);
  Error(final String message);
} 

or that collides with current grammar? I think it's fine to assume that a class declaration inside a sealed class is a case.

rubenferreira97 avatar Apr 27 '23 15:04 rubenferreira97

@rubenferreira97 the syntax you propose is valid today and defines two abstract instance methods.

mraleph avatar Apr 27 '23 15:04 mraleph

@rrousselGit it makes a clearer connection to the surrounding class. class within a class does not convey the same meaning. case makes it clearer.

Would we be able to use all the class syntaxes though? Like mixins, abstract/final/sealed/internal, implements/extends, ...?

If so, I'd prefer using "class". I think the nesting would be enough to communicate the relationship.

rrousselGit avatar Apr 27 '23 16:04 rrousselGit

Just to add that in the future possibility of Dart adding data classes, the syntax case data would be strange. Kinda the same problem with modifiers that @rrousselGit described.

sealed class Result<T> {
  data class Ok<T>(final T value);
  data class Error(final String message);
} 

rubenferreira97 avatar Apr 27 '23 16:04 rubenferreira97

Also what about two-level nesting?

Like:

sealed class A {
  sealed class B {
   class C {}
   class C2 {}
  }
  class B2{}
}

rrousselGit avatar Apr 27 '23 16:04 rrousselGit

I'm definitely interested in some better syntax in this area, yes. The main challenges that I know of are:

  • Users have also requested static nested classes (#336). I think that's also a useful feature, so I'd like to make sure whatever syntax we come up with doesn't collide with that.

  • As @rrousselGit suggests, it would be nice if this extended to support entire sealed hierarchies and not just a single level deep.

  • The subtypes of a sealed supertype might implement or extend it and it would be nice (but not strictly essential) to support being able to express both of those.

I think those are likely tractable, but it will take some work to sort out.

munificent avatar May 01 '23 21:05 munificent

Using the case syntax, could classes have normal bodies, with fields, methods, etc?

Albert221 avatar May 21 '25 14:05 Albert221

when could we see this in dart

atototenten avatar May 24 '25 11:05 atototenten

No current plans. I think we are interested in the feature, but there are higher priority issues we are working on.

munificent avatar May 29 '25 20:05 munificent