Discussion:
[dart-misc] Dart 2 Breaking Change: Argument to assert() must be a bool
'Leaf Petersen' via Dart Misc
2018-03-07 01:55:37 UTC
Permalink
*What is changing?*

In Dart 1, the argument to a call to `assert` could be a boolean, or a
function returning a boolean. In the latter case:

assert(() {
return true;
});


the function argument was invoked by the assert, in order to produce the
value to be asserted.

In Dart 2, the `assert` operation will be defined to expect an operation of
static type assignable to `bool`, and it will be a static error if this is
not the case. Passing a function to an assertion will no longer work.

*What will break?*

Any code that passes an uninvoked function argument to an assertion will no
longer work in Dart 2.

If your code invokes an assertion with an argument whose static type is a
function type, then in Dart 2 you will get a static error indicating that
the function type in question is not assignable to `bool`.

If your code invokes an assertion with an argument whose static type is
assignable to `bool` (e.g. `dynamic` or `Object`), but where the runtime
value of the argument is sometimes or always a function, then in Dart 2 you
will get a runtime cast failure indicating that a function type is not a
subtype of `bool`.

*How do I fix my code?*

If your code invokes an assertion with an argument whose static type is a
function type (and hence your code breaks in Dart 2 with a static type
error), then you can fix your code by immediately invoking the argument to
the assertion.

assert(() {
return true;
}());


If your code invokes an assertion with an argument whose static type is
assignable to `bool` (e.g. `dynamic` or `Object`), but where the runtime
value of the argument is always a function, then you can fix your code by
immediately invoking the argument to the assertion (casting to `dynamic` if
necessary).

assert((e as dynamic)());


If your code invokes an assertion with an argument whose static type is
assignable to `bool` (e.g. `dynamic` or `Object`), but where the runtime
value of the argument is sometimes a function and sometimes a boolean, then
you can fix your code by using an immediately invoked closure to bind the
expression (if necessary) and then testing its runtime type:


assert(((b) => b is bool ? b : b())(e));


*Why is this change being made?*

With this change, we can provide better error messages (when something
which is not a boolean or a function is passed to an assert), and better
type inference (since an assertion context tells the inference engine what
type of object is expected).

For more information see https://github.com/dart-lang/sdk/issues/30326 . I
will update here when this change lands. Please reach out to me here or
offline with any concerns, and/or with help resolving any issues after this
change has landed.

thanks,
-leaf
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Loading...