Dart has an is
operator to check the type of a variable. This operator can also be used with negation is!
. In this case
we check that the type of a variable on the left side of the operator is not the one mentioned on the right side.
Similar operators also exist in other languages, but they might have different spelling. For example, in Kotlin, the same operator is written this
way: !is
. Developers familiar with both languages might confuse these 2 operators. Unfortunately, this won’t lead to a compile time
error, but will change the semantic of your code.
In Dart, x is! Y
, will return true
if x
is not of type Y
and will return false
, if
x
is of type Y
. On the other hand x !is Y
will apply null assertion operator (!
) to x
and then return true
if x
is of type Y
, and return false
if it is not.
What is the potential impact?
It’s hard to estimate the impact taking into account that, firstly x!
can throw an exception and then, the resulted check will have an
absolutely opposite meaning.