Code Monkey home page Code Monkey logo

Comments (14)

rrousselGit avatar rrousselGit commented on July 23, 2024 6

Basic support has landed on 0.9.2

Tell me if this is not enough for you 😄

from freezed.

rrousselGit avatar rrousselGit commented on July 23, 2024 2

I don't like it.
I do have some ideas on how to solve this issue already anyway. I just need to work on it.

I'll first finish the deep equality comparison and the copyAs, then I'll work on this.

from freezed.

rrousselGit avatar rrousselGit commented on July 23, 2024 1

Oh, I've just realized what you did.

That's an interesting case. It's likely because Price hasn't been generated yet.

from freezed.

rrousselGit avatar rrousselGit commented on July 23, 2024 1

No problem 😄

I'll leave this open as it should be able to properly fix this since Freezed should be able to infer that the type used is a union.
It's not a priority though, as it could get fairly complex with generics because it involves manually parsing the type.

from freezed.

rrousselGit avatar rrousselGit commented on July 23, 2024 1

Try to avoid using what's on the right side of the = of a constructor.

I'll think about a fix, but in the mean time that's your best bet

from freezed.

rrousselGit avatar rrousselGit commented on July 23, 2024

It doesn't return dynamic, it's simply not there.

Both PriceExample.price and PriceExample.withQuantity define a price parameter, but they have a different type.

As such you have to use when/map/is to determine if it's a .price or .withQuantity, to then be able to access price.

from freezed.

rebaz94 avatar rebaz94 commented on July 23, 2024

also does not work for when or map because generated class return dynamic.
here is generated class

abstract class PriceWithQuantity implements PriceExample {
  const factory PriceWithQuantity(dynamic price, int quantity) =
      _$PriceWithQuantity;

  dynamic get price; /// this should be [Price] class not dynamic
  int get quantity;

  PriceWithQuantity copyWith({dynamic price, int quantity});
}

And if I know I am using PriceWithQuantity class directly it should return instance of Price with quantity property. for example passing instance of PriceWithQuantity

void addQuantity(PriceWithQuantity quantity){
//code
}

from freezed.

rrousselGit avatar rrousselGit commented on July 23, 2024

I'm not too sure how to support that.

It may be more logical to write:

@immutable
abstract class Price with _$Price {
  const factory Price.price(
    int id,
    int price,
  ) = _Price;
}

@immutable
abstract class PriceExample with _$PriceExample {
  const factory PriceExample.withQuantity(
    Price price,
    int quantity,
  ) = PriceWithQuantity;
}

from freezed.

rrousselGit avatar rrousselGit commented on July 23, 2024

Or alternatively, you do:

@immutable
abstract class PriceExample with _$PriceExample {
  const factory PriceExample(int id, int price) = Price;

  const factory PriceExample.withQuantity(
    int id,
    int price,
    int quantity,
  ) = PriceWithQuantity;
}

from freezed.

rebaz94 avatar rebaz94 commented on July 23, 2024

Thanks for your suggestion, its just a simple example more properties it will cause a lot of code duplication, but separating class or casting manually for now it just work.

Again Thanks

from freezed.

maks avatar maks commented on July 23, 2024

Remi I think I have a simliar issue but I'm not sure your previous work around is going to work fo rme. I'm trying to model data structure simliar to std Flutter widgets so I have something like this:

import 'package:flutter/foundation.dart';

part 'widget_type.freezed.dart';

@immutable
abstract class WidgetType with _$WidgetType {
  const factory WidgetType.page({Body body, Header header}) = Page;
  const factory WidgetType.body() = Body;
  const factory WidgetType.header({String title}) = Header;
}

It mostly works but in the generated code the Page class uses dynamic instead of the actual types:

abstract class Page implements WidgetType {
  const factory Page({dynamic body, dynamic header}) = _$Page;

  dynamic get body;
  dynamic get header;

  Page copyWith({dynamic body, dynamic header});
}

I havent had a chance to look at how the code gen is done, but perhaps it would be possible to do a second pass before writing out the generated src to "patch in" the types for classes that take instances of thier own Union type as parameters?

from freezed.

rrousselGit avatar rrousselGit commented on July 23, 2024

I havent had a chance to look at how the code gen is done, but perhaps it would be possible to do a second pass before writing out the generated src to "patch in" the types for classes that take instances of thier own Union type as parameters?

Yes. The problem is that this is pretty difficult to do reliably.

The analyzer doesn't expose the source of the unparsed type, so we have to parse it manually. It can get very messy easily with generics and decorators and co

For now the easiest solution is to extract your Body/Header as independent classes:

@immutable
abstract class Body with _$Body {
  const factory Body() = _Body;
}

@immutable
abstract class Header with _$Header {
  const factory Header() = _Header;
}

@immutable
abstract class WidgetType with _$WidgetType {
  const factory WidgetType.page({Body body, Header header}) = Page;
}

from freezed.

rebaz94 avatar rebaz94 commented on July 23, 2024

@rrousselGit it also return dynamic even using separate class, does I'm doing something wrong?

@immutable
abstract class First with _$First{
  const factory First.a(String data) = A;
  const factory First.b(int data) = B;
}

@immutable
abstract class Second with _$Second{
  const factory Second(A a) = SecondConst;
}

from freezed.

maks avatar maks commented on July 23, 2024

@rrousselGit This doesn't work with the current code gen, but perhaps something like this could be made to work just using the existing analyzer approach?

import 'package:flutter/foundation.dart';

part 'widget_type.freezed.dart';

@immutable
abstract class WidgetType with _$WidgetType {
  const factory WidgetType() = _WidgetType;
  const factory WidgetType.page() = _Page;
}

@immutable
abstract class Page with _$Page implements WidgetType {
  const factory Page({Body body, Header header}) = _Page;
}

so its a bit more work for Freezed users but then we are telling Freezed what the specific subclass for each of the Unions factory constructors.

from freezed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.