if (x is String)
if (x is List) // true for all lists
if (x is List<int>) // true for lists created like new List<int>, <int>[], etc.
// doesn't guarantee that all entires are ints.
I'm taking in an object by JSON like so:var data = JSON.parse(msg.data.substring(prefix.length));I'd like to do something like dynamic_cast after it based off of the contents of the first element, so:switch (data[0]) {case NaclMsg.NACL_MSG_PRINT_GLYPH:// Some sort of cast to prove that the rest of the list is List<int>
for (int e in data) { // this will throw a TypeError if "e" is not an int// ...}
for (var e in data) {if (e is! int) throw new Exception('not an int');}
case NaclMsg.NACL_MSG_PUTSTR:// Some sort of cast to prove that the remaining arguments are Int, Int, String.
int arg1 = data[1];int arg2 = data[2];String arg3 = data[3];
int arg1 = data[1];int arg2 = data[2];String arg3 = data[3];if (arg1 is! int || arg2 is! int || arg3 is! String) {throw new Exception('invalid argument type');}
interface Foo {doExcitingStuff();}List<Foo> foos = ...;// The following might blow up with a NoSuchMethodError instead of a TypeError,// because there's no runtime check that the thing you got from List<Foo>// is actually a Foo. It might be a completely unrelated class Bar, which may// or may not have a "doExcitingStuff" method.foo[0].doExcitingStuff();