modddels icon indicating copy to clipboard operation
modddels copied to clipboard

Directly create a case-modddel with preserved type

Open CodingSoot opened this issue 1 year ago • 3 comments

Freezed supports directly creating a union-case and preserving its type, for example :

@freezed
class MapLayer with _$MapLayer {
  const factory MapLayer.raster(int someParam) = Raster;

  const factory MapLayer.vector(int someParam) = Vector;

  const factory MapLayer.geojson(int someParam) = Geojson;
}

// then, instead of calling `MapLayer.raster(1)` :
final Raster raster = Raster(1);

In modddels, this is not supported (for now). If needed, you can cast the case-modddel directly after creating it, like this :

final raster = MapLayer.raster(
  //...
) as Raster;

Although it's pretty safe, it's not very clean so I'll look into implementing a better alternative.

Originally posted by @CodingSoot in https://github.com/CodingSoot/modddels/discussions/8#discussioncomment-6066086

CodingSoot avatar Jun 01 '23 23:06 CodingSoot