Skip to content

Directly create a case-modddel with preserved typeΒ #9

@CodingSoot

Description

@CodingSoot

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 #8 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions