Language: Will Object.toString() return a non-null String?

Created on 11 Mar 2019  路  4Comments  路  Source: dart-lang/language

Once we have NNBD, will the type signature of Object.toString() to be changed to enforce that all implementations must return a non-null String?

Or is it considered a desired feature that this method can indeed return null?

nnbd question

Most helpful comment

Yes. The return type of Object.toString will almost certainly be String (non-nullable).
(I can't imagine any issue that would change that, but then, you never know).

All 4 comments

I would think it should be non-null, if only because of the behavior of string interpolation:

class Foo {
  toString() => null;
}

void main() {  print('${Foo(}'); }

Uncaught exception:
Invalid argument: Instance of 'Foo'

Ahh, I see you've referenced that in a similar issue

Yes. The return type of Object.toString will almost certainly be String (non-nullable).
(I can't imagine any issue that would change that, but then, you never know).

Great, thanks for clarifying!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wytesk133 picture wytesk133  路  4Comments

moneer-muntazah picture moneer-muntazah  路  3Comments

architkithania picture architkithania  路  5Comments

har79 picture har79  路  5Comments

dev-aentgs picture dev-aentgs  路  3Comments