var
vsval
-- mutable vs immutable- String interpolation --
"String of length ${b.length}"
- classes -- Backing Fields
var <propertyName>[: <Type>] [= initializer]
[get() = ...]
[set(value) = field = value]
- function references -
::
just like Java - lambdas:
{ param1, param2, ... -> block }
or{ block }
it
-- implicit lambda parameter
inline
: function attribute keyword- inline utilities:
run
,let
,takeIf
,takeUnless
,repeat
,withLock
,use
. @InlineOnly
=>
avoids non-inline use of that function- !!!!! use inline only on small functions
===>
be aware of increasing code footprint. - +++++ Good for higher-order functions avoiding both: 1) allocation and 2) lambdas calls.
use()
=> inline extension totry
,finally
andclose()
.synchronized()
==
withLock()
which is an extension toLock
object
-
Kotlin's type system is aimed to eliminate
NullPointerException
's -
"leaking this" -- an uninitialized
this
available in a constructor is passed and used somewhere. -
?
for Nullable --val variable: Type? = value
-
?.
-- safe call -- access member only if notnull
. Otherwise returns null.
- Smart casts
- "compiler tracks the is-checks and explicit casts"
- "Safe" (nullable) cast --
val x: String? = y as? String
for
iterates through anything that provides aniterator()
, i.e.- has a member- or extension-function
iterator()
- has a member- or extension-function
- return type of
iterator()
must have:- a member- or extension-function
next()
, and - a member- or extension-function
hasNext()
that returnsBoolean
.
- a member- or extension-function
** All of these three functions need to be marked as operator
**
- Sequence extensions
=>
NOT inlined because of Lazyness - Collections extensions
=>
:- are extensions for
Iterable
and return collections likeList
- are
inline
- are extensions for
asSequence()
likestream()
of Java.- fusion semantics
<=>
horizontal vs vertical evaluation