Language: Enable 'as' and 'is' syntax for types available only at runtime

Created on 29 Apr 2020  路  1Comment  路  Source: dart-lang/language

There may be some obscure reason that this is not a good idea or even possible but it would be nice to be able to do the following:

var string = 'hello';
var int = 1;
print(int is string.runtimeType);

Or be able to save a type in a variable and use it later:

var string = 'hello';
var int = 1;
var aType = string.runtimeType;
print(int is aType);

This produces a pretty silly error:
Error: 'aType' isn't a type.

Is there a difference between types and the type class?

feature

Most helpful comment

This has been considered before in various forms (e.g. as an extension to Type API https://github.com/dart-lang/sdk/issues/30717 or as permitting expressions on the right hand side of is).

In general the only argument against this is based on the estimated code size impact, because compiler looses a good way to estimate which type checks can occur in the program - and thus can no longer throw away some of the metadata associated with the class hierarchy. @rakudrama has done experiment for dart2js to estimate approximate impact and saw some non negligible costs in terms of the code size.

>All comments

This has been considered before in various forms (e.g. as an extension to Type API https://github.com/dart-lang/sdk/issues/30717 or as permitting expressions on the right hand side of is).

In general the only argument against this is based on the estimated code size impact, because compiler looses a good way to estimate which type checks can occur in the program - and thus can no longer throw away some of the metadata associated with the class hierarchy. @rakudrama has done experiment for dart2js to estimate approximate impact and saw some non negligible costs in terms of the code size.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

moneer-muntazah picture moneer-muntazah  路  3Comments

jonasfj picture jonasfj  路  3Comments

munificent picture munificent  路  5Comments

wytesk133 picture wytesk133  路  4Comments

panthe picture panthe  路  4Comments