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

[Feature request]safe cast #93

Open
shinriyo opened this issue Jul 1, 2020 · 3 comments
Open

[Feature request]safe cast #93

shinriyo opened this issue Jul 1, 2020 · 3 comments

Comments

@shinriyo
Copy link

shinriyo commented Jul 1, 2020

I want to cast easyly.

because it is too long.
List<int> hoge = map['hoge']?.cast<int>() ?? [];

I want to do this.

List<int> hoge = map['hoge'].safecast<int>();

@simc
Copy link
Owner

simc commented Jul 4, 2020

I'm not sure whether this is something many users would find useful. Is there a language which has a similar method?

There are many examples where we could add a "safe" method but I don't think cluttering the auto complete is worth it.

Example:

5.safeAdd(7)       // -> 13
null.safeAdd(7)    // -> 7

@passsy
Copy link
Collaborator

passsy commented Jul 9, 2020

I've constantly ran into similar problems when parsing json responses, that's why I created the deep_pick package.

Your code would become:

List<int>? hoge = pick(map, 'hoge').asListOrNull();
List<int> hoge = pick(map, 'hoge').asListOrEmpty();

@shinriyo
Copy link
Author

shinriyo commented Jul 10, 2020

@passsy Thank you. I will be considering use it

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

3 participants