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

pls add syntactic sugars of List and Map #555

Open
stategen opened this issue Aug 31, 2019 · 7 comments
Open

pls add syntactic sugars of List and Map #555

stategen opened this issue Aug 31, 2019 · 7 comments

Comments

@stategen
Copy link

  1. add property or getter of selected to List;

  2. list?.asMap()?.keys?.map(...) =>list?.asMap().keys.map(...);
    when ?. get null , cade after should stop ;

  3. {...map} , map should can be null like javascript?

@leafpetersen
Copy link
Member

  1. dd property or getter of selected to List;

Not sure what is meant by this, can you elaborate?

2. list?.asMap()?.keys?.map(...) =>list?.asMap().keys.map(...);
when ?. get null , cade after should stop ;

If I understand this request correctly, this is planned for the NNBD release.

3. {...map} , map should can be null like javascript?

You can currently write {...?map} which evaluates to {} if map is null. Is that what you're looking for?

@stategen
Copy link
Author

2 & 3, yes!thanks!,
for 1:
List sometimes for render, the item's property of selected of checked is importent. and will save more code.
var list =['aaa','bbb','ccc','ddd'];
list.select(1);
list.select(2);
list.asMap().keys.map((index)=>{
if (list.selected(index)){
...
} else {
...
}
});

@stategen
Copy link
Author

can you add these
var list =beans?.list;
4. list??(list.add(1)); //<=== if (list!=null) {list.add(1)};
5.list!!=null?list.add(1) ; //<=== list!!=null?list.add(1):null;

@lrhn
Copy link
Member

lrhn commented Sep 2, 2019

Dart Lists does not have any notion of elements being "selected". They are plain arrays of values. Selection is a UI concept that is not relevant to most uses of lists in programs. For something like that, you probably want some custom list selection model class which matches your use-case.

Ad 4:

  list?.add(1)

will add 1 to the list if the list is not null.

Not sure what 5. mean. The type of list.add(1) is void, so it's not a good choice for an expression's value.(I also don't recognize the !!= operator).
Maybe list?..add(1) will do what you want. This syntax will be added with the NNBD update as a null-aware cascade. It will evaluate to the value of list, and add 1 if that value is not null.

@stategen
Copy link
Author

stategen commented Sep 5, 2019

  1. list??(myList.addAll(list)); //<=== if (list!=null) {myList.addAll(list)};
    5.list!=null?myList.addAll(list) ; //<=== list!=null?myList.addAll(list):null;

@lrhn
Copy link
Member

lrhn commented Sep 5, 2019

There is currently no easy way to take an action if an argument value is not null. We have ?. to take an action if the receiver is not null, but nothing shorter than:

if (list != null) myList.addAll(list);

For addAll, it's possible because it has a neutral element: myList.addAll(list ?? const []).
This performs the call in all cases, but we know that it will do nothing with const [] as argument.
If you really need to not call the function at all, then you still need a conditional to guard it.

The syntax list ?? myList.addAll(list) is already valid and means the opposite of what you want—it calls the function only if list is null.

The unmatched ? (with no :) is interesting, but hard to parse, and will likely be problematic with non-nullable types where you can't just use null as a default value.

@stategen
Copy link
Author

stategen commented Sep 5, 2019

thanks!

  1. list??(myList.addAll(list)); //<=== if (list!=null) {myList.addAll(list)};
    beacuse of format rules, word if must with {} even there is 1 line(this condition is common) ,so I can not write:
    if (list != null) myList.addAll(list);
    but :
    if (list != null) {
    myList.addAll(list);
    } //three lines

  2. must with : is ugly, even do nothing after, javascript ,java,typescript...,dart is new lang

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