Skip to content

Feature: firstOrElse variant in List #37372

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

Open
Antoniossss opened this issue Jun 26, 2019 · 3 comments
Open

Feature: firstOrElse variant in List #37372

Antoniossss opened this issue Jun 26, 2019 · 3 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug

Comments

@Antoniossss
Copy link

Antoniossss commented Jun 26, 2019

It would be convinient to have variation of first property as method that would allow something to get first element, or produce default value insteaf of throwing wrong state exception..

Right now I am doing something like this (to avoid extra var)

var val=myList.where((i)=>i.x>5).map((i)=>i.y).firstWhere((v)=>true), orElse:()=>someDefaultValue

while I would like to terminate like this:
var val=myList.where((i)=>i.x>5).map((i)=>i.y).firstOrElse(()=>defaultValue);

@lrhn
Copy link
Member

lrhn commented Jun 26, 2019

It is unlikely that we'll change the List or Iterable APIs at the current time, there are far too many implementations that would then be broken.
This functionality seems like something that can easily be implemented using extension methods, though, and anyone can add that extension.

For now, you can do:

bool kTrue(_) => true;
var val = myList.where((i) =>i.x > 5).map((i) => i.y).firstWhere(kTrue, orElse: () => defaultValue) ;

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug labels Jun 26, 2019
@Antoniossss
Copy link
Author

I have no problem in extending API by myown for own purpose. Actually that is more than fine for me. Can you double check your link for extension methods? It seems to be dead .

@lrhn
Copy link
Member

lrhn commented Jun 27, 2019

Link fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants