Skip to content

No way to verify that a list argument's entries are valid in a const assert #29276

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
Hixie opened this issue Apr 6, 2017 · 15 comments
Open
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). customer-flutter type-enhancement A request for a change that isn't a bug

Comments

@Hixie
Copy link
Contributor

Hixie commented Apr 6, 2017

abstract class MultiChildRenderObjectWidget extends RenderObjectWidget {
  const MultiChildRenderObjectWidget({ Key key, this.children: const <Widget>[] })
    : assert(children != null),
      assert(!children.any((Widget child) => child == null)), // <-- this line won't compile
      super(key: key);
  // ...
}
@lrhn
Copy link
Member

lrhn commented Apr 6, 2017

Correct. Const expressions cannot iterate over lists or call methods. That is not new, it is a know restriction on compile-time constant expressions, and not something we expect to change.

That does restrict what you can do with compile-time constant asserts, but no more than it restricts what you can do with const expressions in general.

As a rule, you cannot deconstruct composite values (whether lists or other objects) at compile time, you can only build them.

@vsmenon vsmenon added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Apr 6, 2017
@lrhn lrhn added the type-enhancement A request for a change that isn't a bug label Jun 25, 2018
@mathieujobin
Copy link

assert(children.all((Widget child) => child != null)) would be more readable IMO

@tvolkert
Copy link
Contributor

tvolkert commented Apr 9, 2019

Revisiting this. This one assert is too useful to remove, and the fact that it's keeping MultiChildRenderObjectWidget from being const constructible is keeping large swaths of app code from being const constructible (because Row and Column are both descendants of MultiChildRenderObjectWidget).

@dnfield
Copy link
Contributor

dnfield commented Apr 9, 2019

When non-nullability comes in, will we have List<T> where T is non-nullable?

@eernstg
Copy link
Member

eernstg commented Apr 9, 2019

Definitely yes.

@a14n
Copy link
Contributor

a14n commented Apr 15, 2020

Plugged question regarding assert in initializer list: I often wonder why they are part of the const evaluation? If they were executed only at the first initialization we could make almost every widget classes const (especially Row, Column, and Stack) without changes.

@cedvdb
Copy link
Contributor

cedvdb commented Nov 18, 2021

How is that assert valuable ? If the children is a List<Widget> the elements cannot be null (dynamic not included), can they ?

@dnfield
Copy link
Contributor

dnfield commented Nov 19, 2021

It's still valuable as long as we have customers running in unsound null-safety mode, and we still have a substantial number of those.

@gustav3d
Copy link

gustav3d commented Nov 19, 2021 via email

@cedvdb
Copy link
Contributor

cedvdb commented Nov 19, 2021

I did not know they could run on unsound null safety while at the same time receive updates from flutter.. That sounds like a significant maintenance annoyance. Hopefully there are plans to drop that

@mateusfccp
Copy link
Contributor

When will unsound null-safe mode be deprecated?

We shouldn't restrain ourselves for so much time because some people refuses to progress...

@lrhn
Copy link
Member

lrhn commented Apr 6, 2022

Don't restrain yourself. Write perfectly good null safe code, then allow that code to crash and burn if someone passes in a null argument to a non-nullable parameter. Don't bother writing if (arg as dynamic == null) throw NiceError("this thing shouldn't be null"). Just let the dice roll and see where the null crashes things.

They had it coming. They only had themselves to blame.

@dnfield
Copy link
Contributor

dnfield commented Apr 6, 2022

The last I checked on what data we have for this, >20% of Flutter users are using unsound null-safety. As nice as it would be to just migrate, the user experience of getting nonsensical errors at some unexpected place in your program is pretty bad.

@larssn
Copy link

larssn commented Oct 20, 2022

Isn't this one of those things that's purely theoretical? Do you have any data on how often people run into this assertion?

For us, still running unsound, we've never run into it.

It seems like a pretty niche thing, and for it to hold the codebase back for the majority of users is not something I agree with, even though we're not going to benefit from it atm.

So pull the trigger. 🙂

@dnfield
Copy link
Contributor

dnfield commented Oct 21, 2022

Isn't this one of the things that's purely theoretical? Do you have any data on how often people run into this assertion?

For us, still running unsound, we've never run into it.

It seems like a pretty niche thing, and for it to hold the codebase back for the majority of users is not something I agree with, even though we're not going to benefit from it atm.

So pull the trigger. 🙂

We have data that a large number of users are still running code in unsound null-safe mode, where this assertion is helpful.

Dart 3 will be removing this problem before very long. We can just wait for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). customer-flutter type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests