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

Record typdefs? #29

Open
aloisdeniel opened this issue Aug 28, 2023 · 6 comments
Open

Record typdefs? #29

aloisdeniel opened this issue Aug 28, 2023 · 6 comments

Comments

@aloisdeniel
Copy link

Hello!

Thanks for this useful package!

Would it be possible to generate reflectors for record typedefs like the following one?

typedef User = ({
    String name,
    int age,
});

@ReflectionBridge([
  User,
])
class UserReflectionBridge {}

Thanks

@aloisdeniel aloisdeniel changed the title Does it work with Record typdefs? Record typdefs? Aug 28, 2023
@gmpassos
Copy link
Owner

There's no reflection for 'typedef' yet, and also I haven't tested it with records yet.

A full support for records is already planned, but I haven't realized the use with typedef. If you can provide a full use case, with complex examples, it can help the planning.

Best regards.

@aloisdeniel
Copy link
Author

aloisdeniel commented Sep 1, 2023

Sure,

I basically use typedefs with records to define simple data classes.

typedef User = ({
    String name,
    int age,
});

// You can add extension methods or properties
extension UserExtensions on User {
   String get description {
      return '$name $age';
   }
   
   bool isOlder(User other) {
      return age > other.age;
   }
}

void main() {
    final User user1 = (name: 'John', age: 34);
    final User user2= (name: 'Bob', age: 32);
    final isUser1Older = user1.isOlder(user2);
    print('${user1.description} ${user2.description} : $isUser1Older');
}

So the typedef is just a "shortcut" for the record type. It just makes it you avoid repeating the full record type each time. But I actually use them as I would use real types.

That said, If the reflection bridge works for the record type, it should work for the typedef of this same record type.

@gmpassos
Copy link
Owner

gmpassos commented Sep 1, 2023

Nice,

Please add this approach to this thread, since no one mentioned it yet:

dart-lang/language#314

Now there's a proposal for data classes:

https://github.com/dart-lang/language/blob/main/working/value-classes/feature-specification.md

@gmpassos
Copy link
Owner

gmpassos commented Sep 1, 2023

Could you demonstrate how you intend to utilize reflection_factory with your 'data' classes?

Just getting the list of fields? Since there's no reflection for extensions yet.

@aloisdeniel
Copy link
Author

I would use it only for record properties, not for extension methods!

It is mainly to display the tree of data in the application.

@gmpassos
Copy link
Owner

gmpassos commented Sep 4, 2023

Speaking about a tree of data: maybe this can be helpful to generate the tree for console:

https://pub.dev/packages/ascii_art_tree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants