Dart
Length 8:
main(){}
The shortest Dart program you can write. Every Dart program requires a main
function as an entry point.
Length 7:
a()=>4;
You can use fat arrows for short hand functions. The above code is equivalent to:
a() {
return 4;
}
Length 6:
a as B
The as
keyword allows you to cast an object to any type, in this case the variable a
is cast to type B
. This is very useful when you know the type of an object returned by a dynamic method and want the tooling to reflect that. For instance, if you were working with HTML and queried the DOM for an element, you could specify the type of the element, even though that type is not actually returned:
InputElement input = querySelector('#someInput') as InputElement;
// Go ahead and use input safely as an InputElement
Length 5:
'A$k'
$
is used within Strings for interpolation. In this example if k
was a variable with a value of wkward
, the full value of the interpolated String would be Awkward
.
Length 4:
true
Unlike other languages such as JavaScript, in Dart true
is the only value that is true
. The same applies to false
. Values such as 0
, 1
, ''
, null
are not truthy.
Length 3:
var
Var allows a variable to refer to any type of object. Tools will attempt to infer the actual type of the object.
Length 2:
{}
Dart supports map literals.
Length 1:
;
Semi-colons are mandatory to terminate a statement.
Factoid: Dart is an optionally typed language, which means you can add types to aid developers and tools, but they have no effect on your program's semantics - it will run the same whether you add types, or don't, or even add incorrect types.