Skip to content

Instantly share code, notes, and snippets.

@fmcarvalho
Last active January 10, 2019 15:57
Show Gist options
  • Save fmcarvalho/79dd31eeb5af468ea972c50498008704 to your computer and use it in GitHub Desktop.
Save fmcarvalho/79dd31eeb5af468ea972c50498008704 to your computer and use it in GitHub Desktop.
kotlin essentials
  • var vs val -- 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 to try, finally and close().
  • synchronized() == withLock() which is an extension to Lock 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 not null. 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 an iterator(), i.e.
    • has a member- or extension-function iterator()
  • return type of iterator() must have:
    • a member- or extension-function next(), and
    • a member- or extension-function hasNext() that returns Boolean.

** 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 like List
    • are inline
  • asSequence() like stream() of Java.
  • fusion semantics <=> horizontal vs vertical evaluation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment