Elements of Kotlin
by Mark L. Murphy
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Elements of Kotlin
by Mark L. Murphy
Copyright © 2018-2021 CommonsWare, LLC. All Rights Reserved.
Printed in the United States of America.
Printing History:
December 2021: FINAL Version
The CommonsWare name and logo, “Busy Coder's Guide”, and related trade dress are trademarks of CommonsWare,
LLC.
All other trademarks referenced in this book are trademarks of their respective 5rms.
The publisher and author(s) assume no responsibility for errors or omissions or for damages resulting from the use of the
information contained herein.
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Table of Contents
 $)".!*-(// $) bold-italic #1 #)" .$) /# './1 -.$*)>
I - !
- - ,0$.$/ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1$$
*0- * )/.$ ). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1$$$
&)*2' "( )/. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1$$$
I )/-*0$)"*/'$)
#4B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> S
?)#4*/B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> S
#/*0 /*)*2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> T
**&/''/# */'$).A >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> V
 //$)"*/'$);*-(''4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> W
)/-*0$)"/# '..**& >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> X
*2#$.**&$./-0/0-  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> X
I  2E ''*;*-'AF3(+' .
0.//# // ( )/ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> [
-++ $)0)/$*) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SR
-++ $)'.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SS
*;#/* .#$.*B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SS
.$)"/# '..**& >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SU
0))$)"# . )$++ /.$)) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SV
I .$4+ .)3+- ..$*).
.$4+ .)E% /) ..F >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SY
0( -. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SY
**' ) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TR
/-$)". >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TR
#-/ -. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TT
0$&*/ *0/,0'$/4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TU
I -$' .?# /# -# 4-4*-*/
 '-$)"-$' . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TW
 '-$)" @)'4-$' . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TX
- ! -1'1 -1- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TZ
/-$)")/ -+*'/$*) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TZ
*- + -/*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> T[
*0/*(/$0( -*)1 -.$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> US
 4;#/*0/B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UT
I 0)/$*).
i
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
0)/$*).2$/#-( / -. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UW
Functions with Return Types .................................................... 36
*'-$' . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UY
)$ -0)/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UY
I *'' /$*).)(.
%*-*'' /$*)4+ .)- /$*)0)/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> VU
.$." >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> VY
((0/$'$/4)*'' /$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> V[
)/-*0$)"(3+- ..$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WR
*((*)*'' /$*)+ -/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WT
--". >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WX
/# -*'' /$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WZ
" /JK)LM4)/3 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> W[
I !;# );)#$'
! >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> XS
# ) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> XU
#$' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> XY
!G# ).3+- ..$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> XZ
0./$)D0/ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> YS
I .$'.. .
.$'.. . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> YY
- /$)")./) .*!'.. . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> YY
&" . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> YZ
*((*)*)/ )/. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Y[
/#$. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ZS
*)./-0/*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ZT
)# -$/) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ZZ
I *(( )/.)*0( )//$*)
.$*(( )/4)/3 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SRW
)/-*0$)"* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SRX
I -*+ -/$ .
)$/$'$5/$*) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SST
*)./)/. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SSY
 //$)"*2)$)/#  . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SSY
I $.$$'$/4)*+
$.$$'$/4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SS[
*+ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> STV
I ./-/'.. .))/ -! .
# % /$1 =*)/-/. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> STY
./-/'.. . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> STZ
)/ -! . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUR
ii
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#$#**0. B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUX
I /'..
*2*0 '- / >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SU[
#/*0$) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SU[
#/*0*. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SVT
/'.. .2$/#/# --*+ -/$ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SVT
I # *% / 42*-
$)"' /*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SVW
*(+)$*)% /. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SVY
 ./ % /. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SV[
% /3+- ..$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SV[
I  ./ % /.)'.. .
 ./ % /. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SWU
 ./ '.. . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SWW
 ./ )/ -! .)./-/'.. . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SXR
I )0(.) ' '.. .
)0(. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SXU
 ' '.. . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SYR
I  ) -$.
;#/- # . *-"$)B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SY[
)./)/$/$*)2$/# ) -$. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SZR
++'4$)" ) -$./*'.. .))/ -! . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SZW
++ -*0). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SZY
 ) -$.;B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SZZ
I 3 +/$*).
/#$)"3 +/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SZ[
$.$)"3 +/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> S[V
 7)$)"3 +/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> S[W
# & 1.>)# & 3 +/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> S[X
I ))*//$*).
# - ))*//$*).*( -*( >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> S[[
++'4$)"))*//$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TRR
 7)$)"))*//$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TRW
I 0''$'$/4
)/-*0$)"0''' 4+ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TR[
3+- ..$*).2$/#0''' 4+ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TSR
0''' 4+ .) ) -$. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TSZ
0''' 4+ .)./. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TS[
% /$1 =$)$($5 0''. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TTR
I *+ 0)/$*).
' /JK >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TTU
iii
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
++'4JK >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TTW
-0)JK >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TTZ
2$/#JK >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TT[
'.*JK >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TUR
0. JK >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TUS
0((-4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TUS
I 0)/$*)'-*"-(($)"
*0-++$"#/*/ 0)/$*)' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TUU
# - ((0/$'$/4*( .)/*'4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TUV
3(+' .*!0)/$*)'*/'$) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TUW
0)/$*)4+ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TVS
--4.;*'' /$*).;?) ,0 ) . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TWR
I 3/ ).$*)0)/$*).
# . *!/# /$'$/40)/$*) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TWX
*)& 4$)"-*0) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TWZ
 '-$)"3/ ).$*)0)/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TW[
''$)"3/ ).$*)0)/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TXS
# $($//$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TXS
I 1)/ -*+ -$'$/4
 +*!)/ -*+ -$'$/4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TXU
*/'$)''$)"1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TXV
1''$)"*/'$) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TYV
 *(+$'$)"*/'$)/*1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TZY
I #)" .$) 2 -*/'$) -.$*).
-.$*)S>U >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TZ[
-.$*)S>V >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> T[T
I 0./*( ..*-.
 7)$)"0./*( ..*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> T[[
)/$@// -). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> URS
'/ -)/$1 = ' "/ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> URT
I 3/ ).$*)-*+ -/$ .
 ++$)"3/ ).$*)0)/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> URU
 ++$)"-*+ -/40./*( ..*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> URU
3/ ).$*)-*+ -/$ .^.#0+ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> URV
I .+$)" 42*-.
#  )-$*=*&$/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> URY
# -*' (= 42*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> URY
# *'0/$*)=&/$&. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> URZ
I .+  /#*( .
#  )-$*=)$/ ./. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UR[
# -*' (=*0- $- *!( '. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UR[
iv
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
# *'0/$*)=&/$&. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> USR
I -*+ -/4 ' "/ .
 !- .# -*)'54 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> USS
#/D. ''4*$)") >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UST
/# -/*&-*+ -/4 ' "/ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> USU
I '.. ' "/$*)
'4$)"1*-$/ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> USY
)0' ' "/$*) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> US[
# '.. ' "/ '/ -)/$1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTR
I *)./)/.
 '-$)"*)./)/ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTS
#4*/# -;# ) 1 1'B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTS
*)./)/4+ $($//$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTT
I ./-/-*+ -/$ .
0/;0/;0/?#4B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTU
./-/1' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTU
./-/1- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTW
I *1-$) $) ) -$.
*0)D/0/#/$)#/A >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTY
*0/.$- /$*) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UTZ
 '-/$*)@$/ ). @$/ -$) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UT[
I *)/-1-$) $) ) -$.
 '-/$*)@$/ ). @$/ -$) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UUW
I )*)4(*0.0)/$*).
# -*' (= /0-)4+ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UU[
# *'0/$*)=)*)4(*0.0)/$*)4)/3 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UVR
*)./-$)/.)6 /. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UVR
I *'0)/$*).
 -*0$& 0)/$*).? >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UVU
?*0/0)/$*)$)*0-0)/$*) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UVV
0) +/$*) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UVW
I *'4+ .
 )-$*= -$'.*!$--*"-(($)" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UVY
I )'$) 0)/$*).
-*$./*-4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UWS
)'$) 0)/$*).=$& -*. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UWT
)'$) -*+ -/$ . >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UWU
I )'$) '.. .
#/B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UWW
#4B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UWW
'+#A >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UWY
v
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I  $7 4+ -( / -.
4+ -.0- J/# -#)$/# &.+  4K >>>>>>>>>>>>>>>>>>>>>>>> UW[
 $7 ^ /$) 4+ !*-)'$) 0)/$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UXR
I )*$)'$) )-*..$)'$)
)*$)'$) = +/# (.)% / >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UXU
-*..$)'$) =''*2$)"- /0-) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UXW
I   $1 -.$)0)/$*)4+ .
#/2$/#JK**&.$& >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UXZ
#/++'4JK**&.$& >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UX[
. . = >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UX[
I  )( (+*-/.
#  )-$*=. -1' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UYS
# -*' (=(+*-/( *''$.$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UYT
# *'0/$*)=$(+*-/>>>.>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UYT
I + -/*-1 -'*$)"
#//# QPO:\AB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UYW
# *) +/*!+ -/*-1 -'*$)" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UYW
3(+' =$1$$)"/-$)" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UYX
*>)4>+ -/*-.> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UYX
I )730)/$*).
*./73))73 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UY[
# $)73 42*- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UY[
$($//$*). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UZR
I  ./-0/0-$)" '-/$*).
# *(+*) )/.*!'.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UZS
;#4*0' . #$.B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UZT
*;#4* .#$.3$./B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UZU
I  '  /0-).
# - - /0-)* .4 !0'/ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UZW
 /0-)$)"0./!-*(( >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UZY
++'4$)" ' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UZY
.)4*!#$.** B >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UZZ
I */#$)"
*/#$)"=/D.)/# *//*( >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> U[R
. .*!*/#$)" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> U[R
I 4+ .*! 42*-.
- 42*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> U[W
*!/ 42*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> U[X
*$7 - 42*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> U[Y
) ) -';1*$ 42*-. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> U[Z
vi
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Preface
#)&.A
$-./;/#)&.!*-4*0-$)/ - ./$)*/'$)A*/'$)$.*) *!/# !./ ./@"-*2$)"
+-*"-(($)"')"0" .$)/# 2*-'>))*.(''+-//#/$.0 /***"' D.
)*-. ( )/*!*/'$)!*-)-*$++ 1 '*+( )/;.)-*$$./# 2*-'D.(*./
2$ '4@0. *+ -/$)".4./ (> -#+.4*0- $)/ - ./ $)0.$)"*/'$)!*-2-$/$)"
)-*$++.> -#+.4*0- $)/ - ./ $)*/'$)$)*/# -+' .;.0#.!*-
++.>*/'$)$/. '!$./# .( )*(// -2# - 4*0++'4$/;.*/#$.**&)# '+
4*0" /0+/*.+ 2$/#/# .4)/3)+// -)./#/4*02$''. .4*0- */# -
 1 '*+ -.D*/'$)* )./-/2-$/$)"$/4*0-. '!>
'.*;/#)&.!*-- $)"/#$.**&A#$.$.*) *!. -$ .*!**&.+0'$.# 4
*((*).- ; # .$") /*# '+ 1 '*+ - *( +-*7$ )/$).*( +$ 
*!/ #)*'*"4>
Prerequisites
#$.**&$.2-$// )!*-) 2*( -.$)*/'$)>!4*0#1 '- 4 3+ -$( )/
.*( 2$/#*/'$);4*0($"#/ ' //*.&$(/# 7-./! 2#+/ -.;0)/$'4*0./-/
)*0)/ -$)"/*+$./#/- ) 2/*4*0>
#$.**&* . 3+ /4*0/*#1 +-$*-+-*"-(($)" 3+ -$ ) >'*/*!
*(+-$.*).2$'' ( /*1;.1$.1 -4+*+0'-+-*"-(($)"')"0"
)/# *) /#/2.*($))/!*-/# 7-./  *!)-*$++ 1 '*+( )/>
*0*)*/) /* 1+-*"-(( -/*0. /#$.**&;/#*0"#>
vii
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Source Code and Its License
# .*0- * $)/#$.**&$.'$ ). 0) -/# +# T>R$ ). ;$). 4*0
#1 /#  .$- /*- 0. )4*!$/>
*+4$)".*0- * $- /'4!-*(/# **&;$)/#  $/$*).;2*-&. ./2$/#
*   -;/#*0"#$/(4'.*2*-&2$/#*/# -1$ 2 -.>*( 1$ 2 -.;
!*-- .*)./#/- ($)0)' -;!*0'0+*+4$)"/# .*0- * /*/# '$+*-
2# )$/$.. ' / >
Acknowledgments
# 0/#*-2*0''$& /*/#)& /-$).;/# 7-(/#/- / */'$)>$/#*0/
/# (;/#$.**&2*0')*/ +*..$' >
!*0-. ;2$/#*0//# (;/#$.**&2*0' - ''4./-)" ;.*/'$)2*0')*/
3$./;.*/#$.**&2*0' /-4$)"/* 3+'$)#*2/*0. )*)@ 3$./ )/
+-*"-(($)"')"0" >
PREFACE
viii
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Kotlin Basics
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Introducing Kotlin
*/'$)D.+*+0'-$/4 3+'* $)TRSY;2# )**"' ))*0) *8$'.0++*-/!*-
*/'$) 1 '*+( )/!*-)-*$++.># /# -4*0- '**&$)"/*0. */'$)!*-
)-*$++ 1 '*+( )/*-!*-*/# -.*-/.*!+-*% /.;4*0#1 #*. )*) *!/#
!./ ./@"-*2$)"')"0" .)*) 2$/#/- ( )*0.(*0)/*!E055F>
#$.**&2$''# '+4*0" /0+/*.+ *)/# *- .+ /.*!*/'$);+'0.# '+4*0
0) -./)0)0.0'*/'$).4)/3/#/4*02$'' )*0)/ -!-*(/$( /*/$( >
Why?
*/'$)$.(*./*!/ )*(+- /*1>*/'$)2.*-$"$)''4- / /*. -1 .1
- +' ( )/;/#*0"# /#/./*-4#."*// ).*( 2#/(*- *(+'$/ >'.*;1
2./# +- ($) )/')"0" !*-)-*$++ 1 '*+( )/;).*()4 1 '*+ -.
- *($)"/**/'$)#1$)"#.*( (*0)/*!1 3+ -$ ) >
*(+- /*1;*/'$)#.' ..E - (*)4F>/$. .$") /*''*2* /*
concise;2$/#! 2 -#-/ -.0. /* 3+- ../# .( +-*"-(($)"'"*-$/#(.>
*- 3(+' ;1)()4*/# -+-*"-(($)"')"0" .J >">;;]]K0. 
. ($*'*)J;K/* .$")/ /#  )*!.// ( )/>)*/'$);/# . ($*'*)0.0''4$.
*+/$*)'>#/$..(''#)" $).4)/3;0/*/'$)$.!0''*!/# . .*-/.*!.('' -
#)" .; #+-*1$$)"4*02$/#(*- - .0'/.2$/#' ../0'* >
…And Why Not?
)/# */# -#);*/'$)$.*) *!/# (*./*(+' 3+-*"-(($)"')"0" . 1 -
- / >*-(*./* ;*/'$)) *)$. >*2 1 -;$)()4. .;/#/$.
 0. /# E- '* F$.#$ );.0++'$ 4./)-'$--$ .># +-*"-(($)"
1
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
/ #)$,0 ./#/''*2/#*. '$--$ ./*' /4*02-$/ *)$. */'$)* - '.*
1$'' /*4*0;)/#/$.2# - /# *(+' 3$/4'$ .>
*-/0)/ '4;(*./*!/#/*(+' 3$/4) $")*- //# *0/. />#/*(+' 3$/4
3$./.!*-E+*2 -0. -.F*!*/'$);.0#.+ *+'  1 '*+$)"*/# -*/'$)'$--$ .>
# 4/**)0. /#*. / #)$,0 ./*(& /# $-'$--$ . .4/**).0( >*2 1 -;
()4 1 '*+ -.2$'') 1 - 3($) /# * *!.0#'$--4;' /'*) - / *) ;
).*!*-()4 1 '*+ -.;/#$.*(+' 3$/4$..*( /#$)"/#/2$''  )*0)/ -
*)'4-- '4>
*/'$)'.*' ).$/. '!/*2-. terse +-*"-(($)">E*)$. F$.E.#*-/0/
0) -./)' F;2#$' E/ -. F$.E.#*-/;0/+ -#+.)*/0) -./)' F> +$)"
*/'$)* .#*-/0/./$''- ' ;+-/$0'-'44) 2*( -.;$./# .*-/*!/#$)"
/#/.*( */'$) 1 '*+ -.$")*- $)-$1 /*($)$($5 /# )0( -*!#-/ -.
$)/# $-*/'$).*0- * >
What You Need to Know
#$.**&..0( ./#/4*0#1 .*( (*0)/*! 3$./$)"+-*"-(($)" 3+ -$ )
$))*% /@*-$ )/ +-*"-(($)"')"0" >*# '+ 3+'$) -/$)*/'$).4)/3;
/# **&2$''-2+-'' './*1;1-$+/;)04;/#- +*+0'-
+-*"-(($)"')"0" .>#$' 4*0*)*/)  3+ -$ ) $))4*!/#*. ;4*02$''
) /*&)*2/# .$.*!*% /@*-$ )/ +-*"-(($)">
# !*''*2$)". /$*).*0/'$) /# .*-/.*!+-$*- 3+ -$ ) /#/4*0) /*#1 $)
*- -/*(& /#  ./0. *!/#$.**&>
Data Types and Expressions
 -'4 1 -4+-*"-(($)"')"0" #./# *) +/*!//4+ .=./-$)".;)0( -.;
**' ).;).**)>#  3/-*./ -*!//4+ .1-$ .4')"0" ;.*.*( *!
/#  /$'.*!#*2/#*. //4+ .- $(+' ( )/ >0/;4*0.#*0'
*(!*-/' $)/#$)&$)"*0/#*2./-$)".;)0( -.;).$($'-/4+ .*!/9*2
/#-*0"#*0-+-*"-(($)">
!/ );/# . +$  .*!/- 0. $)'0'/$*).;!*-($)"+-/.*! 3+- ..$*)./*
 -$1 ) 2/>#/*0' (/# (/$' 3+- ..$*).;.0#. 2 + 2>#/*0'
  3+- ..$*)./$ /**/# -//4+ .;.0#../-$)"*)/ )/$*)J >">; "foo" +
"bar"K>#  /$'.*!2#/ 3+- ..$*).- +*..$' )/# $-.4)/31-$ .$/4
+-*"-(($)"')"0" ;0/''')"0" .)*/#$..*-/*!/#$)">
INTRODUCING KOTLIN
2
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Objects and Classes
*/'$)$.'..@. *% /@*-$ )/ ')"0" >0*/$)" $&$+ $=
# (*./+*+0'-) 1 '*+ (* '*!$.'..@. (* ';.
*++*. /*)*% /@. (* '>)/#$.(* ';*% /.-  )/$/$ ./#/
*($) .// J$> >/K; #1$*-J$> >+-* 0- .;*-( /#*.K)
$ )/$/4J0)$,0  3$./ ) (*)"''*/# -*% /.K># ./-0/0- )
 #1$*-*!)*% /-  7) 4'..;2#$#$. 7)$/$*);*-
'0 +-$)/;*!''*% /.*!.+ $7/4+ >)*% /(0./  3+'$$/'4
- / . *)'..))*% //#0.- / $.*).$ - /* )
$)./) *!/#/'..>
1)04- '..@. *% /@*-$ )/ ')"0" .;2# - 2 0. /# class
& 42*-/* "$)/#  .-$+/$*)*!'..>1-$+/*-$"$)''42.)*/'..@
. ')"0" ;/#*0"#-  )/0+/ .#1 +0'' $/(0#'*. -/*$/.'..@. 
*0)/ -+-/.>
Methods or Functions
)'..@. *% /@*-$ )/ ')"0" ;2 ) /*/ ''/# '..2#/$/.
 #1$*-.- > Box '..($"#/#1 open() ) close()  #1$*-.;!*- 3(+' >
)1)04;/#*.  #1$*-.2*0' $(+' ( )/ $)/# !*-(*!E( /#*.F>
)*/'$))1-$+/;/#*.  #1$*-.- '' E!0)/$*).F>
Fields, Properties, and Variables
$($'-'4;$)'..@. *% /@*-$ )/ ')"0" ;2 ) /*/ ''/# '..2#/
/$/#./*2*-&*)> Box '..($"#/#1 =
I /. contents J- +- . )/$)"2#//# *3#*'.K
I /. material J- +- . )/$)"2#//# *3$/. '!$.( *!=-*-;2**;
./ ';()/$0(; />K
I /. isClosed .//
I /. length; width;) height
I ).**)
# / -(!*-/#$.1-$ .2$ '4=
I 1- ! -./*/# (.E7 '.F
INTRODUCING KOTLIN
3
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I 04- ! -./*/# (.E$)./) 1-$' .F
I 1-$+/)*/'$)- ! -/*/# (.E+-*+ -/$ .F
'.*;*0-( /#*.*-!0)/$*).2$''#1 / (+*--4#*'$)".+*/.!*-$/.*!/>
# $)+0/$.0.0''4- ! -- /*.E+-( / -.F*-E-"0( )/.F;.* seal() !0)/$*)
($"#/ +/+-( / -$)$/$)"#*2/# *3.#*0' . ' J2$/#/+ ;2$/#
./+' .;2$/#2 '.; />K># 2*-&$)"/$).$ /# !0)/$*)$.0.0''4'' 
E1-$' .F;.* seal() !0)/$*)($"#/0. /# length; width;) height +-*+ -/$ .
/*'0'/ #*2(0#/+ $.)  /*. '/# *3;#*'$)"/#/'0'/ 1'0
$)1-$' >
Look At All the Kotlins!
#$.**&$.!*0. *)*/'$).4)/3;.*4*00) -./)#*2/*- )2-$/
*/'$)* >*2 1 -;0.0''4;$!4*0- 2-$/$)"*/'$)* ;/# +*$)/ #$)/#
* $.)*/!*-$//*%0./.$//# - >*./'$& '4;4*0+')/*-0)/#/* >
-*"-(($)"')"0" ..*( /$( .#1 1-$ /4*!24./#//# $-* )
-0)>*- 3(+' ;04* ) -0)*)! 204$)/ -+- / -.;*-*)/# 1
$-/0'#$) JK1$04;*-+*..$'4*(+$' $)/*)/$1 * >!/#*.
*+/$*).;/# ./)-04$)/ -+- / -$./# (*./*((*);)$./# *) /#/
+ *+' / )/*/#$)&*!>$($'-'4; .+$/ * D.+*+0'-$/4;2# )'*/*!+ *+'
/#$)&*0/-0))$)"1-$+/* ;/# 42$''/#$)&*!*$)".*$).$ *!
-*2. ->
$($'-'4;*/'$)#.! 2E-0)/$(  )1$-*)( )/.F/**).$ ->
Kotlin/JVM
!4*0%0./# -/# )( E*/'$)F;2$/#*0/)4,0'$7 -.;$/$.'$& '4/#//# + -.*)
$),0 ./$*)$.- ! --$)"/*/# *-$"$)'!*-(*!*/'$);2#$#-)*)/# >*/'$)D.
*(+$' -2*0'- / 14/ * !-*(/# */'$).*0- * ;%0./.1D.
*(+$' -- / .14/ * !-*(1.*0- * ># - .0'/$)"4/ * )
0. 4 java ).$($'-/**'.;- "-' ..*!2# /# -$/2.2-$// )$)1;*/'$);
*-.*( /#$)" '. >
)+-/$0'-;$!4*0- $)/ - ./ $))-*$++ 1 '*+( )/;2# )4*0/#$)&*!
*/'$);(*./'$& '44*0- /#$)&$)"*!*/'$) $)"*(+$' $)))-*$+-*% />
)/#/. ;2#$' )-*$* .)*/0. /# ;/# .( .$( #)$.($.
++'$ =*/'$)D.*(+$' -" ) -/ .14/ * ;2#$#/# )-*$0$'+-* ..
INTRODUCING KOTLIN
4
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
/# )*)1 -/.$)/*'1$&4/ * !*-0. $))-*$++.>
Kotlin/JS
*-/# 7-./! 24 -.*!*/'$)D. 1 '*+( )/;*/'$).$(+'42.*/'$)G>
)TRSV;*/'$)G2.$)/-*0 >#$.''*2.*/'$)D.*(+$' -/*E*(+$' F*/'$)
.*0- * $)/*1-$+/.*0- * ;2$/#) 4 /*2-./#/*  $)"0. $)
) )1$-*)( )/'$& * ># 1-$+//#/4*0" /!-*(/# *(+$'/$*)
+-* ..$.)*/)  ..-$'4"*$)"/*  .$'4- ' 4*-$)-4#0().;0//#/D.
)*//# "*'># "*'$./* ' /*2-$/ $)*/'$))-0)$/$)/-$/$*)'
1-$+/-0)/$(  )1$-*)( )/>
Kotlin/Native
*- -  )/'4;/# */'$)/ (#. */'$)G/$1 >#$.*(+$' .*/'$)*
/*)/$1 *+* .;/# .( .4*0($"#/7)2$/#G]]*(+$' ->)+-/$0'-;
*/'$)G/$1 $. .$") /*" ) -/ * /#/)-0)*)$)(;
$)/ -*+ -/$)"2$/#% /$1 @>
Kotlin/Common
*/'$)G*((*)* .)*/- +- . )/) 2-0)/$(  )1$-*)( )/!*-*/'$);/' ./
$)/# ./-$/. ). >*/'$)G*((*)$./# / -("$1 )/*/4+ *!'$--4/#/'$($/.
$/. '!/*0.$)"*/'$)'.. .)!0)/$*)./#/ 3$./!*-''*/'$)-0)/$(
)1$-*)( )/.>0#'$--4) 0. 4*/'$)G+-*% /. .$'4.4
*/'$)G*-*/'$)G/$1 +-*% /;!*- 3(+' >
Kotlin/Multiplatform
*/'$)G0'/$+'/!*-('.*$.)*/) 2-0)/$(  )1$-*)( )/!*-*/'$)>/# -;$/$.
24*!. //$)"0++-*% /.0#/#/.*( *- ++'*"$$.2-$// )$)*/'$)G
*((*)'$--4;2#$' */# -E(*0' .F- . /0+!*-.+ $7-0)/$(
)1$-*)( )/.>1 -'';/#  )/$- +-*% /)/-" /T]-0)/$(  )1$-*)( )/.;.0#
.0.$)"*/'$)./# !*0)/$*)!*-*/#))-*$J*/'$)GK)$J*/'$)G
/$1 K++>
Getting Kotlin, Normally
*-()4 1 '*+ -.;4*02$'')*/) /**2)'*)$)./''*/'$)
INTRODUCING KOTLIN
5
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
)1$-*)( )/;./#/2$'' #)' 44*0-*-*/# -0$'/**'.>
*- 3(+' ;)-*$/0$*0. -." /*/'$)E!*-!- F.+-/*!0$'$)")-*$
/0$*@. +-*% /.>*/'$)D./**'#$)H*(+$' -.; />H- */$) $)/#
!*-(*!-' +'0"$).;*0+' 2$/#.*( */'$).0++*-/E& $)/*F)-*$
/0$*$/. '!>$($'-'4;/# */'$)-0)/$( $/.-  1$-' /*))-*$
+-*% />'0"$).- 1$'' !*-)/ ''$;'$+. ;)*/# -.>
*2 1 -;4*0)'.* *2)'*./)'*) */'$)*(+$' - $! .$- >
Introducing the Klassbook
) .424/* 3+ -$( )/2$/#+-*"-(($)"')"0" $./*0. >
$.)-*)4(; .-$$)"/**'/#/2$'' R /4+ @$).*0- * ; E1'0/
$/; P-$)//# - .0'/.*!/#/ 1'0/$*)$)/# /**';) L**+&/*+-* ..(*-
$)+0/>- ,0 )/'4;- . (' .)*+ -/$)".4./ (.# ''*-/ -($)';.0#.
$)*2.D*(()-*(+/*- bash .# ''$)(*-$)03>
.$)""$1 .4*0-+$! &$)'*2@ 6*-/ )1$-*)( )/>*0*)*/) 
!0''*-!0''++'$/$*)+-*% //* 3+ -$( )/2$/#$/.*!')"0" .4)/3)
. #*2/# 42*-&>
'..**& $.. /0+!*-0. 2$/#/#$.**&>*./*!/# *  3(+' ..#*2)
$)/#$.**&#1 *-- .+*)$)"'..**&+" .;2# - 4*0). /# * ;-0)
/# * /*. /# - .0'/.;) 1 ) $//# * /*/-44*0-*2) 3+ -$( )/.>
2$''. #*2/*0. /# '..**&(*- $) /# ) 3/#+/ ->
How This Book is Structured
#$.**&#./2**% /$1 .=
I  '+4*0./-/2-$/$)".$*/'$)*
I  '+4*0./-/- $)"1) */'$)*
)4 1 '*+ -.*)*/) /* 2-$/$)"1) */'$)* ;0/ 1 '*+ -.
2$''. !)4*/'$).4)/3!-*(/$( /*/$( J*)! - ) +- . )//$*).;'*"+*./.;
/>K>1 )/#*. 2#*2$'' 2-$/$)"!$-'4.$*/'$)2$'') /* ' /*
E * F*(+' 3*/'$)2# ))  >
INTRODUCING KOTLIN
6
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*/#/ );/#$.**&$.$1$ $)/*/2*(%*-. /.*!#+/ -.=/# *- #+/ -.;
)/# EBF#+/ -.>
The Core Chapters
# *- #+/ -.;./-/$)"2$/#/# ) 3/#+/ -;2$''!*0.*)=
I .$*/'$).4)/3=- /$)"'.. .;!0)/$*).;+-*+ -/$ .;) 3+- ..$*).
I $ '4@0. */'$)$$*(.=/#$)"./#/- '$//' ./-)" 2$/#- .+ //*
*/# -+-*"-(($)"')"0" .0/" /0. '*/$)*/'$)/*- 0 /# .$5
*!/# .*0- *
!4*0(./ -/# *) +/.*0/'$) $)/# . #+/ -.;4*0.#*0' ' /* 
+-*0/$1 */'$) 1 '*+ ->
The “WTF?” Chapters
# - ($)$)"#+/ -.!*0.*)1) */'$).4)/3;!*-/# .*-/.*!/#$)"./#/
(*./ 1 '*+ -.2$'')*/0. 1 -4!- ,0 )/'4>*./'$& '4;4*02$'' )*0)/ -/#/
.4)/32# )- $)" 3$./$)"*/'$)* >*/# . #+/ -.- *-")$5 -*0)
.+ $7$/.*!.4)/3/#/4*0($"#/ )*0)/ -; 3+'$)$)"/# $--*' )0. >
INTRODUCING KOTLIN
7
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
A Few “Hello, World!” Examples
 /D../-/'**&$)"/.*( .$E# ''*;2*-'AF$/.*!*/'$)* ;)'*)"/#
24. #*24*0)-0)/#$.* $)/# '..**&*- '. 2# - >
Just the Statement
println("hello, world!")
J!-*( N ''*;*-'AJ)0)/$*)KN$)/# '..**&K
# *1 * .)$++ /$..$(+' */'$).// ( )/;*) /#/+-$)/.E# ''*;2*-'AF
.*( 2# - >
9
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
 '*2/# * .)$++ /;4*02$''. '$)&>!4*0- $)) **&-  -/#/
.0++*-/.'$)&.;4*0)'$&$//*1$.$//# '..**&+" *-- .+*)$)"/*/#/
* .)$++ /=
Figure 1: Klassbook, Showing a Lesson
!4*0'$&/# E0)F0//*);/# - .0'/.*!-0))$)"/# */'$)* 2$''++ -
 '*2/# * $)/# -*2. -=
Figure 2: Klassbook, Showing the Results
Wrapped in a Function
)- '$/4;/#*0"#;/# - $.$/(*- /*/#$.*/'$)* /#)2#/4*02$''$)$/$''4
. >
# )4*0. )E3+)F0//*)*1 /# * ;/#/( )..*( *!/# * $.
 $)"#$ )!-*(1$ 2;/*!*0.4*0-// )/$*)*)/*.*( .+ $7. /*!'$) .>
A FEW “HELLO, WORLD!” EXAMPLES
10
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
'$&$)"/# E3+)F0//*)2$''.#*2/#  )/$- $/*!*/'$)/#/2 -
3 0/$)"=
funfun main() {
println("hello, world!")
}
#$. main() !0)/$*)2*-&..$($'-'4/*/# .//$ main() ( /#*$)1+-*"-(;
$)$/$)"/# !0)/$*)/#/.#*0' '' /*-0)/# 1+-*"-(>
#*!/# '..**&.)$++ /.#. main() !0)/$*)>*( /$( ./# !0''*/'$)
.)$++ /2$'' 1$.$' $)/# +" ;)/# - 2$'' )*E3+)F0//*)>/# -
/$( .;/# main() !0)/$*)2$'' #$ );0//# E3+)F0//*)2$''.#*2/#
!0''.)$++ /;)*/%0./*) +*-/$*)>
Wrapped in a Class
# +- 1$*0.#+/ -( )/$*) /#/*/'$)$.'..@. *% /@*-$ )/ 
')"0" >*;$!2 2)/ ;2 *0'2-+/#$.* $)'..)0. /#/>
classclass HelloWorldHelloWorld {
funfun speak() {
println("hello, world!")
}
}
funfun main() {
HelloWorldHelloWorld().speak()
}
J!-*( N ''*;*-'AJ)'..KN$)/# '..**&K
!4*0-0)/#$.;4*0" //# .( *0/+0/./# .$(+' .// ( )/> %0./2-++ $/
$)'../*.#*2*6*/'$)'.. .$/>
So, What Does This Do?
 /D. 3($)  #*!/# +$  .*!2#/2 #1  ) 3 0/$)";)0. /#*. /*
.#*24*02# - 2 2$''  3+'*-$)"$)/#$.**&>
println("hello, world!")
A FEW “HELLO, WORLD!” EXAMPLES
11
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
$-./;2 #1 "hello, world!">#$.$.*/'$)./-$)">/'**&.(0#'$& ./-$)".$)
1;04;1-$+/;)()4*/# -+-*"-(($)"')"0" .>*/'$)./-$)".- 
$/(*- +*2 -!0'/#)/#*. $)1;.*/'$)*+/ .*( *!/# ./-$)"! /0- .
*6 - $)*/# -+-*"-(($)"')"0" .> 2$''/& '*. -'**&/./-$)".$) /#
) 3/#+/ ->
 3/;2 #1 println()>#$.$.*/'$)!0)/$*);*) /#/ +/../-$)".$)+0/
)+-$)/.$/?.*( 2# - >#  3/'*/$*)*!2# - /#/./-$)"" /.+-$)/ 2$''
 + )$/*)2# - */'$)$.-0))$)"=
I );$/2$''+-$)/2# - 1 -/# +-$)/../06;.0#./# '..**&
.#*2$)"$/ '*2/# *
I )))-*$++;$/2$''++ -$)*"/
I )*(()@'$) +-*"-(;$/2$''+-$)//*/# / -($)'2$)*2*-
2# - 1 -E./)-*0/F#. )- $- / /*
I ).**)
1+-*"-(( -.2$''2*) -2# - /# '..)( /#*- >)1;2 *)*/
#1 ./)'*) '$) .*!* >/# -;''* "* .$)'...*( 2# - ;0.0''4
$).$ *!( /#*$)/#/'..>.0''42 import /# '..)/# )''/#
( /#**)/#/'..;.0#. System.out.println();2#$#$./# 1 ,0$1' )/*!
println() $)*/'$)>*/'$).0++*-/..*( .// ( )/.)!0)/$*)./#/- )*/
+-/*!)4'..;&$)/*#*204)1-$+/)#1 /# $-*2)"'*'
( /#*.)!0)/$*).>
1 )/0''4;2 2-++ /# println() $)!0)/$*))+0/$/$).$ '..=
classclass HelloWorldHelloWorld {
funfun speak() {
println("hello, world!")
}
}
#$.2*-&..$($'-'4/*#*2'.. .)( /#*.-  7) $)1)04>
1-$+/D.*% /(* '$..*( 2#/$6 - )/0/;$)/#  );4*0)*(+'$.#
(0#/# .( /#$)"> 2$'' 3($) !0)/$*). $))0+*($)"#+/ - )
'.. . $/!/ -/#/>
/# )- / )$)./) *!*0-'..)'' *0- speak() !0)/$*)=
HelloWorldHelloWorld().speak()
A FEW “HELLO, WORLD!” EXAMPLES
12
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*/'$)#.1 -4/ -. .4)/3!*-- /$)")$)./) *!'..=%0./0. /# '..
)( '$& !0)/$*)>*; HelloWorld() - /0-).)$)./) *! HelloWorld *% />
# - $.)*) !*-& 42*-;/# 241;04;*-1-$+/0. . new>
''$)"!0)/$*)*))*% //# )2*-&.(0#'$& */# -*% /@*-$ )/ 
')"0" .H0. . !*''*2 4/# !0)/$*)''Jspeak()K>
*( /$( .;*0-!0)/$*)./& +-( / -.;.2 .22$/# main()=
funfun main(args: ArrayArray<StringString>) {
HelloWorldHelloWorld().speak()
}
 - ; args $.+-( / -;*!/4+ Array<String>>*/'$)$.E./-*)"'4/4+ F
')"0" = 1 -4/#$)"$)*/'$)$./$ /*.*( /4+ >#$.$..$($'-/*10/$./$)/
!-*(04)1-$+/;2#$#*)*/0. /4+ .$)/#$.!.#$*)> 2$'' 3+'*-
+-( / -.)/# $-0. *!/4+ .(*- $) /# #+/ -*)!0)/$*).> 2$'''.*
'**&//#/)"' @-& /.4)/3H+-/*!*/'$)D..0++*-/!*-" ) -$.H $))
0+*($)"#+/ ->
1 -/# ) 3/. 1 -'#+/ -.;2 2$''  3($)$)"(*- *0//# . .$
0$'$)"'*&.*!*/'$).4)/3;+'0.2 2$''  3+ -$( )/$)"2$/#(*-  '*-/
* .)$++ /.>
Using the Klassbook
#$.**&#.#0)- .*!* .)$++ /.;(*./*!2#$#2$''#1 *-- .+*)$)"
'..**&+" .># *(+)$*)**& Elements of Kotlin Coroutines '.*
*)/-$0/ .$/..)$++ /./*/# '..**&.2 ''>
# '..**&#.. 1 -'$/$*)'! /0- .!*-# '+$)"4*0' -)*/'$)>
Editing the Snippets
#+" #.)E$/F0//*)*1 /# * >'$&$)"/#/*)1 -/./# * $)/*
'$1  $/*-;2# - 4*0)(& #)" .>/'.* 3+)./# * ;'$& /#
E3+)F0//*)* .;.*4*0)2*-&2$/#/# !0''.)$++ />
) 4*0#1 ( #)" .;'$&E0)F/*-0)/# - 1$. */'$)* >/2$''/&
! 2. *).!*-/# */'$)* /* E/-).+$' F$)/*1-$+/)/# )-0)$)
4*0--*2. ->
A FEW “HELLO, WORLD!” EXAMPLES
13
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
!4*0(& .4)/3 --*-;/#  --*-( .." .2$''++ - '*2/# * ;
#$"#'$"#/ $)- >
The Scratch Pad
#+" #.E-/#F0//*)$)/# )1->'$&$)"/#//& .4*0/*
+" /#/%0./*)/$)./# *  $/*-)E0)F0//*);2$/#*0//# - ./*!/#
*)/ )/*-2$" /./#/4*0. *)' ..*)+" .>*0)0. /# .-/#+!*-
4*0-*2)*/'$) 3+ -$( )/.>
*/ ;/#*0"#;/#/4*0- '$($/ /*S*!*/'$).*0- * $)/#  $/*->*0)
/4+ $)(*- /#)/#/;0/)4/#$)"'*)" -2$'')*/-0)>
Navigating
*0'- 4.2#*2/*"*!-*(/#$.**&/*/# '..**&/*-$)"0+- ' 1)/
' ..*)>*2 1 -;/# - - */# -)1$"/$*)*+/$*).$)'..**&$/. '!>
Prev/Next Buttons
# )1-#.E- 1F)E 3/F0//*)./*1) /#-*0"#/# ' ..*).$)*- ->
#/2$''" ) -''4!*''*2/# *- -$)2#$#/# 4++ -$)/# **&;/#*0"#/# -
2$'' *.$*)' 1$/$*).>
Tags
#' ..*)#.*) *-(*- /"...*$/ 2$/#$/>#*. ++ -*)/# .$ *!/#
+" >'$&$)"/"2$''-$)"0+.-*''' '$./*!*/# -' ..*)./#/#1 /# .(
/">
*0)'.*'$&/# E -#F0//*)$)/# )1-/*-$)"0+'$./*!''/".>
'$&$)"*)/"/# - *) "$)-$)".0+'$./*!' ..*)...*$/ 2$/#/#//">
Running These Snippets in an IDE
*0- '.*2 '*( /*/-4/# . */'$).)$++ /.*0/.$ *!/# '..**&>
A FEW “HELLO, WORLD!” EXAMPLES
14
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
)-*$/0$*))/ ''$ ##.*/'$);1$'' !-*(/# **'._
*/'$)_*/'$)($)( )0*+/$*)=
Figure 3: Android Studio Kotlin REPL, As Initially Launched
# '..**&$.. /0+/*-0)4*0- main() !0)/$*)2# )4*0'$&/# E0)F
0//*)>)-*$/0$*D.)D.$.)*/>!4*0*+4'..**&.)$++ /
)+./ $/$)/*/# ;4*02$'') /*'' main() 4*0-. '!;4.$(+'4/4+$)"
main() *)/*. +-/ '$) !/ -/# +./ @$)* ># );'$&/# "- )E-0)F
/-$)"' 0//*);*-+- .. Ctrl-Enter *)$)*2.G$)03(#$) .;/*-0)/#
* ># *0/+0/2$'' +-$)/ $)"- )$/'$.$(( $/ '4 '*24*0-* =
Figure 4: Android Studio Kotlin REPL, After Running Code
! 2* .)$++ /.2$'')*/2*-&$)/# )-*$/0$*G>#/$.
 0. '..**&$.0.$)"*/'$)G;2# - .)-*$/0$*)0. */'$)G
>*./*!/# */'$)/#/2 . 2$'' /# .(  /2 )/#*. +'/!*-(.;0/
/# - 2$'' *.$*)'$6 - ) .>
A FEW “HELLO, WORLD!” EXAMPLES
15
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
'.*;)4* .)$++ /./#/+-$)/(*- /#)*) '$) 2$'')*/2*-&2 ''$)/# @
. .># 42$''/ )/**($) ''/# *0/+0/*)/**) '$) =
Figure 5: Android Studio Kotlin REPL, Showing Poor Results
A FEW “HELLO, WORLD!” EXAMPLES
16
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Basic Types and Expressions
- //4(0# 1 -4+-*"-(($)"')"0" #./# *) +/*!//4+ .;/#$)".'$&
$)/ " -.)./-$)".).*!*-/#>*/'$)$.)* 3 +/$*)>*/'$)D..$/4+ .-
(* ' .*( 2#/!/ -1;.*/'$)D.*-$"$)'/-" / )1$-*)( )/2./# >
*2 1 -;/# - - .*( $6 - ) ./#/4*02$'' )*0)/ -$)4*0-*/'$)
 1 '*+( )/2*-&>
Basic Types and “Objectness”
-*(*/'$)D../)+*$)/; 1 -4/#$)"$.)*% />#$../).$)*)/-.//*.*(
')"0" .;'$& 1;2# - .$/4+ .- E+-$($/$1 .F0/#1 *% /*0)/ -+-/.>
*;$)1; int $.+-$($/$1 ) Integer $./# *-- .+*)$)"*% //4+ >*/'$)
* .242$/#/#/$./$)/$*);.* 1 -4/#$)"$.)*% /;$)'0$)"/#$)".'$&
)0( -.>
Numbers
*/'$).0++*-/./# .( .$)0( -$/4+ ..* .1># - - !*0-$)/ "-'
/4+ .=
I Byte JZ@$/- +- . )//$*)K
I Short JSX$/.K
I Int JUT$/.K
I Long JXV$/.K
)/# - - /2*9*/$)"@+*$)//4+ .=
I Float JUT$/.K
17
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I Double JXV$/.K
# . - '.*!$-'4*((*)-*..*/# -+-*"-(($)"')"0" .;).*$/.#*0'
 !$-'4 .4/*" /0. /*/# (>
Number Literals
*( /$( .;4*02$''0. )0( -.$- /'4$)4*0-* ;.'$/ -'1'0 .;.0#. 1234
*- 3.14159>4 !0'/;/# . 2$'' ) Int ) Double;- .+ /$1 '4>!4*02)//*
(& '$/ -'  Long;) L .083=
I 1234 $.) Int
I 1234L $. Long
!4*02)/9*/$)"@+*$)/'$/ -'/* /- / . Float;)*/ Double;++ ) f /*
/# )0( ->
!4*0-0)/#$..)$++ /*)/# '..**&.$/ =
println(1234::classclass)
println(1234L::classclass)
println(3.14159::classclass)
println(3.14159f::classclass)
J!-*( N-$)/$)"/# '..*!0( -.N$)/# '..**&K
?4*0.#*0'" /=
class Int
class Long
class Double
class Float
))-*$/0$**-)/ ''$;4*02$''" /=
class kotlin.Int
class kotlin.Long
class kotlin.Double
class kotlin.Float
 - ;/# ::class .4)/3.4.E"$1 ( - ! - ) /*/# */'$)'..!*-/#$.*% /F>
*; 1234::class - /0-).)*% //#// ''.4*02#/'.. 1234 $.>)/#$.. ;$/$.
kotlin.Int H Int  $)"/# '..;)/#/'.. $)"$)E+&" F)( 
BASIC TYPES AND EXPRESSIONS
18
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
kotlin> 2$''/'&(*- *0/+&" . $))0+*($)"#+/ -;.*%0./$")*-
/#/+-/!*-)*2>
*-'*)" -)0( -.;$!4*0'$& ;4*0)0. 0) -.*- .!*-/# E/#*0.).
. +-/*-F>*/#$.=
println(1_234)
J!-*( N.$)") -.*- .!*-#*0.). +-/*-.N$)/# '..**&K
"$1 .4*0=
1234
#)$''4;/#*. 0) -.*- .)"*)42# - ;.* 1_2_3_4 $.'.*+ -! /'41'$>
*-$)/ "-'/4+ .;/#  !0'/- +- . )//$*)$. $('!*-(/>*0) 7)
'$/ -'.$)# 3 $('40.$)") 0x +- 73J0xFFA4C639K>*0)'.* 7)
$)-4'$/ -'.40.$)") 0b +- 73J0b10110100K>
J)!*-/#*. *!4*02*) -$)"*0/*/'.0++*-/;'$& 1#.?$/$./# TS./
 )/0-4;))**40. .*/')4(*- K
Mathematical Expressions
*0-.$(/# (/$'*+ -/*-.- 1$'' $)*/'$)./# 4- $)(*./*/# -
+-*"-(($)"')"0" .=
I + !*-$/$*)
I - !*-.0/-/$*)
I * !*-(0'/$+'$/$*)
I / !*-$1$.$*)
I % !*-/# - ($) -!/ -$1$.$*)JE(*0'*FK
- )/# . .) 0. !*-"-*0+$)"/**6 -()0'*)/-*'*1 -/# *- -*!
*+ -/$*).>#  !0'/*- -*!+-   ) +0/.(0'/$+'$/$1 *+ -/$*).J*; /; %K
#$"# -/#)$/$1 *+ -/$*).J+; -K>
*=
println(1+2*3)
println((1+2)*3)
BASIC TYPES AND EXPRESSIONS
19
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
J!-*( N.$/#N$)/# '..**&K
- .0'/.$)=
7
9
Boolean
*/'$)*6 -. Boolean /4+ ;2$/#/2*1'0 .= true ) false># . 2*-&+- //4
(0#./# 4*$)*/# -+-*"-(($)"')"0" .>*- 3(+' ;2 2$''. #*2/*
0. Boolean 1'0 .!*- if 3+- ..$*). '/ -$)/# **&>
# /4+$'.*-/.*! Boolean *+ -/*-./#/4*0(4 0. /*!-*(*/# -
+-*"-(($)"')"0" .- 1$'' /*0.=
I && !*-'*"$'
I || !*-'*"$'
I ! !*-'*"$'
Strings
String;- +- . )/$)"+$  *!/ 3/;$.*((*)+' /4+ $)(*./+-*"-(($)"
')"0" .;)*/'$)$.)* 3 +/$*)>*2 1 -;*(+- /*')"0" .'$& 1;
*/'$)*6 -."- / - 3+- ..$1 ) ..2# ) '-$)"./-$)"'$/ -'.>
String Quoting Options
/$.!$-'4'$& '4/#/4*0- 0. /*+-*"-(($)"')"0" .2# - ./-$)".-
 )*/ 4*0' @,0*/ J"K#-/ -.;.0#.$)*0-E# ''*;2*-'F 3(+' =
println("hello, world!")
J!-*( N ''*;*-'AJ)0)/$*)KN$)/# '..**&K
)1;/#/$.4*0-*)'4*+/$*)>/# -')"0" .;'$& 04;*6 -'*/.*!+*..$'
24./* '- ./-$)".;.0#..$)"' @,0*/ 1'0 .J'Hello, world!'K>*/'$)!''.
$) /2 );*6 -$)"/2*./-$)",0*/$)"*+/$*).=*0' @,0*/ .)/-$+' @,0*/ .
J"""K>
*0' @,0*/ ./-$)"2*-&.'$& $/.1*0)/ -+-/=
BASIC TYPES AND EXPRESSIONS
20
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I /))*/*)/$)) 2'$) .
I + $'#-/ -.;.0#.) 2'$) .*-/.;) /* $)$/ 1$E .+
. ,0 ) .F
# - -  $"#/ .+ . ,0 ) ..0++*-/ $)*/'$)=
I \t !*-/
I \b !*-&.+
I \n !*-) 2'$)
I \r !*-E--$" - /0-)FJ$!4*0- 0) -WR4 -.*';.&4*0-"-)+- )/.
2#/E--$" F2.2$/#- .+ //*E/4+ 2-$/ -FK
I \' !*-.$)"' ,0*/
I \" !*-*0' ,0*/
I \$ !*-*''-.$")
I \\ !*-&.'.#
)4/#$)" '. )  )* 0.$)")$*  .+ . ,0 ) .J >">; \u221E !*-/#
$)7)$/4.4(*'=`K
/-$+' @,0*/ E-2F./-$)";*)/# */# -#);* .)*/.0++*-/ .+ . ,0 ) .
0/* ..0++*-/ (  ) 2'$) .;/.;))4/#$)" '. >*;/#$.*/'$)=
println("""Hello,
world!""")
J!-*( N2/-$)".N$)/# '..**&K
+-*0 ./#$.*0/+0/=
Hello,
world!
#$.''*2.4*0/*$- /'4 3+- .../-$)"./#/*/# -2$. 2*0' ( ..*!/ 3/
) .+ . ,0 ) .;- .0'/$)"$)(*- - ' * >
*2 1 -;$) )/./# ) *( +-*' (>!4*0-2)/./*$) )/* /*& +
/#$)".'$") ;$/($"#/.+ .*-/.$).$ *!4*0-./-$)"/#/4*0*)*/
2)/>*# '+2$/#/#$.;4*0)0.  trimMargin() !0)/$*)*) String /* '$($)/
0)2)/ $) )//$*)>
*- 3(+' ;' /D.'**&/=
BASIC TYPES AND EXPRESSIONS
21
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun main() {
println("""Hello,
world with extra whitespace!""")
println("""Hello,
>world using >!""".trimMargin(">"))
println("""Hello,
|world using |!""".trimMargin())
}
J!-*( N/-$(-"$)JKN$)/# '..**&K
# 7-./ println() ''- .0'/.$) 3/-2#$/ .+  $)" (  $)/# - .0'/;.
/# . *)'$) $.$) )/ =
Hello,
world with extra whitespace!
# */# -/2* println() ''.0. trimMargin()># '// -*!/#*. 0. ./#  !0'/
+$+ J|K./# (-"$)$)$/*-;2#$' /# */# -0. .0./*(#-/ -J>K;+..$)"
/#/#-/ -.+-( / -/* trimMargin()>*/#- (*1 *0-$) )/=
Hello,
world using >!
Hello,
world using |!
String Expressions
/-$)"*)/ )/$*)2*-&.0.$)"/# + *+ -/*-.4*02*0' 3+ /=
println("Hello, " + "world!")
J!-*( N/-$)"*)/ )/$*)N$)/# '..**&K
*2 1 -;4*02$''7)'*/' .../-$)"*)/ )/$*)0. $)*/'$)/#)4*0(4
0. /*$)*/# -')"0" .'$& 1>#/$. 0. */'$);'$& 04;.0++*-/.
E./-$)"$)/ -+*'/$*)F;2# -  3+- ..$*). (  $)./-$)"'$/ -'.)
1'0/ $- /'4> 2$''. /#/$)/$*) '/ -$)/# **&>
Characters
.$*)''4;2 ) /* '2$/#$)$1$0'#-/ -.;)*/'*)" -./-$)".>
#-/ -.) - +- . )/ 41 -4.#*-/./-$)".;$! .$- >*2 1 -;%0./.1
#. char ) Character /*- +- . )/$)$1$0'#-/ -.;*/'$)/**#.
BASIC TYPES AND EXPRESSIONS
22
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Character '..>
)*/'$);.2$/#1; Character '$/ -'$.- +- . )/ 4.$)"' @,0*/ 
#-/ -=
println('a')
J!-*( N#-/ -.N$)/# '..**&K
'.*;.$($'-/*1; .+ . ,0 ) .) 0. !*-$)$1$0'#-/ -.;(0#
./# 4)2$/#./-$)".>
A Quick Note About Equality
)(*./+-*"-(($)"')"0" .;/# == *+ -/*-- +- . )/..*( !*-(*!)
,0'$/4# &>#  3/( )$)"*! == 1-$ .4')"0" >
*- 3(+' ;$)/# . *!1; == ( ).$)./)  ,0'$/4;2# - /# ' !/#))
-$"#/#)*% /.- /#  3/.( *% />*-*)/ )/ ,0'$/4;10. .)
equals() ( /#*>#$.#. )&)*2)/**)!0. ()4 1 '*+ -.*1 -/# 4 -.=
StringString value = "something"
SystemSystem.out.println(value.toUpperCase().toLowerCase() == value);
// prints false
SystemSystem.out.println(value.toUpperCase().toLowerCase().equals(value));
// prints true
)*/'$); == $.0. /*( )*)/ )/ ,0'$/4;/# 24$/$.!*-()4*/# -
+-*"-(($)"')"0" .> === J/#-  ,0'..$").K$./# *+ -/*-!*-$)./)
,0'$/4=
valval value = "something"
println(value.toUpperCase().toLowerCase() == value)
// prints true
println(value.toUpperCase().toLowerCase() === value)
// prints false in Kotlin/JVM and true in Kotlin/JS
J!-*( N*)/ )/))./) ,0'$/4N$)/# '..**&K
 - ; val  7) .1-$' ;)(  value> 2$'' 3+'*- 1-$' .$)"- / - /$'
'/ -$)/# **&>
#/4*0" /./# - .0'/*!*)/ )/ ,0'$/4# &J==K$. / -($) 4*/'$)>
#/4*0" /./# - .0'/*!)$)./)  ,0'$/4# &J===K$. / -($) $/4
*/'$)0/(*./'44/# */'$) )1$-*)( )/>*- 3(+' ;$!4*0-0)/# *1 *
$)/# */'$)G@. '..**&;4*0" /=
BASIC TYPES AND EXPRESSIONS
23
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
true
true
!4*0-0)/#/.( * $)))-*$++*-.*( */# -*/'$)G+-*% /;4*0
" /=
true
false
# $.- +)4#./**2$/#/# 24*/'$)E/-).+$' .F$/.* $)/*1-$+/
)/# 24/#/1-$+/*(+- ../-$)".;.$.*0/'$) $) /#$./&1 -9*2
).2 ->
*-/# 1./(%*-$/4*!4*0-*/'$)2*-&;4*02$'' 0.$)"*)/ )/ ,0'$/4;)*/
$)./)  ,0'$/4;)/# - !*- /# $6 - ) $)#*2./-$)"$)./)  ,0'$/4$.
#)' 2$'')*/6 /4*0>
BASIC TYPES AND EXPRESSIONS
24
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Variables… Whether They Vary or Not
*(+0/ -+-*"-( usually ) ..*( /#$)"(*- /#).$(+' '$/ -'.> ) /*
+ -!*-('0'/$*).;).*( /$( .2 ) /*#*'/#*. - .0'/..*( 2# - >
'..$''4;$)+-*"-(($)";2 ''.0#/#$)".E1-$' .F>)*/'$);/#/2$).0+
 $)"$/*!0-$*0.)( ;.E1-$' F.0"" ./.1'0 /#/)1-4?2#$#$.
)*//-0 $)''*/'$). .>
Declaring Variables
#1 /*#*'*0-/ somewhere;)$).*( . ./#/E.*( 2# - F$.
1-$' >$6 - )/')"0" .#1 $6 - )/24.*! '-$)"1-$' .=
I *4*0) & 42*-;'$& var;*-)*/B
I *4*0) //4+ ;'$& int;*-)*/B
I *4*0) /*+-*1$ )$)$/$'$5 -/* ./'$.#/# 1-$' D.1'0 ;*-)*/B
)*/'$);/# - ,0$- $/ (.- & 42*-Jvar *- valK;/# )( ;)J$)(*./
. .K)$)$/$'$5 -># /4+ (4*-(4)*/ - ,0$- ; + )$)"*)/#
$)$/$'$5 ->
Typeless Declarations
!4*0- $)$/$'$5$)"/# 1-$' .+-/*! '-$)"$/;*/'$)2$''$)! -/# /4+ *!
/# 1-$' >
*- 3(+' =
25
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun main() {
varvar count = 5
println(count::classclass)
}
J!-*( N-$' .N$)/# '..**&K
#$."$1 .0./# .( *0/+0/.2#/2 2*0'" /$!2 +-$)/ 5::class=
I class Int $)/# '..**&*-*/# -*/'$)G )1$-*)( )/.
I class kotlin.Int $))-*$*-*/# -*/'$)G )1$-*)( )/.
 - ;*/'$). ./#/*0-1-$' $)$/$'$5/$*)*  1'0/ ./*) Int;).*$/
 '- ./# 1-$' count . $)"*!/4+ Int>
#$.$.*) *!()424./#/*/'$)/-$ ./*& +/# E - (*)4F*2))& +/#
')"0" *)$. >
Typed Declarations
/$.'.*+*..$' /* '- 1-$' 2$/#/4+ =
varvar count: LongLong = 5
println(count::classclass)
J!-*( N4+ -$' .N$)/# '..**&K
#$..4./#//# 1-$' count $. Long>1 )/#*0"# 5 $.) Int J5L 2*0' /#
Long ,0$1' )/K;*/'$)$.&$)/*4*0)2$''*)1 -//# '$/ -'1'0 /* Long .
+-/*!..$")$)"$//*/# 1-$' >
)()4. .;/# /4+ $.0))  ..-4;.*/'$)) !0'//# 1-$' D./4+ /*
2#/ 1 -/# /4+ $.*!*0-$)$/$'1'0 >0//# - 2$'' /$( .2# )4*02)/
1-$' /*#1 $6 - )//4+ /#)/# $)$/$'1'0 ;)2 2$''. ! 2
3(+' .*!/#/.2 +-* /#-*0"#/# **&>
Declaring Read-Only Variables
'*. '4@- '/ & 42*-/* var $. val>//**) '- 1-$' ? 3 +//#/ val
1-$' .- - @*)'4>!/ -4*0$)$/$'$5 /# (;/# 4))*/ #)" >
VARIABLES… WHETHER THEY VARY OR NOT
26
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*;/#$.2*-&.=
valval count = 5
println(count::classclass)
println(count)
J!-*( N @)'4-$' .N$)/# '..**&K
#$.+-$)/./# /4+ )/# 1'0 ;.0#./#$.'..**&*0/+0/=
class Int
5
*0)(*$!4 var 1'0 =
varvar count = 5
println(count)
count = 7
println(count)
J!-*( N*0)*$!4*-('-$' .N$)/# '..**&K
#$."$1 .0.=
5
7
*2 1 -;/#$.* .)*/=
varvar count = 5
println(count)
count = 7
println(count)
valval readOnly = 5
println(readOnly)
VARIABLES… WHETHER THEY VARY OR NOT
27
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
readOnly = 7
println(readOnly)
J!-*( N*0))*/*$!4 @)'4-$' .N$)/# '..**&K
!4*0/-4-0))$)"$/;4*02$''" /*(+$'  --*-=
e: klassbook.kt: (14, 3): Val cannot be reassigned
#$' 2 )7''1'0 $)/* var 2# ) 1 -2 2)/;2 )*)'47''1'0 $)/*
val *) ;0.0''42# - 2  '- $/>
Prefer val Over var
/(4. (/#/ var $.(*- 0. !0'/#) val>!/ -''; val )*)'4#*'*)
1'0 ;2# - .2 )- ..$")1'0 ./* var .)  >
/4*02$''7)/#//# 1./(%*-$/4*!*/'$)* 0. . val>!)4/#$)"; var / ).
/* *).$ - .E2*-&-*0)F;!*-. .2# - val ))*/ 0. !*-*)
- .*)*-)*/# ->
2$''#*'*6*) /$'  3+')/$*)*!/#$.+#$'*.*+#4;)2 2$''- /0-)/*
/#$.+*$)/ '/ -$)/# **&>*-/# (*( )/;/& $/*)!$/#/#/2 #1 "**
- .*)./*+- ! - val *1 - var;)/#/$.2#4/#$.**&2$'' 0.$)" val (0#
(*- /#) var>
String Interpolation
)4+-*"-(($)"')"0" .*6 -.*( !*-(*!E./-$)"$)/ -+*'/$*)F;2# -
./-$)".)*)/$)+-*"-(($)"')"0"  3+- ..$*).$- /'4$)/# (>*-
3(+' ;4*0)*/#$.$)1-$+/;0.$)"&/$&./* )'*. /# ./-$)"=
letlet count = 5;
console.log(`The value of count is ${count}`);
#$.2$''+-$)//# !*''*2$)"/*/# *).*' =
The value of count is 5
04#..$($'-+$'$/4!*-*0' @,0*/ ./-$)".=
VARIABLES… WHETHER THEY VARY OR NOT
28
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
count = 5
puts "The value of count is #{#{count}}"
*/'$)D../-$)"$)/ -+*'/$*).4)/3$.- ($)$. )/*!1-$+/D.>*2 1 -;!*-
.$(+' 1-$' - ! - ) ;4*0).&$+/# - .)%0./0. $=
valval count = 5
println("The value of count is $count")
J!-*( N/-$)")/ -+*'/$*)N$)/# '..**&K
-$/--4*/'$) 3+- ..$*).) 0. 2$/# ${} .4)/3=
valval count = 3
println("The value of count is not ${count+2}")
J!-*( N/-$)")/ -+*'/$*)2$/#3+- ..$*).N$)/# '..**&K
#$.) 1 -4#)4!*-.. ('$)"./-$)"!-*(()4+$  .;*(+- /*0.$)"
.*( /#$)"'$& StringBuilder $)'..$1>
More Operators
)/# +-  $)"#+/ -;2 .2 (/# (/$' 3+- ..$*). )1-$*0.*+ -/*-.;
.0#. + ) ->#*. *+ -/*-.)2*-&*)'$/ -'1'0 ..2 .2>*/
.0-+-$.$)"'4;/# 4)'.*2*-&2$/#1-$' .=
valval count = 5
valval more = count + 2
println(more)
J!-*( N-$' .)+ -/*-.N$)/# '..**&K
.4*0($"#/ 3+ /;/#$.+-$)/. 7 ./# *0/+0/>
*2 1 -;)*2/#/2 #1 1-$' .;2 #1 (*- *+ -/*-./#/2 )0. A
Increments and Decrements
&$)/*()4*/# -+-*"-(($)"')"0" .;2 )0. ++ ) -- *+ -/*-./*
$)- ( )/) - ( )/1-$' 4S=
VARIABLES… WHETHER THEY VARY OR NOT
29
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun main() {
varvar postIncrement = 5
println("postIncrement ${postIncrement} ${postIncrement++} ${postIncrement}")
varvar preIncrement = 5
println("preIncrement ${preIncrement} ${++preIncrement} ${preIncrement}")
varvar postDecrement = 5
println("postDecrement ${postDecrement} ${postDecrement--} ${postDecrement}")
varvar preDecrement = 5
println("preDecrement ${preDecrement} ${--preDecrement} ${preDecrement}")
}
J!-*( N)- ( )/) - ( )/+ -/*-.N$)/# '..**&K
 - ;2 0. ./-$)"$)/ -+*'/$*)2$/# println() /*.#*2/# 1'0 *!1-$'
 !*- ;E0-$)"F;)!/ -)$)- ( )/*- - ( )/*+ -/$*)>
# - .0'/.- =
postIncrement 5 5 6
preIncrement 5 6 6
postDecrement 5 5 4
preDecrement 5 4 4
*=
I *./@$)- ( )/Jvariable++K$)- ( )/./# 1-$' !/ -/# 0.
I - @$)- ( )/J++variableK$)- ( )/./# 1-$'  !*- /# 0.
I *./@ - ( )/Jvariable--K - ( )/./# 1-$' !/ -/# 0.
I - @ - ( )/J--variableK - ( )/./# 1-$'  !*- /# 0.
!*0-. ;/# . *)'42*-&2$/# var;./# 1'0 *! val $.E$((0/' F)))*/
 #)" >
Augmented Assignments
$($'-'4;/# - - '.**+ -/*-./#/ 1'0/ (/# (/$' 3+- ..$*))
..$")$//*/# var $)*) .#*/;.0#. +==
VARIABLES… WHETHER THEY VARY OR NOT
30
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
varvar count = 5
count += 2
println(count)
J!-*( N0"( )/ ..$")( )/.N$)/# '..**&K
#$.+-$)/. 7;.#*2$)"/#/ count #$/.1'0 $)- ( )/ 4T>#$.$./#
,0$1' )/*!=
varvar count = 5
count = count + 2
println(count)
# - - -=; *=; /=;) %= *+ -/*-..2 '';*($)$)"/#*. (/# (/$'
*+ -/*-.2$/#..$"( )/.>
Unary Operators
*./+-*"-(($)"')"0" .*6 - ! J*-.*(  ,0$1' )/K.E0)-4*+ -/*-F;
2#$#$)1 -/./# 1'0 *! Boolean>*/'$)#./#/;'*)"2$/# - ) "/$*)
*+ -/*-/#/$)1 -/./# .$")*!)0( -=
valval thisIsTrue = truetrue
println(!thisIsTrue)
valval whySoNegative = -5
println(-whySoNegative)
J!-*( N)-4+ -/*-.N$)/# '..**&K
.4*0($"#/ 3+ /;/#$.+-$)/.=
false
5
No Automatic Number Conversions
)()4+-*"-(($)"')"0" .;4*0)*)1 -/)0( -. /2 ).#*-/ -
VARIABLES… WHETHER THEY VARY OR NOT
31
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
- +- . )//$*).J'$& 1 intK)'*)" -- +- . )//$*).J'$& 1 longK%0./1$
..$")( )/.># *(+$' -&)*2./*E0+.$5 F/# 1'0 >
)*/'$);/#*0"#;/#/*)'42*-&.!*-'$/ -'.;)*/1-$' .>
*;!*- 3(+' ;.2 .2 -'$ -;/#$.2*-&.=
varvar count: LongLong = 5
println(count::classclass)
J!-*( N4+ -$' .N$)/# '..**&K
*2 1 -;/#$.* .)*/*(+$' =
valval thisIsInt = 5
valval thisIsLong : LongLong = thisIsInt
println(thisIsLong::classclass)
# *(+$' - --*-2$'' .*( /#$)"&$)/*=
error: type mismatch: inferred type is Int but Long was expected
val thisIsLong : Long = thisIsInt
*/'$)2)/.4*0/*$)/ )/$*)''4+ -!*-(.0#*)1 -.$*).># - $. toLong()
!0)/$*)/#/* ./# /-$&=
valval thisIsInt = 5
valval thisIsLong : LongLong = thisIsInt.toLong()
println(thisIsLong::classclass)
J!-*( N4+ *)1 -.$*).N$)/# '..**&K
Hey, What About ??
!4*0- /#-*0"#*/'$)* ;4*02$''. 1-$' . '- 2$/#,0 ./$*).!*-
/4+ .=
varvar something : BooleanBoolean? = nullnull
println("something was: $something")
VARIABLES… WHETHER THEY VARY OR NOT
32
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
something = truetrue
println("something is now: $something")
*/'$)#.()4! /0- ./#/- )*/. )1 -4*!/ )$)*/# -+-*"-(($)"
')"0" .>) *!/# (*./$(+*-/)/*!/# . ! /0- .$.#*2*/'$)#)' . null
1'0 .> Boolean $./4+ /#/$. $/# - true *- false;2#$' Boolean? $./4+ /#/$.
$/# - true; false;*- null>
2$'' 3+'*- E)0''$'$/4F$)(0#"- / - /$' '/ -$)/# **&>*-)*2;/& $/
*)!$/#/#/2 )..$") null /*1-$' 2#*. /4+  ).$) ?;)2 ))*/
..$") null /*1-$' 2#*. /4+ * .)*/ )$) ?>*;2# - 4*0. ? $)/4+ .;
2/#*0/!*- null 1'0 .>
VARIABLES… WHETHER THEY VARY OR NOT
33
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Functions
.2*0-7-./*/'$)!0)/$*) . 1 -'#+/ -."*=
println("hello, world!")
J!-*( N ''*;*-'AJ)0)/$*)KN$)/# '..**&K
*2;' /D. 3+'*- !0)/$*).$)"- / - /$';./# 4*6 -)0( -*!! /0- ./#/
- )*/,0$/ .*((*)(*)"/# + -. /*!')"0" ./#/2 - *(+-$)"
*/'$)/*J1;04;1-$+/K>
Functions with Parameters
0- main() !0)/$*).#1 #)*+-( / -.>*2 1 -;!0)/$*).)/&
+-( / -.;)(*./!0)/$*)./#/4*02$''2-$/ 2$''2$)0+2$/#*) *-(*-
+-( / -.=
funfun main() {
lessTrivial(1, 1)
}
funfun lessTrivial(left: IntInt, right: IntInt) {
println(left + right)
}
J!-*( N0)/$*).2$/#-( / -.N$)/# '..**&K
)/# !0)/$*) '-/$*);+-( / -$.)( ;!*''*2 4*'*);!*''*2 4
/4+ ># !0)/$*) '-/$*))#1 )*+-( / -.;.$)"' +-( / -;*-
*((@ '$($/ '$./*!+-( / -.>*;# - ;2 #1 /2*+-( / -.;)(  left
) right;/#/- */#*!/4+ Int>
35
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#  !0'/++-*#!*-''$)".0#!0)/$*)$./*.$(+'4+-*1$ 1'0 .!*-/#*.
+-( / -.;$)/# .( *- -./#*. +-( / -.++ -$)/# !0)/$*)
 '-/$*)>#/$.2#/2 - *$)"2$/#*0- lessTrivial(1, 1) ''>*2 1 -;.
2 2$''. '/ -$)/#$.#+/ -;/# - - '/ -)/$1 24.*!''$)"/#$.!0)/$*)>
Functions with Return Types
# !0)/$*)..#*2).*!-- */'$)D. ,0$1' )/*!1( /#*./#/- /0-) void;
$)/#//# 4- )*/- /0-)$)")41'0 ./*/# '' ->
J/ #)$''4;/# 4- - /0-)$)" Unit;2#$#2 2$'' 3+'*- '/ -$)/# **&K
*2 1 -;.2$/#!0)/$*).)( /#*.$)(*./+-*"-(($)"')"0" .;*/'$)
!0)/$*).)- /0-)1'0 .;0.$)"/# return & 42*-=
funfun main() {
println(simpleReturn(1, 1))
}
funfun simpleReturn(left: IntInt, right: IntInt) : IntInt {
returnreturn left + right
}
J!-*( N0)/$*).2$/# /0-)4+ .N$)/# '..**&K
*2 1 -;/* ' /*- /0-)1'0 ;/# !0)/$*)) ./* '- /# /4+ *!2#/
$. $)"- /0-) >)()4')"0" .;.0#.1;/#/E- /0-)/4+ F$. '- /
/#  "$))$)"*!/# !0)/$*) '-/$*)>)*/'$);$/$. '- //#  );!/ -
/# '*.$)"+- )/# .$.;. +-/ 4*'*)>*;# - ;2 - - /0-)$)") Int;4
1$-/0 *!/#/ : Int !/ -/# fun simpleReturn(left: Int, right: Int) +-/>
* /#/''./# !0)/$*)/# ))0. /#/- /0-) 1'0 >)/# *1 *
.)$++ /;2 - +-$)/$)"$//*./)-*0/+0/>*0*0''.*./*- $/$)1-$' =
funfun main() {
valval result = simpleReturn(1, 1)
println(result)
}
funfun simpleReturn(left: IntInt, right: IntInt) : IntInt {
returnreturn left + right
}
FUNCTIONS
36
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
J!-*( N..$")$)"0)/$*) .0'/.N$)/# '..**&K
Local Variables
-$' . '- $).$ *!!0)/$*)- *).$ - /* '*'1-$' .># 4-
1$'' $).$ *!/# !0)/$*)0/)*/*0/.$ *!$/>)/#*. 1-$' ."*E*0/*!
.*+ F*) /# !0)/$*)- /0-).>
*;2 *0'- 2*-&*0-!0)/$*)/*0. '*'1-$' /*#*'*)/*/# '0'/$*)=
funfun main() {
println(actLocally(1, 1))
}
funfun actLocally(left: IntInt, right: IntInt) : IntInt {
valval result = left + right
returnreturn result
}
J!-*( N*'-$' .N$)/# '..**&K
Fancier Functions
#*. .$.-  )*0"#/*''*24*0/*2-$/ */'$)* >*2 1 -;/# - - ! 2
$/$*)'! /0- ./#/- !$-'4+- 1' )/$)*/'$)+-*"-(($)">*2(0#4*0
0. /# . ! /0- .$.0+/*4*0;0/4*02$'' )*0)/ -/# (!- ,0 )/'4).*$/$.
$(+*-/)//*0) -./)#*2/# 42*-&>
Expression Bodies
- ,0 )/'4;*0-!0)/$*)*$ .- '$) .*!* 2-++ $)- .;..#*2)*1 >
*( /$( .;4*02$'' )*0)/ -!0)/$*)./#/-  '- 0.$)") = $)./ =
funfun main() {
println(expressionBody(1, 1))
}
funfun expressionBody(left: IntInt, right: IntInt) = left + right
J!-*( N3+- ..$*)*$ .N$)/# '..**&K
FUNCTIONS
37
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#$.$.'' E 3+- ..$*)*4F.4)/3>
/$. .$") /*.$(+'$!4. .2# - /#  )/$- ( /#*$(+' ( )//$*)$..$)"'
3+- ..$*);.0#.$)"/2*)0( -./*" /# -> - ;2 - +' +$-*!- .
) return & 42*-2$/#) =;2#$#(& ./#$.* ' ..1 -*. >
Expression Bodies and Types
)*/# -/#$)"/#/2  '$($)/ $)/# *1  3(+' $./# - /0-)/4+ > $
)*/) /* '- /#/ expressionBody() - /0-).) Int;./# *(+$' -)
 / -($) /#/*)$/.*2)>/&)*2.2#//# /4+ *! left $.;)$/&)*2.2#//#
/4+ *! right $.>/ 1 )&)*2.2#//# /4+ *! left + right $.>*;$/&)*2./#
/4+ *! expressionBody() .- .0'/>
*2 1 -;.*( /$( .;4*02$'') /**1 --$ /#//4+ >*- 3(+' ;/# - 2$''
. .2# - /#  3+- ..$*) 1'0/ ./**) /4+ ;0/4*02)//# !0)/$*)/*
- /0-).0+ -/4+ >
)/#*. . .;4*0%0./&/# : .4)/3!*- '-$)"/# - /0-)/4+ =
funfun main() {
println(expressionBody(1, 1))
}
funfun expressionBody(left: IntInt, right: IntInt) : NumberNumber = left + right
J!-*( N3+- ..$*)*$ .) /0-)4+ .N$)/# '..**&K
 - ;2  '- expressionBody() /*- /0-) Number>.$//0-).*0/; Int $)# -$/.
!-*( Number>
#$.+-/$0'- 3(+' $..$''4>*2 1 -;*) 2 ./-/" //$)"$)/* '.. . )
$)# -$/) ;/#$.+$'$/4 *( .(*- $(+*-/)/>
Why Bother?
)/#  );/#$.(4)*/. ('$& E$"2$)F;!*-/2*- .*).=
S> - *)'4.1$)"! 2#-/ -.
T> *//#/()4!0)/$*).2*0'++ -/* .$(+'  )*0"#/#//# $- )/$-
$(+' ( )//$*)) .$)"'  3+- ..$*)
FUNCTIONS
38
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
# 7-./-"0( )/$./-0 =2 - )*/.1$)"(0#*)+ -@!0)/$*).$.>*2 1 -;
/# . .$(+'$7/$*).0+*1 -/$( >'*/*!!*0.$)*/'$)$.*)*6 -$)"/# .
.(''.$(+'$7/$*).;$)'*/*!+' .;2#$#*($) /*(& */'$)* (0#
(*- *)$. /#)/#  ,0$1' )/* $)1)*/# -')"0" .>
)/ -(.*!/# . *)-"0( )/?$//0-).*0//#/'*/(*- /#$)".$)*/'$))
 2-$// )$)/ -(.*! 3+- ..$*)./#)4*0($"#//#$)&> 2$''. (*- *0/
/#/ $))0+*($)"#+/ ->
Default Parameter Values
*( ')"0" .*6 - !0'/+-( / -1'0 .;.0#.1-$+/=
functionfunction increment(base, amount = 1) {
// something yummy
}
?)04=
defdef increment(base, amount = 1)
# something yummy
endend
*/'$)'.**6 -. !0'/+-( / -1'0 .=
funfun main() {
println(increment(1))
}
funfun increment(base: IntInt, amount: IntInt = 1) = base + amount
J!-*( N !0'/-( / -'0 .N$)/# '..**&K
 - ;2 )'' increment() 2$/# $/# -*) *-/2*1'0 .>!2 *)'4.0++'4*)
1'0 ;/#  !0'/1'0 *! 1 2$'' 0. !*- amount>!2 .0++'4/2*1'0 .;/# )
/# '' -*)/-*'.*/# base ) amount>
#  ,0$1' )/1* - ,0$- ./2*( /#*.=
int increment(int base, int amount) {
returnreturn base + amount;
}
FUNCTIONS
39
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
int increment(int base) {
returnreturn increment(base, 1);
}
Named Parameters
0)/$*).2$/#'*/.*! !0'/1'0 .- *!/ )'' 0.$)")( +-( / -.>)
/#$.!*-(*!'';-/# -/#)$ )/$!4$)"+-( / -.4/# *- -$)2#$#/# 4
++ -$)/# '';4*0 3+'$$/'4.// /# )( *!/# +-( / -$)/# ''$/. '!>
*0) 1 )E($3)(/#F+*.$/$*)'+-( / -.))( +-( / -.=
funfun main() {
println(increment(base = 1))
println(increment(amount = 10, base = 1))
println(increment(1, amount = 10))
}
funfun increment(base: IntInt, amount: IntInt = 1) = base + amount
J!-*( N( -( / -'0 .N$)/# '..**&K
 - ;$)/# 7-.//2* println() ''.;2 .+ $7''4.// */#/# )( )/#
1'0 *!/# +-( / -.$)/# !0)/$*)''>)/# /#$- println() '';/# 7-./
+-( / -$./-$/$*)'E+*.$/$*)'F+-( / -;2#$' /# */# -*) $.)( >
)/#$.+-/$0'- 3(+' ;2 - )*/"$)$)"(0#!-*(/#$..4)/3>*2 1 -;
*(+' 3!0)/$*).2$/#'*/.*! !0'/+-( / -1'0 .)"$)'*/(*- !-*(
/#$.>0++*. /#/2 #!0)/$*)/#/'**& '$& /#$.=
funfun iCanHazCookie(name: StringString,
value: StringString,
maxAge: LongLong? = nullnull,
expires: LocalDateTimeLocalDateTime? = nullnull,
domain: StringString? = nullnull,
path: StringString? = nullnull,
secure: BooleanBoolean = falsefalse,
httpOnly: BooleanBoolean = falsefalse) : StringString {
// code to generate an HTTP Set-cookie header goes here
}
J.- ($) -;/4+ .2$/# ? $)$/ +-( / -./#/.0++*-/ null 1'0 .H2
2$'' 3+'*- /#$.(*- '/ -$)/# **&K
 - ;.$3*!/# +-( / -.#1  !0'/1'0 . 7) ;).*/# 4- *+/$*)'
FUNCTIONS
40
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
2# )2 '' iCanHazCookie()>
0++*. /#/2)//*- / **&$ 2$/#+-/$0'-+/#;0/(2$''$)"/*
 +//#  !0'/.!*-/# - ($)$)"71 *+/$*)'+-( / -.>!2 2 - ./0&2$/#
+*.$/$*)'+-( / -.;2 2*0'#1 /*''$/'$& /#$.=
valval cookieHeader = iCanHazCookie("foo", "bar", nullnull, nullnull, nullnull, "/")
) 6 /;/# 7-.//#- *+/$*)'+-( / -.- )*'*)" -*+/$*)'; 0. 2 ) 
/# (.+' #*' -.>
$/#)( +-( / -.;/#$. *( ..$(+' -=
valval cookieHeader = iCanHazCookie(name = "foo", value = "bar", path = "/")
?*- 1 )=
valval cookieHeader = iCanHazCookie("foo", "bar", path = "/")
)(*-  .$'4.&$+*1 -/# +-( / -./#/2 2$.#/*$")*- ) +//#
 !0'/1'0 .; 0. 2 ))( /# +-( / -./#/2 are .0++'4$)";-/# -
/#)- '4$)"*)+*.$/$*)./*$ )/$!4/# (>
-$/$*)''4;)( +-( / -.#/*++ -!/ -''+*.$/$*)'+-( / -.> */'$)
S>V- '3 /#/- ./-$/$*).*( 2#/>*0)0. )( .!*-)4+-( / -.;0/0+
/#-*0"#/# './+*.$/$*)'+-( / -;/# +*.$/$*).J)*//# )( .K- 2#/
(// ->
FUNCTIONS
41
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Collections and Lambdas
!/ )$)+-*"-(($)";2 ) /* '2$/#*'' /$*).*!./06>!4*0#1 2*-& 
2$/#)4(%*-+-*"-(($)"')"0" ;4*02$''#1  '/2$/#*'' /$*)/4+ .;
.0#.=
I --4.*-'$./.
I . /.J*'' /$*).*!$./$)/0) ,0'*% /.K
I (+.J*'' /$*).*!& 4G1'0 +$-.K
*/'$)#./# . .2 ''> -)$)"#*2/*0. /# ()*/*)'4$)1*'1 . '$)"2$/#
/# /0'/4+ .;0/'.*2$/##*2/*+ -!*-(*((*)*+ -/$*).*)/# (;.0#.
$/ -/$)"*1 -/# $-*)/ )/.>
Major Collection Types and Creation Functions
*/'$)#./# !*- ( )/$*) -*./ -*!*'' /$*)/4+ .;)/# 4!*-(/# (*./
*((*)*'' /$*)/4+ ./#/4*02$''0. >
*/'$)G'.*#. ../**/# -./)-1*'' /$*)/4+ .;'$&
LinkedHashMap;2#$#2 2$''$.0.. '/ -$)/#$.#+/ ->*2 1 -;0.0''44*02$''
0. /#*. *)'4$!/# - $.)*)/$1 */'$) ,0$1' )/>
Arrays
*/'$)#.) Array '../#/$.)'*"*0./*1--4.>!4*00. */'$)--4.;
)4*0- -0))$)"*)*/'$)G;4*0-*/'$)--4.2$'' (++ /*1--4.>
1 -'';--4.- )*/,0$/ .+*+0'-$)*/'$).- '$./.>*2 1 -;4*0-
2 '*( /*0. /# (;)/# 4(4 )  ..-4$!4*0-* $."*$)"/*
43
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
$)/ -*+ -/ 2$/#1* >
*- / )--4;/# .$(+' ./++-*#$./*0. /# arrayOf() 0/$'$/4!0)/$*)=
valval things = arrayOf("foo", "bar", "goo")
println(things::classclass)
println(things)
J!-*( N--4.N$)/# '..**&K
0))$)"/#$.$)/# '..**&"$1 .0.=
class Array
foo,bar,goo
Typed Arrays
*/'$)'.*#. $/ '.. .!*---4.*!+-$($/$1 /4+ .=
I BooleanArray
I ByteArray
I CharArray
I DoubleArray
I FloatArray
I IntArray
I LongArray
I ShortArray
# 4#1 *-- .+*)$)"0/$'$/4!0)/$*)./*- / $)./) .;.0#. intArrayOf()=
valval things = intArrayOf(1, 3, 3, 7)
println(things::classclass)
println(things)
J!-*( N4+ --4.N$)/# '..**&K
class IntArray
1,3,3,7
.2$/# Array;/# . /4+ @.+ $7--4.- )*/ .+ $''4+*+0'-; 3 +/2# -
4*0($"#/) /# (!*-2*-&$)"2$/#1* /#/ 3+ /./#*. +-$($/$1 --4
/4+ .>
COLLECTIONS AND LAMBDAS
44
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Lists
# .$*) @$( ).$*)'*'' /$*)/4+ $)*/'$)$./# '$./>#$.$.)'*"*0./*
1 ArrayList>
*0)- / $)./) .*!'$./1$ listOf();(0#'$& 4*0- / --4.2$/#
arrayOf() )/# */# -.( )/$*) *1 =
valval things = listOf("foo", "bar", "goo")
println(things::classclass)
println(things)
J!-*( N$./.N$)/# '..**&K
#)$''4; List $.) $)/ -! ># *)- / $(+' ( )//$*)/#/2 . 2$'' )
ArrayList=
class ArrayList
[foo, bar, goo]
)0)0.0'.+ /*!'$./.$./#/ listOf() - / .)E$((0/' F'$./;*) 2# - 2
))*/- +' $/ (.$)/# '$./> 2$'' 3+'*- $((0/$'$/4(*- '/ -$)/#$.
#+/ ->
Sets
)4+-*"-(($)"')"0" .*6 -E. /F*'' /$*)/4+ ;2#$#$.*) @
$( ).$*)'*'' /$*)$)2#$#*)'4$./$)/*% /.2$'' $)'0 >*- 3(+' ;
$!4*0/-4$)"/# !*0-)0( -. 1, 3, 3, 7 /*'$./;4*02*0'#1 !*0- )/-$ .;
0/$)"/# (/*. /- .0'/.$)/#- @ ' ( )/. /#*'$)" 1, 3, 7>#
0+'$/ 3 1'0 $.$")*- >
*/'$)#.7-./@'...0++*-/!*-. /.;- / 0.$)" setOf()=
valval things = setOf(1, 3, 3, 7)
println(things::classclass)
println(things)
println(things.size)
J!-*( N /.N$)/# '..**&K
.2$/# List; Set $.)$)/ -! ># *)- / $(+' ( )//$*)/#/4*02$''" /
COLLECTIONS AND LAMBDAS
45
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
!-*( setOf() $. LinkedHashSet=
class LinkedHashSet
[1, 3, 7]
3
(*)"*/# -./06;/#$.* .)$++ /+-$)/.*0//# size *! things> size $.
+-*+ -/4*! Set J)*! List;!*-/#/(// -K;+-*1$$)"/# )0( -*! ' ( )/.$)
/# *'' /$*)> - 2 . /#//# 0+'$/ 3 1'0 .2 - - +' 4.$)"' 3 $)
/# Set>
);.2$/#'$./.; setOf() - / .)$((0/' Set;2#$#2 2$''*1 - '/ -$)
/#$.#+/ ->
Maps
*./+-*"-(($)"')"0" .#1 .*( .0++*-/!*-E(+F*-E$/$*)-4F/4+ ;
- +- . )/$)"& 4@1'0 ./*- >*0)/# )+' /$)/*/# *'' /$*).& 4@
1'0 +$-.;/* ' /* 1 )/0''4- /-$ 1 1'0 ."$1 )/# $-& 4.>)*/'$);2
#1  Map /4+ /#/7''./#$.-*' >
*- / #-@* (+H/# 242 #1 - / #-@* '$./.). /.H
4*0)0. /# mapOf() 0/$'$/4!0)/$*)>#$./& .*((@ '$($/ '$./*!& 4@
1'0 +$-.;2# - /# & 4)1'0 - . +-/ 4/# to & 42*-=
valval things = mapOf("key" to "value", "other-key" to "other-value")
println(things::classclass)
println(things)
println(things.size)
J!-*( N+.N$)/# '..**&K
!*0-. ;4*0)*($) /# . *)./-0/.;.0#.#1$)"(+*!'$./.=
valval things = mapOf("odd" to listOf(1, 3, 5, 7, 9), "even" to listOf(2, 4, 6, 8))
println(things::classclass)
println(things)
println(things.size)
J!-*( N*($)$)"*'' /$*).N$)/# '..**&K
J$)/-0/#; to $./0''4!0)/$*);' 1 -"$)".*( .+ $'.4)/3/#/2 2$''.
(0#'/ -$)/# **&K
COLLECTIONS AND LAMBDAS
46
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Basic Usage
- /$)"*'' /$*).$.)$ ;0/ 1 )/0''4;2 ) /*" //$))*0/*!/# (
)*/# -2$. ()$+0'/ /# (>
[][] Syntax
*- /-$ 1 1'0 .!-*(*'' /$*)J*/# -/#) SetK;4*0)0. []>*---4.)
'$./.;/# 1'0 +.. $)/*/# [] *+ -/*-$./# 0@. $) 3$)/*/# *'' /$*)>
*-(+.;/# 1'0 +.. $)/*/# [] *+ -/*-$./# & 4!*-2#$#4*02$.#/*'**&
0+/# *-- .+*)$)"1'0 >
valval thingsArray = arrayOf("foo", "bar", "goo")
println(thingsArray[0])
valval thingsIntArray = intArrayOf(1, 3, 3, 7)
println(thingsIntArray[1])
valval thingsList = listOf("foo", "bar", "goo")
println(thingsList[2])
valval thingsMap = mapOf("key" to "value", "other-key" to "other-value")
println(thingsMap["other-key"])
J!-*( N ..$)"' ( )/.1$LMN$)/# '..**&K
0))$)"/#$.$)/# '..**&2$''"$1 4*0=
foo
3
goo
other-value
#$)"." /$/$)/ - ./$)"$!2 ./-/+'4$)"2$/#/#/(+*!/# '$./.*!*)
1 ).$)"' @$"$/$)/ " -.=
valval oddEven = mapOf("odd" to listOf(1, 3, 5, 7, 9), "even" to listOf(2, 4, 6, 8))
println(oddEven["odd"].size)
COLLECTIONS AND LAMBDAS
47
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#$.2$''!$'/*-0);2$/#*(+$'  --*-=
error: only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable
receiver of type List<Int>?
Map (4- /0-) null !*-"$1 )& 4;./# - (4)*/ 1'0 !*-/#/& 4>.$/
/0-).*0/;*0- Map #.1'0 !*-/# odd & 4;).*/-0)/$( ;/#$.2*0'.0 >
)$)(*./+-*"-(($)"')"0" .;2 *0'/0''4*(+$' )-0)/#$..*-/*!
*+ -/$*)>#$.!$'.$)*/'$); 0. *!#*2*/'$)#)' ./# +*..$$'$/4*! null
.1'0 > 2$'' 3+'*- /#$.$)(0#"- / - /$' '/ -$)/# **&>
Typical Stuff
!4*0- 0. /*2*-&$)"2$/#*'' /$*).$)*/# -+-*"-(($)"')"0" .;$/2$''
*( .'$//' .0-+-$. /#/*/'$)#.'*/*!/# .( .$!0)/$*).!*-2*-&$)"
2$/#$/.*'' /$*).>
)$/$*)/*/# size +-*+ -/4;4*02$''#1 /#$)".'$& =
I isEmpty();!*-,0$& Boolean 1'0 . *)2# /# -/# *'' /$*)$.
(+/4
I contains();2#$#2$''- /0-) true $!/# *'' /$*)*)/$)./# - ,0 ./ 
$/ (J!*-(+.;/#$.# &./# & 4.H0. containsValue() /*# &/#
1'0 .K
*-'$./.)--4.;4*0#1 /#$)".'$& =
I first();/*" //# 7-./ ' ( )/
I last();/*" //# './ ' ( )/
I firstOrNull();/*" //# 7-./ ' ( )/*- null $!/# *'' /$*)$. (+/4
I ).**)
Conversion Functions
# *) @$( ).$*)'*'' /$*)/4+ .J--4.;'$./.;. /.K#1 -$#. /*!
*)1 -.$*)!0)/$*)./*($"-/  /2 )/4+ .=
I Array *6 -. toList() ) toSet()
I List *6 -. toSet() ) toTypedArray()
I Set *6 -. toList() ) toTypedArray()
COLLECTIONS AND LAMBDAS
48
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Immutability and Collections
Array *% /./#/2 - / 2$/# arrayOf() - (0/' >*/*)'4)2 0. []
.4)/3/*- /-$ 1 1'0 . *)$/.$) 3;0/2 )0. []= .4)/3/*- +' 
1'0 . *)$/.$) 3=
valval thingsArray = arrayOf("foo", "bar", "goo")
thingsArray[1] = "something completely different"
println(thingsArray[1])
J!-*( N--4*$7/$*)N$)/# '..**&K
*2 1 -;/#$.!$'.$!4*0/-4$/2$/# List - / 0.$)" listOf()=
valval thingsList = listOf("foo", "bar", "goo")
thingsList[1] = "something completely different"
println(thingsList[1])
*0" /.*( 1 -4./-)" .4)/3 --*-( .." .=
e: klassbook.kt: (4, 1): Unresolved reference. None of the following candidates is
applicable because of receiver type mismatch:
@InlineOnly public inline operator fun MutableMap.set(key: Int, value: String): Unit
defined in kotlin.collections
e: klassbook.kt: (4, 11): No set method providing array access
#  --*-$.-$.  0. listOf() - /0-). List;2#$#* .)*/#1 .0++*-/!*-
/# []= *+ -/*-> List $.$((0/' =4*0))*/- +' $/.( ( -.2$/#*/# -
*% /.>
!4*0) '$./2# - 4*0)- +' $/.( ( -.;4*0)0. mutableListOf()=
valval thingsList = mutableListOf("foo", "bar", "goo")
thingsList[1] = "something completely different"
println(thingsList[1])
J!-*( N0/' $./.N$)/# '..**&K
 - ; thingsList $./0''4 MutableList;2#$#$..0@/4+ *! List /#/
COLLECTIONS AND LAMBDAS
49
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
.0++*-/. []=>
# .( $./$)/$*)#*'./-0 !*- Map> Map $.$((0/' ;0/ MutableMap
- / 1$ mutableMapOf() ) (*$7 >
2$'' 3+'*- (*- *0/*/'$)D.+#$'*.*+#4/*2-.$((0/$'$/4 $)'/ -
#+/ ->)" ) -';$(/*0. $((0/' '$./.)(+.;0)' ..4*0#1 1 -4
.+ $7- .*)/***/# -2$. >
Introducing Lambda Expressions
) /#$)"/#/4*02$''0.  lot $)*/'$) 1 '*+( )/$./# '( 3+- ..$*)>/
$.+-/$0'-'4$(+*-/)/2# )2*-&$)"2$/#*'' /$*).;.'( 3+- ..$*).-
-$/$'!*-+ -!*-($)"*((*)*+ -/$*).;.0#.=
I **+$)"*1 -/#  ' ( )/.
I $)$)"*-7'/ -$)" ' ( )/./#/(/# -/$)-$/ -$
I -*1$$)"*(+-$.*)'*"$!*-.*-/$)" ' ( )/.
I ).**)
What is a Lambda Expression?
.$(+' 24/*./-//#$)&$)"*0/'( 3+- ..$*).$./**).$ -/# (%0./.
. /*!*/'$).// ( )/.2-++ $)0-'4- .=
valval lambda = { println("Hello, world!") }
*2 1 -;./# val $)$/ .;'( 3+- ..$*)$/. '!$.)*% />*0)#*'
*)/*/# ($)1-$' .;+../# (.!0)/$*)+-( / -.;)* 1 -4/#$)" '.
/#/4*0($"#/*2$/#*/# -/4+ .*!*% /.>
*/'$)'( 3+- ..$*).'*. '4- . (' /# $-1*0)/ -+-/.;'*)"2$/#
.$($'-*)./-0/.$)*/# -')"0" .;.0#.04'*&.>
!4*0-  $1 '( 3+- ..$*);)4*02)//*-0)$/;'' invoke()=
valval lambda = { println("Hello, world!") }
lambda.invoke()
J!-*( N(3+- ..$*).N$)/# '..**&K
COLLECTIONS AND LAMBDAS
50
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#$.+-$)/.E ''*;2*-'AF;%0./.$!4*0#%0./$- /'4 3 0/ /# println()
''>
Where Do We Use Lambda Expressions?
/($)$(0(;'( 3+- ..$*).- +' '*/*!/# E'$./ ) -F*-E''&F+// -).
/#/4*0($"#/#1 0. $)1>*- 3(+' ;-/# -/#)- /$)")
$(+' ( )//$*)*! Comparator /*.0++*-/ sort() ( /#*;.2 *$)1;4*0
2*0'0. '( 3+- ..$*)>
)+-/$0'-;)4/#$)"$)12# - 4*0($"#/#1 0. '$./ ) -*-''&
*).$./$)"*!'..2$/#.$)"' ( /#*;'( 3+- ..$*)2$'' /# '$& '4
- +' ( )/>
*/'$)'.*' ).$/. '!/*2-./# !0)/$*)'+-*"-(($)" +-$"(;2# -
0.$) ..'*"$$.( 0+*!'$)& !0)/$*)''.> 2$'' 3+'*- /#/(*- 2# )
2 '**&/ $((0/$'$/4>
0/;$))0/.# ''=4*02$''0. '( 3+- ..$*). lot $)*/'$)>
Lambda Expressions, Parameters, and Return Values
$& !0)/$*).;'( 3+- ..$*).)/& +-( / -.=
valval squarifier = { x: IntInt -> x * x }
println(squarifier.invoke(3))
J!-*( N(3+- ..$*).)-( / -.N$)/# '..**&K
 - ; squarifier /& .*) +-( / -;)(  x;2#*. /4+ $. Int># x: Int
.4)/3$./# .( .!0)/$*)+-( / -.># '$./*!+-( / -./*'(
3+- ..$*)*( //#  "$))$)";2$/#/# E*4F*!/# '( 3+- ..$*)
!*''*2$)"/# ->>
*2 1 -;.$)"' @+-( / -'(.- 1 -4*((*)>*-/#/;*/'$)*6 -.
.#*-/#).4)/3= it>!/# */'$)*(+$' -) / -($) /# //4+ !*-/#
+-( / -;4*0).&$+/# +-( / - '-/$*))%0./- ! -/*/# +-( / -.
it> 2$''.  3(+' .*!/#$.*($)"0+.#*-/'4>
'( 3+- ..$*)2$''- /0-)2#/ 1 -$/.'./.// ( )/* .>)/# . *!
squarifier;$/#.*)'4*) .// ( )/= x * x>*;/#/1'0 $.2#//# '(
COLLECTIONS AND LAMBDAS
51
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
3+- ..$*)- /0-).!-*(/# ''/* invoke()>*;*0-*).*' *0/+0/$. 9;
 0. /# '( 3+- ..$*)/& .U.$)+0/;.,0- .$/;)- /0-)./#/.,0- 
1'0 >
Common Collection Operations
$/#'( 3+- ..$*).$)($);' /D.'**&/.*( !$-'4*((*)!0)/$*).
1$'' *)*0-*'' /$*)'.. .;(*./*!2#$#0. '( 3+- ..$*)>
Iteration
*((*)/#$)"/**2$/#*'' /$*)$./*$/ -/ *1 -$/.*)/ )/.># - - /2*
24./**/#/$)*/'$)= forEach() ) for>
forEach()forEach()
# (*- *((*)++-*#$./*0. /# forEach() !0)/$*)=
valval things = listOf("foo", "bar", "goo")
things.forEach { println(it) }
J!-*( N!*-#JKN$)/# '..**&K
#$.- .0'/.$)=
foo
bar
goo
)*/# -2*-.; forEach() $/ -/ .*1 - #( ( -*!/# *'' /$*))$)1*& .
/# '( 3+- ..$*)!*- #*) $)/0-)>0-'( 3+- ..$*)#++ )./*+-$)/
*0//# 1'0 *!/# '( 3+- ..$*)D.+-( / ->)*0-. ;2 0. /#
.#*-/#) it - ! - ) /*/#/+-( / ->'/ -)/$1 '4;2 *0'#1 "$1 )$/
0./*()( =
valval things = listOf("foo", "bar", "goo")
things.forEach { thing -> println(thing) }
forEach() $.!0)/$*);1$'' !*- List )/# */# -*'' /$*)/4+ .>/(4)*/
look '$& !0)/$*);"$1 )/#/$/'&.+- )/# . .!/ -/# forEach )( >
COLLECTIONS AND LAMBDAS
52
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
forEach() /& .'( 3+- ..$*).+-( / ->/(4)*/ look '$& 
+-( / -;"$)0 /*/# '&*!+- )/# . .>
)/#$.. ;/#$.$.$/*!.$(+'$7 */'$).4)/3>!0)/$*)/#//& .*)
'( 3+- ..$*)+-( / -) '' 2$/#*0//# +- )/# . .;$!4*0-
+-*1$$)"'$/ -''( 3+- ..$*)>*;/# */'$).#*2)*1 $. ,0$1' )//*=
valval things = listOf("foo", "bar", "goo")
things.forEach( { println(it) } )
J!-*( N!*-#JK$.0)/$*)N$)/# '..**&K
#/'**&.(*- '$& *)1 )/$*)'!0)/$*)'';' $/*) /#//& .'(
3+- ..$*).+-( / ->*2 1 -;4*02$''-- '4. .0#''.2-$// )/#/24H
/# .4)/3/#/.&$+./# 0))  ..-4+- )/# . .$.!-(*- *((*);$!4*0-
0.$)"'$/ -''( 3+- ..$*)>
# '( 3+- ..$*)/& ..$)"' +-( / -># /4+ *!/# +-( / -$.
 / -($) 4/#  7)$/$*)*! forEach() H%0./.!0)/$*)) '- 
+-( / -. $)") Int *- String;$/) '- +-( / -. $)"'(
3+- ..$*)/#/$/. '!/& . -/$)+-( / -> 2$''. #*2/* '- .0#
!0)/$*). '/ -$)/# **&>*-)*2;/& $/*)!$/#/#/*/'$))7"0- *0/2#/
/# //4+ *!/# +-( / -/*/# '( 3+- ..$*)$.?.*2 *)*/) /*
 '- /#/+-( / -()0''4*0-. '1 .>)./ ;2 0. it /*- ! -/*/#/
+-( / -;$)/#$.. +..$)"$//* println()>
Many !0)/$*).*) List; Set; />2*-&/#$.24=/# 4/& .$)"' '(
3+- ..$*).+-( / -;2# - /# '( 3+- ..$*)$/. '!/& ..$)"'
+-( / -;)2 2-$/ /# ''2$/#*0/+- )/# . .)40.$)" it /*- ! -/*/#
'( 3+- ..$*)+-( / ->
Good Old-Fashioned forfor
forEach() $.)*//# *)'424/*$/ -/ *1 -/# ( ( -.*!*'' /$*)># - $.
'.* for;2*-&$)"1 -4.$($'-'4/* for '**+.$)*/# -+-*"-(($)"')"0" .=
valval things = listOf("foo", "bar", "goo")
forfor (thing inin things) {
println(thing)
}
COLLECTIONS AND LAMBDAS
53
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
J!-*( N!*-**+N$)/# '..**&K
# +- )/# /$' 3+- ..$*)!/ -/# for & 42*-*).$./.*!)( /*0. !*-)
$)$1$0'( ( -JthingK)/# *'' /$*)JthingsK;. +-/ 4/# in
& 42*-># '*&*!* /#/!*''*2.)/# )- ! -/*)$)$1$0'( ( -4
2#/ 1 -)( 4*0"1 $/;)/#/'*&2$''  3 0/ *) !*- #( ( -$)
. ,0 ) >
J/ #)$''4;/#$.'*&*!* $.)*/'( 3+- ..$*);0/$/$.!$-'4.$($'-K
forEach() $..*( 2#/(*- +*+0'-;.$/) 0. $)#$)  3+- ..$*).;.
2 2$''. $)0+*($)". /$*).>*2 1 -;4*0)0. 2#$# 1 -*!/# ((& .
4*0(*- *(!*-/' >
filter()
filter() ++'$ .7'/ -$)"'( 3+- ..$*)/**'' /$*);- /0-)$)" new
*'' /$*)/#/*)/$)./# .0. /*!$/ (.!-*(/# *-$"$)'*'' /$*)>!/#
'( 3+- ..$*)- /0-). true !*-)$/ (;/#/$/ ($.$)'0 $)/# - .0'/
*'' /$*);*/# -2$. $/$.)*/>
valval things = listOf("foo", "bar", "goo")
things.filter { it[1]=='o' }.forEach { println(it) }
J!-*( N7'/ -JKN$)/# '..**&K
 - 2 #1 #$)  3+- ..$*)> filter() /# things *'' /$*);/# )''
forEach() *)/# *0/+0/*! filter()>#$' /#$.) 2-$// )*)*) '$) ;4*02$''
. $/(*- *!/ )2-$// )2$/# # 3+- ..$*)./-/$)"*)$/.*2)'$) =
valval things = listOf("foo", "bar", "goo")
things
.filter { it[1]=='o' }
.forEach { println(it) }
filter() /& .'( 3+- ..$*)/#/ +/.)$/ (!-*(/# *'' /$*))
- /0-). Boolean;2# - true ( )./#//# $/ (.#*0'  +/ )+.. 
'*)";2#$' false ( )./#//# $/ (.#*0' - % / >*;-0))$)"/#$.*
.)$++ /$)/# '..**&4$ '.=
COLLECTIONS AND LAMBDAS
54
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
foo
goo
?. bar * .)*/#1 ) o ./# . *)#-/ ->
map()
filter() +.. .'*)"./-$/.0. /*!/# $/ (.$)/# *'' /$*);0//# $/ (.
/# (. '1 .- 0)#)" >
map();4*)/-./;+.. .'*)" #$/ (!-*(/# *'' /$*);/-).!*-( 4
'( 3+- ..$*)/#/4*0.0++'4=
valval things = listOf("foo", "bar", "goo")
things
.map { it.toUpperCase() }
.forEach { println(it) }
J!-*( N(+JKN$)/# '..**&K
 - ;2 map() String /*)*/# - String;*)1 -/$)"$//*0++ -. =
FOO
BAR
GOO
# - $.)*- ,0$- ( )/!*- map() /*- /0-)/# .( /4+ .$/./-/.2$/#;/#*0"#=
valval oneAndPrimes = listOf(1, 2, 3, 5, 7)
oneAndPrimes
.map { 1.0 / it }
.forEach { println(it) }
J!-*( N(+JK4+ *)1 -.$*).N$)/# '..**&K
#$.* .)$++ /$/ -/ .*1 -.$)"' @$"$/+-$( 1'0 .)+-$)/./# $-- $+-*'JS
$1$ 4/# )0( -K> - ;2 0. 1.0 /*!*- 9*/$)"@+*$)/- +- . )//$*);
).*2 " / Double - .0'/.=
COLLECTIONS AND LAMBDAS
55
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
1.0
0.5
0.3333333333333333
0.2
0.14285714285714285
# - - *0)/' ..(*- *!/# . .*-/.*!!0)/$*).;)2 2$''. ()4*!/# (
.2 +-* /#-*0"#/# **&> 2$'''.* 3+'*- /#$..*-/*!E!0)/$*)'
+-*"-(($)"F++-*# $))0+*($)"#+/ ->
*;2# - . filter() /& .*'' /$*))- /0-)..0. /*!$/.$/ (.; map() /& .
*'' /$*))- /0-)./-).!*-( - +- . )//$*)*!$/.$/ (.>
Varargs
)4+-*"-(($)"')"0" .#1 /# *) +/*!E1--".F=+-( / -. '- !*-
( /#**-!0)/$*)/#/- +- . )/5 -*;*) ;*-. 1 -'1'0 .>
)1;2 0. ... .4)/3=
void something(StringString... lots) {
ifif (lots.size>0) {
// do something
}
}
lots $./- / .1--4;$)/#$.. )--4*! String *% /.>
1-$+/%0./0(+. 1 -4/#$)")*/(/# 4)( +-( / -.$)/*)
arguments --4=
functionfunction something() {
ifif (arguments.length>0) {
// do something
}
}
040. . * .4)/3=
defdef something(*lots)
ifif lots.length > 2
# do something
endend
endend
COLLECTIONS AND LAMBDAS
56
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*/'$) $/ ./# vararg & 42*-/*/#$.-*' =
funfun main() {
reciprocate(1, 2, 3, 5, 7)
}
funfun reciprocate(varargvararg values: IntInt) {
values
.map { 1.0 / it }
.forEach { println(it) }
}
J!-*( N--".N$)/# '..**&K
 - ; reciprocate() ) +/5 -*+-( / -.;*) +-( / -;*-()4
+-( / -.> ..$")/# vararg )( JvaluesK;)/# +-( / -.- "$1 )/*
0.$)/# !*-(*!) Array> - ;2 0. /# .( * .$)/# +- 1$*0.. /$*)/*
*(+0/ /# - $+-*'*! #*!/#*. 1'0 .)+-$)//# (/*/# *).*' >
What You Can Pass In
4+$''4; vararg +-( / -#.1'0 .+.. $)1$*((@ '$($/ '$./J >">;
reciprocate(1, 2, 3, 5, 7)K>#*. *)*/#1 /* *)./)/.;/#*0"#=
funfun main() {
valval foo = 1
valval bar = 2
valval goo = 3
valval baz = 5
valval heyWhatComesAfterBaz = 7
reciprocate(foo, bar, goo, baz, heyWhatComesAfterBaz)
}
funfun reciprocate(varargvararg values: IntInt) {
values
.map { 1.0 / it }
.forEach { println(it) }
}
J!-*( N'0 .!*---".N$)/# '..**&K
)4 3+- ..$*);$)'0$)".$(+' 1-$' - ! - ) .;) .0++'$ !*-1'0 .>
)$/$*);*/'$)*6 -..+ $'.0++*-/!*-) 3$./$)" Array>!4*0'- 4#1 )
COLLECTIONS AND LAMBDAS
57
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Array 2$/#/# 1'0 ./#/4*02)//*.0++'4;0//# !0)/$*)/#/4*0- ''$)"
0. . vararg J$)./ *!) Array +-( / -K;4*0)0. /# E.+- *+ -/*-F=
funfun main() {
valval things = arrayOf("foo", "bar", "goo")
capped(*things).forEach { println(it) }
}
funfun capped(varargvararg strings: StringString) = strings.map { it.toUpperCase() }
J!-*( N+- + -/*-N$)/# '..**&K
 - ;*0- capped() !0)/$*)/& . vararg *! String 1'0 .)- /0-)./# (
+$/'$5 > #1 ) Array *! String 1'0 .$)/# !*-(*! things> )+..
things /* capped() 4+- 73$)" things 2$/# *>#$..$''4.4.E/& /# *)/ )/.
*!/#$.--4)/# ( #.$)$1$0'+-( / -./*/# !0)/$*)''F>
$) 2 #++ ) /*2-$/ capped();$/2*0' .$(+' -/*%0./#1 capped /&
) Array +-( / ->! capped() 2 - 2-$// )4.*( *4 '. ;/#*0"#;)/# 4
#*. /*0. vararg;/# )/# .+- *+ -/*-./$''' /.0.0. *0- Array>
#$)"." /$/./-)" 2$/#/4+ ./#/(+/*+-$($/$1 .;/#*0"#> vararg 3+ /.
/#  $/ --4/4+ ./* 0. 2$/#/# .+- *+ -/*->*;$)/# !*''*2$)"
3(+' ;/# reciprocate() !0)/$*)/& . vararg *! Int>*-/# .+- *+ -/*-
/*2*-&;2 ) /*0. ) IntArray;)*/- "0'- Array=
funfun main() {
valval primes = intArrayOf(1, 2, 3, 5, 7)
reciprocate(*primes)
}
funfun reciprocate(varargvararg values: IntInt) {
values
.map { 1.0 / it }
.forEach { println(it) }
}
J!-*( N+- + -/*-2$/#-$($/$1 4+ .N$)/# '..**&K
Other JVM Collections
!4*0- 0.$)"*/'$)GH!*- 3(+' ;$!4*0- 0.$)"*/'$)!*-*-$)-4
COLLECTIONS AND LAMBDAS
58
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
)-*$++ 1 '*+( )/H4*0- 2 '*( /*0. */# -*'' /$*)/4+ .;
.0#. LinkedList>)/# 2#*' ;4*0.#*0'./$&2$/#*/'$)*'' /$*)/4+ .
2# - +*..$' ;./# */'$)/4+ .- (*- E)/0-'F!*-0. $)*/'$)>0/
.*( /$( .4*02$''7)1'../#/7/.4*0-. )-$* // -;.0#.0.$)"
LinkedHashMap /*$(+' ( )/)@./4' # >
*($)"0+$) '/ -#+/ -;2 2$''. #*2/*- / $)./) .*!-$/--4'.. .
H.0#. LinkedList H)#*2/*2*-&2$/#/# (>
get()get() and [][] Syntax
-'$ -;2  3+'*-  [] .4)/3!*- ..$)" ' ( )/.$) List;2# - [] 2*0'2-+
0@. $) 3*!/# $/ ($)/# '$.//*- /-$ 1 >
'.* 3+'*- 0.$)" [] !*- ..$)" )/-$ .$) Map;2# - [] 2*0'2-+/#
& 4!*-2#$#2 2)//*/-4/*- /-$ 1 1'0 >
)- '$/4; [] .4)/3$.1$'' !*- anything /#/*6 -.*) @+-( / - get()
!0)/$*)># )*/'$) )*0)/ -. [] .4)/3;$/.$(+'4- +' ./#/2$/#/#
*-- .+*)$)" get() ''>
*;!*- 3(+' ;2 '.*.20.$)" [] .4)/3/*" //# /##-/ -!-*( String>
String #. get() !0)/$*);/#//& . 0@. $) 3)- /0-)./#/#-/ -
!-*(/# ./-$)"># )2 0.  [] .4)/3Jit[1] == 'o'K;*/'$)''  get() *)/#
String;+..$)"$)*0-$) 3>
COLLECTIONS AND LAMBDAS
59
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
If, When, and While
-)#$)")'**+$)"- *((*)*+ -/$*).$)(*./+-*"-(($)"')"0" .>
*./')"0" .#1 .*( *) +/*! if J-)#. *)*)$/$*)K) while
J'**+. *)*)$/$*)K>*/'$)#.$/./& .*)/#*. *)/-*'./-0/0- .;+'0.
when ./-0/0- /#/$.$/- ($)$. )/*!/#$)".'$& 1D. switch ./-0/0- >
*;' /D./& '**&/ if; when;) while;). #*2*/'$)#)' ./# (/#
.( .H)$).*( . .;$6 - )/'4/#)H*/# -')"0" .>
If
if $.) -'40$,0$/*0.$)*(+0/ -+-*"-(($)";2# /# -/#/$.1=
ifif (i>10) {
// do something
}
elseelse {
// do something else
}
?1-$+/=
ifif (i>10) {
// do something
}
elseelse {
// do something else
}
// yes, it is the same as the Java syntax
61
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
?*-04=
ifif (i>10)
# do something
elseelse
# do something else
endend
)/# .0-! ;*/'$) if 2*-&./# .( 24=
valval i = 3
ifif (i > 10) {
println("something")
}
elseelse {
println("something else")
}
J!-*( N$!N$)/# '..**&K
.2$/#(*./')"0" .;/# else '0. $.*+/$*)';$!4*0*)*/#1 )4/#$)"
.+ $'/#/4*02)//**$)/#/.$/0/$*)=
valval i = 3
ifif (i > 10) {
println("something")
}
println("the if is done")
J!-*( N$!$/#*0/ '. N$)/# '..**&K
*-.$)"' @'$) *)$/$*)'2*-&;4*0).&$+/# - .=
valval i = 3
ifif (i>10) println("something") elseelse println("something else")
J!-*( N$)"' @$) $!N$)/# '..**&K
if '.*.0++*-/. else if $)$/$*)/*.$(+' else=
valval i = 3
ifif (i > 10) {
IF, WHEN, AND WHILE
62
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
println("something")
}
elseelse ifif (i > 2) {
println("something else")
}
elseelse {
println("and now for something completely different")
}
J!-*( N '. $!N$)/# '..**&K
*2 1 -;.4*02$''. $)/# ) 3/. /$*);/# *)1 )/$*)$)*/'$) 1 '*+( )/$.
/*0. when $)./ *! if !*-.0#. )-$*.>
When
when $.$./$)/$1 > when $.+*2 -!0'> when $.0.  lot $)*/'$)+-*"-(($)">)
when $..*( 2#/$6 - )//#)2#/4*0D''. $)()4*/# -+-*"-(($)"
')"0" .>
What We Do Elsewhere
# '*. ./)'*"4/* when $)1$./# switch .// ( )/=
switchswitch(foo) {
casecase 1:
// do something
breakbreak;
casecase 2:
// do something else
breakbreak;
defaultdefault:
// like, whatever
}
 - ; + )$)"*)/# 1'0 *! foo;2  1'0/ *) . /*!.// ( )/./#/!*''*2.
/# (/#$)" case J*-/# default .// ( )/.;$!/# - - )*(/# .K>
1-$+/'.*#. switch;2$/#/# .( .4)/3.1>
040. . case;)$/$./# '*. ./*!/# /#- $)/ -(.*!(/#$)"/# +*2 -*!
when=
IF, WHEN, AND WHILE
63
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
casecase foo
whenwhen 1
# do something
whenwhen 2
# do something else
elseelse
# like, whatever
endend
The Basic Use of whenwhen
# (*./*((*)'4@. )!*-(*! when '**&.'$& 1G1-$+/ switch *-04
case;2# - 4*0.0++'41'0 !*-*(+-$.*)). /0+$6 - )/-)#
*)$/$*)./*/ ./"$).//#/1'0 =
valval i = 3
whenwhen (i) {
1 -> println("something")
2 -> println("something else")
elseelse -> println("and now for something completely different")
}
J!-*( N2# )N$)/# '..**&K
#-)##.1'0 J1 ) 2K/#/$.*(+- /*/# $)+0/JiK># 7-./(/#
#.$/..// ( )/*-'*& 1'0/ ;) 1 -4/#$)" '. $..&$++ >!/# - - )*
(/# .;/# else .// ( )/*-'*&$. 3 0/ ># -> . +-/ ./# -)#
*(+-$.*)!-*(/# ./06/#/.#*0'  3 0/ $!/#/-)#*(+-$.*)$.( />
)/#$.. ;/# *0/+0/$.=
and now for something completely different
#$' /#/.)$++ /0. . Int 1'0 .!*-*(+-$.*);/#/$.)*/- ,0$- ( )/H
)4/#$)"/#/) *(+- 0.$)" ,0'$/4J==K) 0. =
valval thingy = "foo"
whenwhen (thingy) {
"foo" -> println("something")
"bar" -> println("something else")
elseelse -> println("and now for something completely different")
}
IF, WHEN, AND WHILE
64
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
J!-*( N2# )*-&.2$/#)44+ N$)/# '..**&K
*2 1 -;4*0))*/($3/4+ .;0)' .. == #++ )./*2*-&/**(+- /# (>#$.;
!*- 3(+' ;!$'.2$/#*(+$'  --*-;+*$)/$)"*0//#/ Int ) String -
$)*(+/$' /4+ .=
valval thingy = "foo"
whenwhen (thingy) {
"foo" -> println("something")
2 -> println("something else")
elseelse -> println("and now for something completely different")
}
Using whenwhen Instead Of else ifelse if
# when .4)/3!-*(/# +-  $)". /$*)$.!$-'4.$($'-/* switch ) case>
*2 1 -; when #.()4(*- *+/$*).>
) *+/$*)$./*.&$+/# +-( / -/*/# when>)/#/. ; #-)##.$/.
*2) Boolean 3+- ..$*);)/# 7-.//#/ 1'0/ ./* true $.0. >#$.- .0'/.$)
(*- $- /)'*"0 /* ifGelse ifGelse=
valval i = 3
whenwhen {
i > 10 -> println("something")
i > 2 -> println("something else")
elseelse -> println("and now for something completely different")
}
J!-*( N2# )$/#*0/)3+- ..$*)N$)/# '..**&K
Consolidating Multiple Branches
!4*0#1 ! 21'0 ./#/''.#*0'-*0/ /*/# .( -)#;4*0)0. 
*((@ '$($/ '$./$)./ *!.$)"' 1'0 !*-/# -)#*(+-$.*)"$).//#
1'0 .0++'$ /* when=
valval thingy = "foo"
whenwhen (thingy) {
"foo", "goo" -> println("something")
IF, WHEN, AND WHILE
65
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
"bar", "baz", "frobozz" -> println("something else")
elseelse -> println("and now for something completely different")
}
J!-*( N2# )2$/#*((.N$)/# '..**&K
*/'$)2$''*(+-  #*!/# *((@ '$($/ 1'0 .)$!)4*!/# ((/#;
/#/-)#$.0. >
*-)0( -.;4*0)0. .. .4)/3) in /*. /0+E-)" F)/ ./"$)./$/=
valval i = 3
whenwhen (i) {
inin 3..10 -> println("something")
inin 1..2 -> println("something else")
elseelse -> println("and now for something completely different")
}
J!-*( N2# )2$/#)" .N$)/# '..**&K
*2 1 -;/# . .$(+'$7/$*).*)'42*-&2# )4*0.0++'41'0 /* when
Jwhen(thingy)K;.*++*. /*' 1$)" when (+/4)0.$)"**' ) 3+- ..$*).
!*-/# -)# .>
Expressions As Branch Conditions
# 1'0 !*-*(+-$.*)* .)*/) /* *)./)/H)4 3+- ..$*)2$'''.*
2*-&=
valval thingy = "bar"
valval otherThingy = "BAR"
whenwhen (thingy) {
"foo", "goo" -> println("something")
otherThingy.toLowerCase() -> println("something else")
elseelse -> println("and now for something completely different")
}
J!-*( N2# )2$/#3+- ..$*).!*--)# .N$)/# '..**&K
 - ;/# . *)-)#*(+- . thingy /* otherThingy.toLowerCase() H$)/#$.
. ;/# 4-  ,0';).*/#/-)#$.0. >
IF, WHEN, AND WHILE
66
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
While
0./. if $.!$-'4*((*)$)+-*"-(($)"')"0" .;.*$. while *-.*( */# -
'**+/#/$.. *) 1'0/$)"*)$/$*)>
*2 #1 while $)1=
int i = 0;
whilewhile (i < 10) {
i++;
}
?)1-$+/=
varvar i = 0;
whilewhile (i < 10) {
i++;
}
?)04=
i = 0
whilewhile i < 10 dodo
i += 1
endend
*/'$)*6 -.(0#/# .( /#$)"=
varvar i = 0
whilewhile (i < 10) {
i++
println(i)
}
J!-*( N2#$' N$)/# '..**&K
*2 1 -; while $.)*/.*((*)$)*/'$).$/$.$)*/# -')"0" .>*- *!/ )2
0. *+ -/$*).*)*'' /$*).J >">; forEach()K-/# -/#) while>*2 1 -;$/ 3$./.;
.#*0'4*07)/# ) !*-$/>
IF, WHEN, AND WHILE
67
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
If/When As Expressions
)*/'$); if ) while . ('$& /# 4- !$-'4.$($'-/*/# $- ,0$1' )/.$)*/# -
')"0" .> when $.$/$6 - )/0/$/./$''!0)/$*).'*/'$& switch $)1; />
# - /#$)"../-//*" /$)/ - ./$)"$.2# )4*0- '$5 /#/ if ) when )*/*)'4
*6 --)#$)";0//#//# 4-  3+- ..$*).;)*$6 - )//#) 2 + 2>
Ternary Operator Replacement
*- 3(+' ;$)1;2 .*( /$( .0. /# E/ -)-4*+ -/*-F=
int foo = (bar > 10 ? 10 : bar)
 - ;2 - . //$)" foo ,0'/* bar;0)' .. bar $.*1 - 10;$)2#$#. 2 + foo
/ 10># +- )/# /$' 3+- ..$*)- /0-).2#/++ -. /2 )/# ? )/# : $!
/# **' ) 3+- ..$*)*)/# ' !/$. true;*/# -2$. $/- /0-).2#/++ -.!/ -
/# :>
*/'$)'&./ -)-4.0++*-/;0/(*- /#)(& .0+!*-$/4''*2$)" if /*. -1
$)/# .( -*' =
valval bar = 5
valval foo = ifif (bar > 10) 10 elseelse bar
println(foo)
J!-*( N$!.)3+- ..$*)N$)/# '..**&K
#$.$./# .( 0.$) ..-0' .$)1;%0./$(+' ( )/ 0.$)") if>
0- if )#1 !0''- .@2-++ * '*&.;$!2 ) /# (=
valval bar = 5
valval foo = ifif (bar > 10) {
10
}
elseelse {
bar
}
println(foo)
J!-*( N$!.)3+- ..$*);.$)"'*&.N$)/# '..**&K
IF, WHEN, AND WHILE
68
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
# 1'0 *!/# './.// ( )/*!/# '*&$.2#//#/'*& 1'0/ ./*>)/#$.
. ; #*!/#*. '*&.$..$)"' .// ( )/;0/$!2 #. 1 -'.// ( )/.$)
'*&;2#$' /# 4- '' 3 0/ ;/# 1'0 !-*(/# './.// ( )/$.0. !*-/#
- .0'/J..0($)"/#/'*&$.0. ;. *)/# *)$/$*)' 3+- ..$*)K>
whenwhen Expressions
when )'.* 0. .) 3+- ..$*)=
valval i = 3
valval message = whenwhen (i) {
inin 3..10 -> "something"
inin 1..2 -> "something else"
elseelse -> "and now for something completely different"
}
println(message)
J!-*( N2# ).)3+- ..$*)N$)/# '..**&K
 - ;/# when 1'0/ ./*./-$)";2#$#$...$") /* message )+-$)/ >
#)$''4;/# -)# .*!/# if *- when *)*/#1 /* 1'0/ /*/# .( /4+ >
*;/#$.-0).=
valval i = 3
valval message = whenwhen (i) {
inin 3..10 -> "something"
inin 1..2 -> 5
elseelse -> "and now for something completely different"
}
println(message)
*2 1 -;4*02$''" /.*( *(+$' -2-)$)".=
warning: conditional branch result of type String is implicitly cast to Any
in 3..10 -> "something"
^
warning: conditional branch result of type Int is implicitly cast to Any
in 1..2 -> 5
IF, WHEN, AND WHILE
69
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
^
warning: conditional branch result of type String is implicitly cast to Any
else -> "and now for something completely different"
 - ;/# *(+$' -$./ ''$)"4*0/#/.$) /# -)# . 1'0/ /*$6 - )//4+ .;
/# $(+'$ /4+ *! message $. Any> Any $)*/'$)$.-*0"#'4)'*"*0./* Object $)
1H$/$./# -**//4+ *!/# '..#$ --#4>1 -4'..$)*/'$) 1 )/0''4
3/ ).!-*( Any> Any $./# *)'4*((*)) ./*-'..*! Int ) String;2#$#$.
2#4/# *(+$' -#*. /#/./# /4+ !*- message>*2 1 -;/# 2-)$)"$./# -
/*#$)//*4*0/#/+ -#+.2#/4*02-*/ $.)*/2#/4*0- ''4#1 $)($)>
Else Required
*-(''4; if ) when *)*/#1 /* E 3#0./$1 F> - ;E 3#0./$1 F* .)*/
( )/#/$/(& .4*0/$- J/#/2*0' E 3#0./$)"FK>/# -;E 3#0./$1 F
( )./#/''+*..$$'$/$ .- *1 - 4.*( -)#>*;/#$.$. 3#0./$1 =
ifif (i>10) {
// do something
}
elseelse {
// do something else
}
 - ;)*(// -2#//# 1'0 *! i $.;*) *!/# /2*-)# .2$'' /& )>
#$.;#*2 1 -;$.)*/ 3#0./$1 =
ifif (i>10) {
// do something
}
*-$./#$.=
valval i = 3
whenwhen {
i > 10 -> println("something")
i > 2 -> println("something else")
}
)/# . . .;/# - - 1'0 .!*- i !*-2#$# none *!/# -)# .$.1'$>)/#/
. ;/# if *- when .$(+'4* .)*/*)4/#$)">
IF, WHEN, AND WHILE
70
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*-./)'*) if ) when .// ( )/.;/#$.$.7) >*2 1 -;$!4*0- "*$)"/*0.
if ) when . 3+- ..$*).;/# 4(0./  3#0./$1 >!/ -'';2 - /-4$)"/*
1'0/ /# 1'0 *!/# if *- when;)2 *)*/#1 1'0 $!/# - $..*(
$)+0/!*-2#$#)*-)#,0'$7 .>
!4*0/-4)*)@ 3#0./$1 if *- when !*-) 3+- ..$*);.0#.=
valval i = 3
valval message = whenwhen (i) {
inin 3..10 -> "something"
inin 1..2 -> 5
}
println(message)
?4*02$''" /*(+$' - --*-=
error: 'when' expression must be exhaustive, add necessary 'else' branch
val message = when (i) {
Bustin’ Out
*( /$( .;2#$' 4*0- $)'**+;4*02$''2)//*#)" /# )*-('9*2*!/#
'**+>*- 3(+' ;4*0- $) for '**+;)!/ -/# W/#$/ (;4*02$.#/* 3$//#
'**+;+ -#+.$)- .+*). /*.*( .$")'J >">;) ''/$*)9"2.. /K>
*0)'24.0. return /* 3$//#  )/$- !0)/$*)/#/4*0- $)> 4*)/#/;
/#*0"#;/# - - /2*'**+*)/-*'.// ( )/./#/4*0)0. = break ) continue>
# . 2*-&(0#'$& /# $-*0)/ -+-/.$)1)*/# -+-*"-(($)"')"0" .>
breakbreak
break .4.E)*)/# '**+F=
varvar i = 0;
whilewhile (i < 10) {
i++
ifif (i == 5) breakbreak
IF, WHEN, AND WHILE
71
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
println(i)
}
J!-*( N- &// ( )/.N$)/# '..**&K
#$.- .0'/.$)=
1
2
3
4
) i $. ,0'/* 5;2 break *0/*!/# '**+>$) 2 - *$)"/#/ !*- /#
println() '';2 *)*/+-$)/ 5 /*/# *0/+0/>
continuecontinue
continue .4.E.&$+/# - ./*!/#$.+../#-*0"#/# '**+;)(*1 '*)"/*/#
) 3/+../#-*0"#/# '**+F=
varvar i = 0;
whilewhile (i < 10) {
i++
ifif (i==5) continuecontinue
println(i)
}
J!-*( N*)/$)0 // ( )/.N$)/# '..**&K
#$."$1 .0.=
1
2
3
4
6
7
8
9
10
- "*$)"/#-*0"#/# '**+''SR/$( .;0)'$& $)/# break . >*2 1 -;2 -
IF, WHEN, AND WHILE
72
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
.&$++$)"+-/*!/# '**+* 1$ continue H.+ $7''4;2 - .&$++$)"*1 -/#
println() ''>.- .0'/; 5 * .)*/++ -$)/# *0/+0/;/#*0"#''/# */# -
1'0 .*>
IF, WHEN, AND WHILE
73
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Object Orientation
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Basic Classes
 0. */'$)' /.4*0%0./2-$/ )$)1*& !0)/$*).;4*0)*(+'$.#!$-$/
2$/#*0//0''4- /$)"4*0-*2)'.. .>0/; 1 )/0''4;4*02$''2)//*- /
'.. .;/*(* '/)- '/  #1$*->
Basic Classes
'.. .$)*/'$)- 1 -4.$($'-/*/# $-1)04*0)/ -+-/.=/# 4+-$(-$'4
*)/$)+-*+ -/$ .)!0)/$*)./#/*+ -/ *)/#*. +-*+ -/$ .>
'..* .)*/#1 /**)/$))4*!/#/H/#$.$.1'$*/'$)'.. 7)$/$*)=
classclass FooFoo {
}
*$./#$.=
classclass FooFoo
1$)"'..2$/#)**4. (.*>*2 1 -;$)*/'$);4*02$''*.$*)''4.
/#/.*-/*!/#$)";+-/$0'-'42$/# /'.. . ) . ' '.. .>*2 1 -;(*./
*-$)-4'.. .2$''#1 +-*+ -/$ .)!0)/$*).;.2 2$''.  '*2>
Creating Instances of Classes
1)04*/#0. new /*- / $)./) .*!'.. .;2# /# -.& 42*-$)
1=
77
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
FooFoo foo = newnew FooFoo();
?*-.( /#*$)04=
foo = Foo.new
1-$+/#.! 291*-.*!- /$)"*% /.;$)'0$)"*) 0.$)"/# new & 42*-;
(0#.4*02*0'$)1>
)*/'$);/#*0"#;4*0/- //# '..)( .!0)/$*))( ;)%0./''$/=
classclass FooFoo
funfun main() {
valval foo = FooFoo()
println(foo)
}
J!-*( N'.. .))./) .N$)/# '..**&K
) 6 /;4*0- ''$)"/# '..E*)./-0/*-F/#$.24H2 2$'' 3+'*-
*)./-0/*-.(*- '/ -$)/#$.#+/ ->
# println() ''* .)*/+-$)/(0#=
[object Object]
#/$.+-/'40 /*/# '$($/ 0$'/@$)$(+' ( )//$*)*!/# toString() !0)/$*)
/#/2 $)# -$/;)+-/'40 /*'$($//$*).$)/# */'$).-$+/$)" )1$-*)( )/
0. 4'..**&>
Packages
*/'$)(& .0. *!+&" ./# .( 24/#/1* .=.)( .+ !*-
'.. .>);.2$/#1;4 !0'/'..$.)*/$)+&" >*./*/'$)7' .$)
)/0'+-*% /H.*++*. /*'$//'  3+ -$( )/.H2$'' $)+&" >
# .4)/3!*-. //$)"0+*/'$)+&" $.) -'4$ )/$'/*/#/*!1=+0/
package '$) //# /*+*!/# 7' =
BASIC CLASSES
78
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
packagepackage bar.goobar.goo
classclass FooFoo
valval foo = FooFoo()
#/$6 -.;*(+- /*1;$.%0.//# '&*!/# . ($*'*)//#  )*!/#
package .// ( )//#//#  ,0$1' )/1 package .// ( )/2*0'- ,0$- >
*/ /#/2#$' #1$)"+&" .$. very *((*)$)*/'$)+-*% /.;'..**&* .
)*/0. /# (;)) $/# -*(*./*/# -.>
Common Contents
#$' */'$)'.. .)#*''*/.*!$6 - )/.*-/.*!+-*"-(($)"*)./-0/.;/#
/2*(*./*((*)- !0)/$*).)+-*+ -/$ .>0./.2 ) 7) /# (*0/.$
*!'..;2 ) 7) /# ($).$ *!'..;!*-0. 4$)./) .*!/#/'..>
Functions
 '-$)")*-$)-4!0)/$*)$..$(+'4(// -*!#1$)"/# fun  $).$ /#
*4*!'..=
classclass FooFoo {
funfun something() {
println("Hello, world!")
}
}
''$)")*% /D.!0)/$*)$.*(+'$.# 40.$)"*/J.K)*//$*)=
classclass FooFoo {
funfun something() {
println("Hello, world!")
}
}
funfun main() {
valval foo = FooFoo()
foo.something()
}
J!-*( N$)"0)/$*)./*'.. .N$)/# '..**&K
BASIC CLASSES
79
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*0#1  ../*/# !0''-)" *!+$'$/$ ./#/2  .-$ $) /# #+/ -*)
!0)/$*).=+-( / -.;1--".; 3+- ..$*)*$ .;'*'1-$' .; />
Properties
0./.'.. .)#1 !0)/$*).;/# 4)#1 +-*+ -/$ .=
classclass FooFoo {
varvar count = 0
funfun something() {
count += 1
println("something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo()
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N$)"-*+ -/$ ./*'.. .N$)/# '..**&K
 - ;2 #1  count +-*+ -/4;$)$/$''4. //* 0;/#/2 $)- ( )/*) #''/*
something()>#/ count $./# )0. $)/# +-$)/ *0/+0/;*0-/ .4*!$/*!
./-$)"$)/ -+*'/$*)>
0)/$*).$)/# '..)- ! -/*+-*+ -/$ .%0./0.$)"/# +-*+ -/4)( >
0)/$*).*0/.$ *!/# '..;.0#. main();) /*0. */)*//$*);%0./'$& 2
*!*-''$)"!0)/$*).*))*% />
*;2 " /!*0-'$) .*!*0/+0/=/#- !-*(/# something() !0)/$*)/#/2 -
''$)";)*) !-*( main() $/. '!=
something() was called 1 times
something() was called 2 times
something() was called 3 times
the final count was 3
*2 1 -;+-*+ -/$ .$)*/'$))" /.0-+-$.$)"'4*(+'$/ > 2$'' 3+'*-
BASIC CLASSES
80
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
/# ($)"- / - /$' $))0+*($)"#+/ ->
thisthis
.2$/#1;*/'$)0. . this .+. 0*@+-*+ -/4/*- ! -/*/# $)./) *!/#
*% /$)2#*. !0)/$*)4*0#++ )/* -0))$)">
*0)0. /#/.+- 73!*-+-*+ -/4- ! - ) .*-!0)/$*)''.;.0#.
this.count=
classclass FooFoo {
varvar count = 0
funfun something() {
thisthis.count += 1
println("something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo()
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N# /#$.. 0*@-*+ -/4N$)/# '..**&K
-4*0*0'0. $/*)$/.*2)/*- ! -/*/# 0-- )/$)./) ;.0#.!*-.0++'4$)"
/#/*% /.+-( / -/*!0)/$*)=
classclass FooFoo {
funfun printMe() {
println(thisthis)
}
}
funfun main() {
valval foo = FooFoo()
foo.printMe()
}
BASIC CLASSES
81
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
J!-*( N..$)"/#$..-( / -N$)/# '..**&K
.$*)''4;4*02$''. *1-$)/.*! this;.0#. this@Foo> 2$''. 2#/
/#/.4)/3( ). '/ -$)/# **&>
Constructors
.2$/#1)04;*/'$)'..* .)*/)  ..-$'4) ) 3+'$$/'4@ '- 
*)./-0/*->4 !0'/;4*0" /5 -*@+-( / -*)./-0/*-;. Foo $$)/#
+-  $)" 3(+' .>)4*!4*0-*/'$)'.. .2$'')*/) )40./*(
*)./-0/*-.?0/! 22$'') *) ;)*)*.$*)/# 4($"#/) (*- /#)
*) >
What You Normally See
)1)04; '-$)"*)./-0/*-.$.(// -*!#1$)".+ $7( /#*J*-
/#$)"/#/'**&.'$& ( /#*K '- *)/# '..>)1;/#/+. 0*@( /#*
#./# )( *!/# '..=
classclass FooFoo {
finalfinal privateprivate int count;
FooFoo(int count) {
thisthis.count = count;
}
}
2#$' $)04;2 0. /# initialize ( /#*=
classclass FooFoo
defdef initialize(count)
@count = count
endend
endend
$($'-'4;1-$+/0. .!0)/$*))( !/ -/# '..;/' ./$).*(
++-*# .*!$(+' ( )/$)"*% /.$)1-$+/>
# 244*02$'')*-(''4. */'$)'.. .#1 *)./-0/*-$.4(&$)"/#
/0''..)( $/. '!/& /# *)./-0/*-+-( / -.=
BASIC CLASSES
82
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
classclass FooFoo(varvar count: IntInt) {
funfun something() {
count += 1
println("something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N*)./-0/*-.N$)/# '..**&K
 - ;2 #1 *) *)./-0/*-+-( / -; count># var & 42*-$)!-*)/*!$/
( )./#/2 )0. $/'$&  var +-*+ -/4;$)/#$.. #)"$)"$/.1'0 1$/#
$)- ( )/J+=K*+ -/*->
The Formal Approach
# *)./-0/*-.4)/3.#*2)*1 $.- ''4.#*-/#)!*-0.$)"/# constructor
& 42*-=
classclass FooFoo constructorconstructor(varvar count: IntInt) {
funfun something() {
count += 1
println("something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N*-('*)./-0/*- '-/$*)N$)/# '..**&K
BASIC CLASSES
83
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*-(*./'.. .;2$/#5 -**-*) *)./-0/*-.;4*02$'')*/. /# constructor
& 42*-0. >/ *( .(*- $(+*-/)/2# )4*0#1 (0'/$+' *)./-0/*-.;
$)'0$)" +-$1/ *)./-0/*-.>
Init Blocks
*(+- 2$/#1)04*)./-0/*-.;/#*0"#;*0-*/'$)*)./-0/*-.-
($..$)".*( /#$)"=*4>''2 - *$)"$. '-$)"'$./*!+-( / -./#/)
 .0++'$ /*/# $)./) *!/# '..>
*/'$)$)./ 0. .) init '*&>
4)/3@2$. ;) init '*&- . (' .1 static '*&=. /*!- .@2-++ 
.// ( )/.+-   4& 42*->)1;/# static '*&$. 3 0/ *) !*-/#
'..)$.0. /*$)$/$'$5 *(+' 3 static 7 '.>)*/'$);) init '*&$.
3 0/ *) !*- # instance ). -1 ... '$) *)./-0/*-*4=
classclass FooFoo(valval sillyCount: StringString) {
varvar count = 0
init {
count = sillyCount.toInt()
}
funfun something() {
count += 1
println("something() was not called $count times")
}
}
funfun main() {
valval foo = FooFoo("7")
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N)$/'*&N$)/# '..**&K
 - ;$) 3+'$'4;2 - +..$)"$) String - +- . )//$*)*!)$)$/$'1'0 /*
0. !*-/# count;-/# -/#)+..$)"$)/# var count .2 $ !*- ># init
BASIC CLASSES
84
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
'*&$.0. /*$)$/$'$5 count !-*( sillyCount .+-/*!. //$)"0+*0-$)./)
*! Foo># - $..$(+' -24/*#)' /#$.;1$+-*+ -/4$)$/$'$5/$*);/#/2 2$''
3+'*- (*- $) /# #+/ -*)+-*+ -/$ .>
init '*&.#1 *0+' *!*/# -$./$)/$*).;+-/$0'-'42# )*(+- /*1G
04*)./-0/*-.=
I *0)#1 (*- /#)*) init '*&># 4-  3 0/ $)/# *- -/#/
/# 4++ -$)/# '..>
I ) init '*&)*)'4- ! - ) +-*+ -/$ ./#/-  '-  !*- $/$)/#
'..;)*/*) . '- !/ -$/>.- .0'/;) init '*&*!/ )++ -.!/ -
/# '$./*!+-*+ -/$ .;.*/# init '*&)- ! - ) )4*!/# +-*+ -/$ .>
Parameters Sans varvar Or valval
*( /$( .;4*02$''. *)./-0/*-+-( / -./#/*)*/#1 /# var *- val
& 42*-># 4%0./#1 /# +-( / -)( )/4+ ># 4- ./$''*)./-0/*-
+-( / -.;0//# 4- )*/+-*+ -/$ .>*0))*/- ! -/*/# (!-*(/# !0)/$*).
*!4*0-'..>*0)- ! -/*/# (!-*( init '*&.;/#*0"#=
classclass FooFoo(sillyCount: StringString) {
varvar count = 0
init {
count = sillyCount.toInt()
}
funfun something() {
count += 1
println("something() was not called $count times")
}
}
funfun main() {
valval foo = FooFoo("7")
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N*)./-0/*-)$/@)'4-( / -.N$)/# '..**&K
BASIC CLASSES
85
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#$.$./# .( ./# +-  $)" 3(+' ;%0./2$/#*0//# var>/2*-&.; 0.
/# *)'4+' 2 - ! -/* sillyCount $.!-*() init '*&>
Having Multiple Constructors
*( */'$)'.. .#1 )**)./-0/*-;0.$)"*)'4/#  !0'/5 -*@+-( / -
*)./-0/*->*( */'$)'.. .#1 *) *)./-0/*->)#)!0'*!'.. .2$''
#1 (*- /#)*) *)./-0/*->
# +-$(-4*)./-0/*-++ -.$)/# '.. '-/$*)$/. '!;.2 #1 . )>
/# -*)./-0/*-.;$!/# 4 3$./;- '' E. *)-4*)./-0/*-.F;)/# 4
.*( 2#/- . (' *-$)-4!0)/$*).=
classclass FooFoo(varvar count: IntInt) {
constructorconstructor(sillyCount: StringString) : thisthis(sillyCount.toInt()) {
// could have code here if needed
}
funfun something() {
count += 1
println("something() was not called $count times")
}
}
funfun main() {
valval foo = FooFoo("7")
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N *)-4*)./-0/*-.N$)/# '..**&K
+-$(-4*)./-0/*-#.)*!0)/$*)*4H)4.0#* "* .$)/*) init
'*&> *)-4*)./-0/*-.;/#*0"#;)#1 *$ .;/# 24/#/!0)/$*).*;
0/*)'4$!)  >
 *)-4*)./-0/*-.(0./--)" /*''/# +-$(-4*)./-0/*->#$.$.
#)' 4#1$)" this() ''!/ -/# *)./-0/*-+-( / -'$./;. +-/ !-*(
/#/'$./1$*'*)># this() '') ./*(/#/# +-( / -'$./*!/#
BASIC CLASSES
86
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
+-$(-4*)./-0/*-H$)/#  3(+' .#*2)*1 ;/# . *)-4*)./-0/*-
) ./*+-*1$  Int /*/# +-$(-4*)./-0/*->
Default Parameter Values
1$)"(0'/$+' *)./-0/*-.$.*(+-/$1 '4*((*)$)')"0" '$& 1;.
/#-*0"#1Z;/# - 2.)*.0++*-/!*- !0'/+-( / -1'0 .> ) ;$!4*0
2)/ /*''*2!*-*/#.$(+' )*(+' 3*% /$)./)/$/$*);4*0)  /*
 '- ''*!/# )  ..-4*)./-0/*-.;+ -#+.#$)$)"!-*(*) /*)*/# ->*-
3(+' ;)-*$ 1 '*+ -.2#*#1 - / 0./*( View '.. .($"#/
!($'$-2$/#/# !*0- View *)./-0/*-.=
publicpublic View(ContextContext context) {
// a ton of code
}
publicpublic View(ContextContext context, @Nullable AttributeSetAttributeSet attrs) {
thisthis(context, attrs, 0);
}
publicpublic View(ContextContext context, @Nullable AttributeSetAttributeSet attrs, int defStyleAttr) {
thisthis(context, attrs, defStyleAttr, 0);
}
publicpublic View(ContextContext context, @Nullable AttributeSetAttributeSet attrs, int defStyleAttr,
int defStyleRes) {
thisthis(context);
// another ton of code
}
 - ; #*!/# *)./-0/*-..$(+'4.*))*/# -+*..$' +-( / -/*
.0++'$ ;)/# *)./-0/*-.#$)/**/# -*)./-0/*-./* '$($)/ 0+'$/$*)
*!$)$/$'$5/$*)'*"$>
*/'$);'$& 04;$.+ ). .2$/#/#$.;4''*2$)"!*-4*0/* 7)  !0'/1'0 .!*-
4*0-*)./-0/*-+-( / -.?%0./.4*0)*) *-$)-4!0)/$*).># */'$)
,0$1' )/*!/# View *)./-0/*-. /($"#/'**&'$& =
classclass ViewView(
context: ContextContext,
attrs: AttributeSetAttributeSet? = nullnull,
defStyleAttr: IntInt = 0,
defStyleRes: IntInt = 0
BASIC CLASSES
87
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
) {
// two tons of code
}
 - ;/# '// -/#- +-( / -.''#1  !0'/1'0 . 7) ;"$1$)"0./# $'$/4
/*''/# *)./-0/*-2$/#*) +-( / -;''!*0-+-( / -.;*-*($)/$*).$)
 /2 )>
# *1 * .)$++ /*)/$).) 2'$) .$) /2 )/# +-( / -.>#/$.( - '4
(// -*!* !*-(//$)">4+$'*/'$)2$''.+'$/'*)"+-( / -'$./.0+/#$.24;
2$/#*) +-( / -+ -'$) >
'.*; attrs $..#*2).#1$)"/4+ *! AttributeSet?>.2.( )/$*) 
+- 1$*0.'4J-$ 94K;/# ? .083$)$/ ./#//#$.$.E)0''' /4+ F;)2 2$''
3+'*- /#/$)"- / - /$'$) )0+*($)"#+/ ->
Inheritance
.2$/#()4*% /@*-$ )/ ')"0" .;*/'$).0++*-/.'..$)# -$/) >
.0'..$)# -$/.!-*(.0+ -'..;)2#/$/E$)# -$/.F- ''*!/# +-*+ -/$ .;
!0)/$*).;*)./-0/*-.;)*/# -./06/#//# .0+ -'..(& .1$'' >
What Happens By Default
# 1-$*0.1 -.$*).*!/# Foo '...#*2)*1 *)*/$)# -$/ 3+'$$/'4!-*(
)4/#$)">#  !0'/. '..!*-*/'$)'..$. Any;).* Foo $..$/* -$1
!-*( Any>
Extending a Class
)1;2 0. /# extends & 42*-/* )*/ $)# -$/) =
classclass FooFoo extendsextends BarBar {
}
040. ./# < .4(*'=
classclass FooFoo < Bar
endend
BASIC CLASSES
88
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*/'$)$.$/'*. -/*04.4)/3;0/$/0. .*'*)./# .4(*'=
classclass FooFoo(varvar count: IntInt) : AnyAny() {
funfun something() {
count += 1
println("something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N)# -$/) N$)/# '..**&K
 - ;2 -  3+'$$/'4 '-$)"/#//# .0+ -'..*! Foo $. Any>
Chaining to the Superclass Constructors
# () //#  )*! Any() $)/# *1  3(+' $./0''4''/*/# .0+ -'..
*)./-0/*->*02$'') /*.0++'4+-( / -./*/#/'';$!/# .0+ -'..
- ,0$- ./# (=
openopen classclass BaseBase(valval tag: StringString)
classclass FooFoo(varvar count: IntInt) : BaseBase("Foo Example") {
funfun something() {
count += 1
println("$tag: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
BASIC CLASSES
89
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
println("the final count was ${foo.count}")
}
J!-*( N)# -$/) )*)./-0/*-.N$)/# '..**&K
 - ; Base #.*)./-0/*-- ,0$-$)" tag +-( / -> Foo ) ./*.0++'4/#/
+-( / -2# )$/''./# Base() *)./-0/*->0//# ) Foo )- ! - ) /# tag
+-*+ -/4/#/$/$)# -$/.;$)'0$)"$/.+-/*!/# +-$)/ *0/+0/=
Foo Example: something() was called 1 times
Foo Example: something() was called 2 times
Foo Example: something() was called 3 times
the final count was 3
Open Classes
*02$'')*/$ /#//#  '-/$*)*! Base #..*( /#$)" 4*)*)./-0/*-
+-( / -=$/$.+- 73 2$/#/# open & 42*->
)1;4 !0'/;)4'..)  3/ ) 4.0'..>*+- 1 )/'..!-*(
 $)" 3/ ) ;4*0#1 /*(-&$/. final>
*/'$)"* ./# *++*.$/ -*0/ >*/'$)'.. . cannot   3/ ) 4 !0'/>*0
#1 /*(-&'.. ./#/)  3/ ) 0.$)" open>$'0- /**/#/- .0'/.$))
--*->*;$!2 - (*1 /# open !-*(/# +- 1$*0. 3(+' =
classclass BaseBase(valval tag: StringString)
classclass FooFoo(varvar count: IntInt) : BaseBase("Foo Example") {
funfun something() {
count += 1
println("$tag: something() was called $count times")
}
}
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
?2 " /*(+$'  --*-=
BASIC CLASSES
90
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
error: this type is final, so it cannot be inherited from
class Foo(var count: Int) : Base("Foo Example") {
^
#$.#.)$(+*-/)/.$  6 /=4*0))*/- / .0'.. .*!-$/--4'.. .
/#/4*0*)*/*)/-*'>)'4$!/# $(+' ( )/ -*!/#/*/# -'..$.2$''$)"/*
.0++*-/.0'.. .2$''/# $(+' ( )/ -0. /# open & 42*-;)*)'4/#*.
'.. .)4*0 3/ )>
#$.++-*#$.0)*((*)$)(%*-+-*"-(($)"')"0" .>.$''4;*/'$)
- *")$5 ./#/$)# -$/) $.+*/ )/$''4!-"$' - '/$*).#$+ /2 )/2*'.. .>
 ''4;/# '.. .# - /* /# $.&*1.0./$/0/$*)+-$)$+' >#$..$''4.// .
/#/$)./) .*!.0'...#*0' 0.' anywhere /#/$)./) .*!/#
.0+ -'..) 0. >+-*"-(($)"')"0" ))*/"0-)/ /#/;)
 1 '*+ -..*( /$( ." /.'*++42# )- /$)".0'.. .>*;*/'$)!*- .
 1 '*+ -./*(& *).$*0. $.$*)/*''*2.0'.. .;$)#*+ ./#/$/# '+.
/#*.  1 '*+ -./& ++-*+-$/  ! ).$1 +-*"-(($)"./ +./* ).0- /#/
.0'.. . #1 . 3+ / >
Overriding Functions
.4*0($"#/ 3+ /;.0'..)''!0)/$*)./#/$/$)# -$/.!-*($/..0+ -'..=
openopen classclass BaseBase(valval tag: StringString) {
funfun gimmeTheTag() = tag
}
classclass FooFoo(varvar count: IntInt) : BaseBase("Foo Example") {
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N)# -$/) )0)/$*).N$)/# '..**&K
BASIC CLASSES
91
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
);.4*0($"#/ 3+ /;.0'..)*1 --$ !0)/$*).*!$/..0+ -'.. .?/
' ./;$!/#*. /**- (-& . open>4 !0'/;*/'$)!0)/$*)$.)*/*+ ))
))*/ *1 --$ )>*/*)'4(0.//# .0+ -'..!0)/$*) (-& . open;0/
/# overriding !0)/$*)(0./ (-& 2$/# override=
openopen classclass BaseBase(valval tag: StringString) {
openopen funfun gimmeTheTag() = tag
}
classclass FooFoo(varvar count: IntInt) : BaseBase("Foo Example") {
overrideoverride funfun gimmeTheTag() = "Not the Original Tag"
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N)# -$/) )1 --$$)"0)/$*).N$)/# '..**&K
 - ; gimmeTheTag() $.) open !0)/$*)$) Base;.* Foo ) override $/2$/#$/.
*2)$(+' ( )//$*)># ./-$)"$)/ -+*'/$*)2$''0. /# *1 --$ )!0)/$*)=
Not the Original Tag: something() was called 1 times
Not the Original Tag: something() was called 2 times
Not the Original Tag: something() was called 3 times
the final count was 3
4 !0'/; open $./-).$/$1 >) !0)/$*)$.(-& . open;$/$. open !*-''
.0'.. .*2)/# #$ --#4;- "-' ..*!2# /# -$)/ -1 )$)"'.. . override
/# !0)/$*)*-)*/=
openopen classclass BaseBase(valval tag: StringString) {
openopen funfun gimmeTheTag() = tag
}
BASIC CLASSES
92
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
openopen classclass IntermezzoIntermezzo(valval thing: StringString) : BaseBase(thing)
classclass FooFoo(varvar count: IntInt) : IntermezzoIntermezzo("Foo Example") {
overrideoverride funfun gimmeTheTag() = "Not the Original Tag"
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N*1 --$ $.-).$/$1 N$)/# '..**&K
 - ; 1 )/#*0"# Foo )*2 3/ ). Intermezzo;$/)./$''*1 --$ gimmeTheTag()>
/*0'*.* 1 )$! Intermezzo $/. '!*1 --$ . gimmeTheTag()?0)' .. Intermezzo
(-&.$/.1 -.$*)*!/#/!0)/$*). final=
openopen classclass BaseBase(valval tag: StringString) {
openopen funfun gimmeTheTag() = tag
}
openopen classclass IntermezzoIntermezzo(valval thing: StringString): BaseBase(thing) {
finalfinal overrideoverride funfun gimmeTheTag() = "-$thing-"
}
classclass FooFoo(varvar count: IntInt) : IntermezzoIntermezzo("Foo Example") {
overrideoverride funfun gimmeTheTag() = "Not the Original Tag"
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
valval foo = FooFoo(0)
BASIC CLASSES
93
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
foo.something()
foo.something()
foo.something()
#$.- .0'/.$)) --*-=
error: 'gimmeTheTag' in 'Intermezzo' is final and cannot be overridden
override fun gimmeTheTag() = "Not the Original Tag"
^
) "$);/# *% /$1 $./*!*-  1 -4*4/*/#$)&*0//# -($7/$*).*!
''*2$)"!0)/$*)./* *1 --$ ))/# -($7/$*).*!*1 --$$)"/#*.
!0)/$*).>
Overriding Properties
/$.'.*+*..$' !*-'../* '-  property . open;''*2$)".0'.. ./*
*1 --$ $/>
*./'4/#$.$.)  !*-+-*+ -/$ . '- .*)./-0/*-+-( / -.>*-
3(+' ;' /D."*&/*/#$..-$+/=
openopen classclass BaseBase(valval tag: StringString) {
openopen funfun gimmeTheTag() = tag
}
openopen classclass IntermezzoIntermezzo(valval thing: StringString) : BaseBase(thing)
classclass FooFoo(varvar count: IntInt) : IntermezzoIntermezzo("Foo Example") {
overrideoverride funfun gimmeTheTag() = "Not the Original Tag"
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
BASIC CLASSES
94
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
println("the final count was ${foo.count}")
}
J!-*( N*1 --$ $.-).$/$1 N$)/# '..**&K
*/ /#//# *)./-0/*-+-( / -!*- Base $.''  tag;2#$' /# *)./-0/*-
+-( / -!*- Intermezzo $.''  thing># 4#++ )/* /# .( ;0(;/#$)";
.$) Intermezzo +.. .$/. thing +-*+ -/4/*/# ''/*/# Base *)./-0/*->
0++*. ;/#*0"#;/#/2 2)/ /*)( /# . /# .( ;.0#.#1$)"/# (*/#
tag=
openopen classclass BaseBase(valval tag: StringString) {
openopen funfun gimmeTheTag() = tag
}
openopen classclass IntermezzoIntermezzo(valval tag: StringString): BaseBase(tag)
classclass FooFoo(varvar count: IntInt) : IntermezzoIntermezzo("Foo Example") {
overrideoverride funfun gimmeTheTag() = "Not the Original Tag"
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
#$.- .0'/.$)*(+$'  --*-=
error: 'tag' hides member of supertype 'Base' and needs 'override' modifier
open class Intermezzo(val tag: String): Base(tag)
^
#$.$. 0. */#-  '- .+-*+ -/$ .;)4 !0'/4*0))*/- @ '-
+-*+ -/44- 0.$)"/# )( >
) 24/*- ../#$.$./*- '$5 /#/2 *)*/) /# *)./-0/*-+-( / -
$) Intermezzo /* +-*+ -/4>/*0' +'$)*)./-0/*-+-( / -=
BASIC CLASSES
95
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
openopen classclass BaseBase(valval tag: StringString) {
openopen funfun gimmeTheTag() = tag
}
openopen classclass IntermezzoIntermezzo(tag: StringString): BaseBase(tag)
classclass FooFoo(varvar count: IntInt) : IntermezzoIntermezzo("Foo Example") {
overrideoverride funfun gimmeTheTag() = "Not the Original Tag"
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
#$.2*-&.; 1 )/#*0"#2 - 0. /# tag )( $) Intermezzo; 0. +'$) tag
*)./-0/*-+-( / -* .)*/*)9$/2$/#)$)# -$/  val +-*+ -/4; 1 )*)
/#/$/. '!$. '- .*)./-0/*-+-( / ->
#//# *(+$'  --*-.0"" ./.;/#*0"#;$./#/2 )(& tag $) Base open;
/# ) override $/$) Intermezzo=
openopen classclass BaseBase(openopen valval tag: StringString) {
openopen funfun gimmeTheTag() = tag
}
openopen classclass IntermezzoIntermezzo(overrideoverride valval tag: StringString) : BaseBase(tag)
classclass FooFoo(varvar count: IntInt) : IntermezzoIntermezzo("Foo Example") {
overrideoverride funfun gimmeTheTag() = "Not the Original Tag"
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
BASIC CLASSES
96
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N1 --$$)"-*+ -/$ .N$)/# '..**&K
)*/# -2*-.;4*0)*1 --$ +-*+ -/$ .)#)" /# $- #1$*-H2 2$''
3($) /#$.(*- (0#'/ -$)/# **&>
Chaining to Superclass Functions
$& 1;*/'$)0. . super. +- 73/*''*2* $).0'../*.+ $7''4''
$(+' ( )//$*).$).0+ -'..>4+$''44*0*/#$.$)!0)/$*)/#/4*0
*1 --* ;/* 3 0/ /# .0+ -'..$(+' ( )//$*).+-/*!/# *1 --$ )
!0)/$*)D.$(+' ( )//$*)>
openopen classclass BaseBase(valval tag: StringString) {
openopen funfun gimmeTheTag() = tag
}
classclass FooFoo(varvar count: IntInt) : BaseBase("Foo Example") {
overrideoverride funfun gimmeTheTag() = supersuper.gimmeTheTag() + "-Extended"
funfun something() {
count += 1
println("${gimmeTheTag()}: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo(0)
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N#$)$)"/*0+ -'..0)/$*).N$)/# '..**&K
 - ;/# Foo $(+' ( )//$*)*! gimmeTheTag() ''./# .0+ -'..$(+' ( )//$*)
)++ ). -Extended /*$/>.- .0'/;2 " //# *($) - .0'/.*!*/#/#
Base )/# Foo $(+' ( )//$*).*! gimmeTheTag()=
BASIC CLASSES
97
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Foo Example-Extended: something() was called 1 times
Foo Example-Extended: something() was called 2 times
Foo Example-Extended: something() was called 3 times
the final count was 3
Inheritance Tests
*( /$( .;2 ) /*&)*22# /# -- ! - ) /*.*( .0+ -/4+ $.)$)./)
*!.*( .0/4+ >
)1;0.0''42 */#$.1$ instanceof>*$!2 #1 '..#$ --#4'$& /#$.=
classclass AnimalAnimal {
// stuff
}
classclass FrogFrog extendsextends AnimalAnimal {
// froggy stuff
}
classclass AxolotlAxolotl extendsextends AnimalAnimal {
// axolotly stuff
}
?2 )0. instanceof /*. $!.*( Animal $.- ''4 Frog=
void something(AnimalAnimal critter) {
ifif (critter instanceofinstanceof FrogFrog) {
// do froggy things
}
}
)04; kind_of? *- is_a? - /# /4+$'24.*!*(+'$.#$)"/# .( /#$)"=
defdef something(critter)
ifif critter.is_a? Frog
# do froggy things
endend
endend
*/'$)#..4)/3.$($'-/*1;2$/#.#*-/ -& 42*-= is>*;2 )*/#$.=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
BASIC CLASSES
98
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critter: AnimalAnimal = FrogFrog()
ifif (critter isis FrogFrog) println("Ribbit!") elseelse println("Ummm... whatever noise an axolotl makes!")
}
J!-*( N# &$)")# -$/) $$.N$)/# '..**&K
 - ; critter $.) Animal;0/2 0. is /* / -($) 2# /# -$/$.- ''4+*$)/$)"
/*)$)./) *! Frog *-)*/>
Inheritance and whenwhen
)*/'$);2 )0. is )*/*)'4.+-/*!) 3+- ..$*);0/2 )0. $/./ ./
!*- when '0. > )- 2-$/ /# +- 1$*0. 3(+' 0.$)" when=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critter: AnimalAnimal = FrogFrog()
whenwhen (critter) {
isis FrogFrog -> println("Ribbit!")
elseelse -> println("Ummm... whatever noise an axolotl makes!")
}
}
J!-*( N$.)2# )N$)/# '..**&K
#$.$.# '+!0'$!4*0#1 . -$ .*!.0/4+ ./*# &)#)' . +-/ '4;
/#*0"#4*0(4  // -. -1 4*1 --$$)"!0)/$*).)+0//$)"/# '*"$$)/#
'.. ./# (. '1 .J >">;#1  makeSound() !0)/$*)/#/'' Animal '.. .
$(+' ( )/;.*4*0)%0./'' makeSound() *))4 Animal )" //#  .$-
- .0'/K>
Casting
1+-*"-(( -.#1 '*)"#$./*-42$/#./$)"*% /.>#$./0-).- ! - ) /*
.0+ -/4+ $)/*.0/4+ >)1;2 0. /# .0/4+ )( $)+- )/# . ./*
+ -!*-(/# ./>*;$!2 #1 /# Animal; Frog;) Axolotl /4+ ...#*2)*1 ;
BASIC CLASSES
99
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
2 ($"#/#1 ( /#*/#/'**&.'$& /#$.=
void something(AnimalAnimal critter) {
ifif (critter instanceofinstanceof FrogFrog) {
FrogFrog frog = (FrogFrog)critter;
// do something with the frog, preferably not involving boiling water
}
elseelse {
AxolotlAxolotl axolotl = (AxolotlAxolotl)critter;
// do... oh, I don't know, I'm sure you can think of something
}
}
)*/'$);./$)"" /.$/(*- $)/ - ./$)"; 0. /# *(+$' -/-$ ./*# '+
- 0 /# )0( -*!././#/4*0) >)!/;2 ''@2-$// )*/'$)* -- '4
$)1*'1 ../$)">
Manual
#/ $)".$;4*0) -/$)'4.//4+ .>)*/'$);/#/$.+ -!*-( 40.$)"
/# as & 42*-=
valval critter: AnimalAnimal = FrogFrog()
valval kermit = critter asas FrogFrog
 - ; kermit 2$'' *!/4+ Frog;.2 ()0''4.//# Animal /* Frog 1$ as
Frog>
Smart Casts
 /D."*&/*/# 1.)$++ /!*-(*( )/=
void something(AnimalAnimal critter) {
ifif (critter instanceofinstanceof FrogFrog) {
FrogFrog frog = (FrogFrog)critter;
// do something with the frog, preferably not involving boiling water
}
elseelse {
AxolotlAxolotl axolotl = (AxolotlAxolotl)critter;
BASIC CLASSES
100
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
// do... oh, I don't know, I'm sure you can think of something
}
}
)/# if '*&;2 ./ critter /*  Frog> &)*2/#$.$..! ; 0. 2
# & /# /4+ 1'$$/42$/# instanceof $)/# if / ./>
*;$! we &)*2/#/ critter $.- ''4 Frog?2#4* .)D/1B
- .0('4;/# */'$) 1 '*+ -..& /# (. '1 ./#/.*-/*!,0 ./$*);/#*0"#
+*..$'4)*/$)1*'1$)"!-*".>).$ *! if ) when '*&.;$!2 &)*2/#/1-$'
$.- ''4*!.*( .0/4+ ;/# *(+$' -' /.0..&$+/# ./.)''*2.0./*- ! -/*
/# 1-$' ./# .0/4+ ; 1 )/#*0"#$/2. '- ..0+ -/4+ >
*- 3(+' ;' /D."$1 *0-- /0- ..*( !0)/$*).;/# )''/#*. !0)/$*).=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
funfun hop() = println("Hop!")
}
classclass AxolotlAxolotl : AnimalAnimal() {
funfun swim() = println("Swish!")
}
funfun main() {
valval critter: AnimalAnimal = FrogFrog()
whenwhen (critter) {
isis FrogFrog -> critter.hop()
isis AxolotlAxolotl -> critter.swim()
}
}
J!-*( N(-/./.N$)/# '..**&K
)1@./4' 2*-';/#$.2*0')*/*(+$' > critter $.) Animal;) Animal #.
) $/# - hop() )*- swim()>*2 1 -;*/'$)- '$5 ./#//# is Frog * 2$''*)'4
  3 0/ $! critter $. Frog;.*$/''*2.0./*'' hop() *) critter;.$!
critter 2 -  '- . Frog $)./ *!.) Animal>$($'-'4;2 )''
swim() *) critter $!$/ is Axolotl>
*2 1 -;/#$.*)'42*-&.2# )/# *(+$' -$. certain *!/# /4+ >.- .0'/;/#$.
.0/'4@$6 - )/ $/$*)*!/#/* !$'.2$/#*(+$'  --*-=
BASIC CLASSES
101
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
funfun hop() = println("Hop!")
}
classclass AxolotlAxolotl : AnimalAnimal() {
funfun swim() = println("Swish!")
}
valval critter: AnimalAnimal = FrogFrog()
whenwhen (critter) {
isis FrogFrog -> critter.hop()
elseelse -> critter.swim()
}
 - ;2 - +' is Axolotl 2$/# else># */'$)*(+$' -&)*2./#/ critter $.
)*/ Frog;.*/# -2$. 2 2*0')*/  3 0/$)"/# else>*2 1 -; critter
($"#/)*/ ) Axolotl;.$/*0' )$)./) *! Animal>.- .0'/;*/'$)
))*/..0( /#/$/$..! /*'' swim() *) critter;.2#$' ) Axolotl )
swim();) Animal ))*/>*;2 " /*(+$'  --*-=
error: unresolved reference: swim
else -> critter.swim()
*" /$//**(+$' ;2 ) /*()0''4.//# critter /* ) Axolotl=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
funfun hop() = println("Hop!")
}
classclass AxolotlAxolotl : AnimalAnimal() {
funfun swim() = println("Swish!")
}
funfun main() {
valval critter: AnimalAnimal = FrogFrog()
whenwhen (critter) {
isis FrogFrog -> critter.hop()
elseelse -> (critter asas AxolotlAxolotl).swim()
}
}
BASIC CLASSES
102
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
J!-*( N)0'./.N$)/# '..**&K
0/;4*$)"/#$.;2 ($"#/-.#;$! critter $.) $/# - Frog )*-) Axolotl>*;
2#$' /#$.*(+$' .=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
funfun hop() = println("Hop!")
}
classclass AxolotlAxolotl : AnimalAnimal() {
funfun swim() = println("Swish!")
}
valval critter: AnimalAnimal = AnimalAnimal()
whenwhen (critter) {
isis FrogFrog -> critter.hop()
elseelse -> (critter asas AxolotlAxolotl).swim()
}
?$/-.# ./-0)/$( ;.0#./#$. --*-( .." !-*(*/'$)G=
java.lang.ClassCastException: Test$Animal cannot be cast to Test$Axolotl
at Test.<init>(Unknown Source)
*/'$)D.E.(-/./.FH./#$.0/*(/$./$)"$.'' H)*/*)'4.1 .4*0
/4+$)";0/$/'.*# '+.4*01*$$(+-*+ -()0'./.>)" ) -';$!4*07)
4*0-. '!0.$)" as $)*/'$);4*0.#*0' .&$)"4*0-. '!=$!/# */'$)*(+$' -
* .)*/&)*2/#//#$.*% /$.*!/#$..0/4+ ?#*2* I &)*2/#//#$.*% /
2$'''24. *!/#$..0/4+ B
BASIC CLASSES
103
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Comments and Documentation
*2/#/2 - ./-/$)"/*.. (' */'$)'.. .;$/$./$( /*- ../# /2*
/#$)"./#/ 1 '*+ -.. (/*$.'$& (*./=/ ./$)")*0( )//$*)>
*24*02-$/ / ./.!*-4*0-*/'$)* 2$'' + )'*/*)/#  )1$-*)( )/$)
2#$#4*0- -0))$)"/#/* >*;!*- 3(+' ;)-*$++ 1 '*+ -.0.$)"
*/'$)2$''./$''2-$/ )$// ./.;%0./0.$)"*/'$)$)./ *!1>.- .0'/;/# - $.
'$//' /#/**&*)/# */'$) language )/ ''4*0*0// ./$)"*/'$);2#$#$.
2#44*02$''. '$//' *)/#$./*+$# - >
*2 1 -;2# )$/*( ./**0( )/$)"4*0-*/'$)* ;*/'$).0++*-/.*
*(( )/.;(0#'$& (*./+-*"-(($)"')"0" .>);%0./.1#.1*.
)04#.*).*!*-/#;*/'$)#.*!*-- /$)"*(( )/./#/)
 /0-) $)/**0( )//$*)>
Basic Comment Syntax
*/'$)*+/ 1*(( )/.4)/3;2#$##++ )./*'$) 0+2$/#1-$+/
*(( )/.4)/3>
// $)$/ ./#//# - ./*!/# '$) $.*(( )/=
valval foo = 0
// this is a comment
// so is this
/* ) */ - 0. /* '$($/(0'/$+' '$) .. $)"*(( )/=
105
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
valval foo = 0
// this line has executable code
/*
val bar = 0 // this line does not, as it is wrapped in a comment
*/
# $"" ./H)+ -#+.*)'4H$6 - )  /2 )*/'$)*(( )/.)2#/
4*0($"#/*$)1$./#/(0'/$@'$) *(( )/.) ) ./ $)*/'$)>*;!*-
3(+' ;/#$.$.1'$$)*/'$)=
/*
It's
/*
comments
/*
all
/*
the
/*
way
/*
down!
*/
*/
*/
*/
*/
*/
Introducing KDoc
!''/#/4*02)//**$. 3+')/$*)./*4*0-* ;/* - .+-/*!/#/
* ;/# *(( )/.4)/3.#*2)*1 $.''/#/4*0) >
!4*02)//* ' /*" ) -/ ./)'*) *0( )//$*)!*-4*0-* ;/# 24
/#/1*.;*;).$($'-/**'.*?*/'$)D. ,0$1' )/$.*>
Basic KDoc Comments
) "$);*/'$)0. .1D.++-*#) 3/ ).$/>*;%0./.1**(( )/.
./-/2$/# /** ) )2$/# */;.*/*****(( )/.=
COMMENTS AND DOCUMENTATION
106
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
/**
* This is the summary of your Kotlin class. It should explain, in a single
* paragraph, the role of this class in your project.
*
* Additional paragraphs after the first one should explain your class in
* greater detail. Of course, this class is extremely simple. It does not
* need much in the way of explanation. Yet, for some reason, the author
* insists on keeping on typing text into this paragraph, as if his hand is
* guided by some unseen force. Or, perhaps, he is just trying to pad the
* paragraph out a bit.
*
* Oh, by the way, the line of asterisks that you see on the left is optional,
* as you will see in the function comment below.
*/
classclass FooFoo {
/**
This should explain what this function does.
Um, that's it!
*/
funfun something() {
println("Hello, world!")
}
}
Formatting
# - *- &.!-*(1*$.$)/ -(.*!/ 3/!*-(//$)">*0. .
-&*2);-"0'4/# 2*-'D.(*./+*+0'-E2$&$/ 3/F(-&0+')"0" >
1 -4/#$)"!-*(/&1 -9*2,0 ./$*)./*$/0 README 7' .- 2-$// )$)
-&*2)>#$.**&$.2-$// )$)-&*2)>)*' /.4*00. -&*2)
!*-(//$)"$)4*0-*(( )/.>
*;4*0)0. /#$)".'$& =
I **Boldface** J- ) -..= BoldfaceK
I *Italics* J- ) -..= ItalicsK
I &/$&.2-++ -*0)/ 3//* format it in monospace
I ).**)
Cross-Referencing
#/-&*2)'&.$.24/*'$)&/*/#$)".$).$ /# .( *0( )/>/.
COMMENTS AND DOCUMENTATION
107
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#4+ -'$)&.4)/32*-&."- /!*-'$)&./**/# -+" .;0/)*//*(/ -$'$)/#
0-- )/+" >
* 3/ ).-&*2)'$)&$)".4)/3;''*2$)"4*0/*- ! - ) +-*+ -/$ .;
!0)/$*).;).$($'-)( /#$)".40.$)".$(+' -& /.4)/3=
/**
* This is the summary of your Kotlin class. It should explain, in a single
* paragraph, the role of this class in your project.
*
* This class has a single function: [something].
*
* You can also link to other classes, such as [kotlin.String], using the
* fully-qualified class name.
*
* And, of course, you can [link to external content](https://commonsware.com).
*/
classclass FooFoo {
/**
This should explain what this function does.
This function is inside of [Foo], as you can tell by looking up a few lines
from where you are reading right now.
*/
funfun something() {
println("Hello, world!")
}
}
Block Tags
.2$/#1*;*.0++*-/.E'*&/".F!*-*0( )/$)".0@ ' ( )/.*!'..;
!0)/$*); />*- 3(+' ;4*0)0. @param /**0( )/!0)/$*)+-( / -;
@return /**0( )/!0)/$*)- /0-)1'0 ;).*!*-/#=
/**
* This is the summary of your Kotlin class. It should explain, in a single
* paragraph, the role of this class in your project.
*
* This class has a single function: [something].
*
* You can also link to other classes, such as [kotlin.String], using the
* fully-qualified class name.
*
* @author Mark Murphy
COMMENTS AND DOCUMENTATION
108
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*/
classclass FooFoo {
/**
This should explain what this function does.
This function is inside of [Foo], as you can tell by looking up a few lines
from where you are reading right now.
@param msg the message to be printed
@return `true`, because, hey, why be negative?
*/
funfun somethingMoreElaborate(msg: StringString = "Hello, world!"): BooleanBoolean {
println(msg)
returnreturn truetrue
}
}
*(+' / '$./*!'*&/".$.1$'' $) /# */'$)*0( )//$*)>
Generating the Documentation
0./.1#. javadoc +-*"-(/*" ) -/ 1*.;)%0./.04#. rdoc
/*" ) -/ *0( )//$*)!-*(**(( )/.;*/'$)#..$($'-/**'= *&&>
*./'$& '4;4*02$''" ) -/ 4*0-*0( )//$*)0.$)"/# 0$'.4./ (!*-4*0-
+-*% /;.0#.-' !*-))-*$+++-*% /># - $.24/* " ) -/ /#
*0( )//$*)!-*(/# *(()'$) ;/#*0"#*&&* .)*/. (/*
*+/$($5 !*-/#/0. . >
COMMENTS AND DOCUMENTATION
109
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Properties
) /# #+/ -*)'.. .;2 .2/#/2 )#1 +-*+ -/$ .$)*0-*/'$)'.. .;
- ($)$. )/*!#1$)"7 '.$)1=
classclass FooFoo {
varvar count = 0
funfun something() {
count += 1
println("something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo()
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N$)"-*+ -/$ ./*'.. .N$)/# '..**&K
))0/.# '';4*0)#1 var@) val@' ' +-*+ -/$ .$)4*0-'.. .;%0./'$&
4*0)*0/.$ *!'.. .>-*+ -/$ .$)'.. .-  ..$' 4$)./) .*!/#*.
'.. .;%0././# something() !0)/$*)$) Foo #. ../*/# count +-*+ -/4>
*!-;/#$.$.0)- (-&' ;)4*0($"#/2*) -2#4/# - $.2#*' #+/ -
 $/ /*+-*+ -/$ .>#/$. 0. +-*+ -/$ .$)*/'$)#1 )0( -*!
! /0- ./#/"* 4*)/# $- ,0$1' )/.$)*/# -+*+0'-+-*"-(($)"')"0" .>
# . ! /0- .- *((*)'40. $)*/'$) 1 '*+( )/;).*$/$.$(+*-/)//*
111
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
0) -./)2#//# 4- )#*24*00. /# (>
Initialization
-*+ -/$ .) /* $)$/$'$5 /.*( +*$)/>*/'$)"$1 .4*0.*( *((*)
*+/$*).;).*( ' ..@*((*)*+/$*).;!*-*$)"/#$.>
When Declared
0#*!/# /$( ;4*02$''$)$/$'$5 +-*+ -/$ .2# )4*0 '- /# (>-$(-$'4;
.0#$)$/$'$5/$*)2$''- ! -/*'$/ -'.;*)./-0/*-+-( / -.;+-*+ -/$ . '- 
*0/.$ *!'.. .;)+ -#+. *)./)/.># 4)'.*- ! -/**/# -+-*+ -/$ .
/#//# (. '1 .- $)$/$'$5 >
 - ;2 $)$/$'$5 count . *) sillyCount *)./-0/*-+-( / -=
classclass FooFoo(sillyCount: StringString) {
varvar count = sillyCount.toInt()
funfun something() {
count += 1
println("something() was not called $count times")
}
}
funfun main() {
valval foo = FooFoo("7")
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N-*+ -/4)$/$'$5/$*)$) '-/$*)N$)/# '..**&K
In an Init Block
*0)'.*$)$/$'$5 +-*+ -/$ .$)) init '*&>) /# #+/ -*)'.. .;2 .2
/#$. 3(+' =
PROPERTIES
112
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
classclass FooFoo(valval sillyCount: StringString) {
varvar count = 0
init {
count = sillyCount.toInt()
}
funfun something() {
count += 1
println("something() was not called $count times")
}
}
funfun main() {
valval foo = FooFoo("7")
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N)$/'*&N$)/# '..**&K
 - ;2 $)$/$'$5 count /* 0;/# )- +' $/$(( $/ '42$/#1'0  -$1 !-*(
sillyCount>.$//0-).*0/;/# $)$/$'$5/$*)$.)*/- ,0$- >#$.$.%0./.1'$)
"$1 ./# .( - .0'/.=
classclass FooFoo(sillyCount: StringString) {
varvar count: IntInt
init {
count = sillyCount.toInt()
}
funfun something() {
count += 1
println("something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo("7")
foo.something()
PROPERTIES
113
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N-*+ -/4)$/$'$5/$*)$))$/'*&N$)/# '..**&K
Sometime Later
/(4 /#/4*0*)*/&)*2#*2/*$)$/$'$5 /# +-*+ -/4)*2;0/4*02$''
' /*$)$/$'$5 $/'/ ->*- 3(+' ;$))-*$++ 1 '*+( )/;/# - 2$''
+-*+ -/$ ./#/4*0))*/$)$/$'$5 0)/$'.*( +-/$0'-+*$)/$))*% /D.
'$! 4' ;.0#.$) onCreate() *!) Activity *- Fragment>
# (*./*((*)24*!#)'$)"/#$.$.1$ lateinit> lateinit $.+-*($. /#/
4*02$'')*/// (+//*- ! - ) +-*+ -/40)/$'4*0$)$/$'$5 $/E'/ FJ$> >;
.*( /$( !/ -/# *% /$.$)$/$'$5 K=
classclass FooFoo {
varvar count = 0
lateinitlateinit varvar label: StringString
funfun defineTag(tag: StringString) {
label = tag
}
funfun something() {
count += 1
println("$label: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo()
foo.defineTag("Foo")
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N'/ $)$/-*+ -/$ .N$)/# '..**&K
PROPERTIES
114
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
 - ;2 #1 */# count ) label;2# - 2 0. */#$)/# +-$)/ *0/+0/>
*-.*( - .*);2 *)*/#1 /# 1'0 !*- label //# /$( 2 - / )
$)./) *! Foo>*;2  7) label . lateinit;.*2 ).&$+/# $)$/$'$5/$*)>
# $(+'$ - ,0$- ( )/$./#/ defineTag() (0./ ''  !*- something()>
) /*$)$/$'$5 label  !*- 2 )- ! -/*$/.! '4>$'$)"/**/#$.H.0#
.4*(( )/$)"*0//# foo.defineTag("Foo") '$) $)/# .(+' H- .0'/.$)
-.#=
kotlin.UninitializedPropertyAccessException: lateinit property label has not been
initialized
# - +*..$' ;4*0.#*0'/-4/*1*$ lateinit;.$/$. .4/*(& ($./& .)
/-40.$)"/# lateinit +-*+ -/4 !*- $/$.$)$/$'$5 >
'.*)*/ /#/ lateinit ))*/ 0. !*-/4+ .. *)+-$($/$1 .;.*4*0))*/
#1  lateinit Int; Boolean; />
Eh, Whenever
/)*/# -+*..$$'$/4$./*0. by lazy=
classclass FooFoo(valval rawLabel: StringString) {
varvar count = 0
valval label: StringString byby lazy { println("initializing via lazy!"); rawLabel.toUpperCase() }
funfun something() {
count += 1
println("$label: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo("foo")
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N'54-*+ -/$ .N$)/# '..**&K
 - ; label $.$)$/$'$5 '5$'4># ) 1 -2 7-./// (+//*0. label;/# '(
3+- ..$*)2$''  3 0/ ;)/#/1'0 $.2#/2 . ./# 1'0 *! label>)
/#$.. ;/# '( 3+- ..$*)/& .*)./-0/*-+-( / -)*)1 -/.$/$)/*
''+.;.**0-*0/+0/$.=
PROPERTIES
115
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
initializing via lazy!
FOO: something() was called 1 times
FOO: something() was called 2 times
FOO: something() was called 3 times
the final count was 3
)/-0/#;2 2*0')*/) /*0. by lazy # - >$) /# *)./-0/*-+-( / -$.
1$'' /$)$/$'$5/$*)/$( ;)$/$.)*/#)"$)";2 *0'%0./$)$/$'$5 label
)*-(''4=
classclass FooFoo(valval rawLabel: StringString) {
varvar count = 0
valval label = rawLabel.toUpperCase()
funfun something() {
count += 1
println("$label: something() was called $count times")
}
}
valval foo = FooFoo("foo")
foo.something()
foo.something()
foo.something()
.2$/# lateinit var; by lazy $."**!*-. .2# - 4*0))*/$)$/$'$5 /# val
0+!-*)/;!*-2#/ 1 -- .*)J >">; onCreate() #.)*/4 / )'' *)4*0-
)-*$ ActivityK>);.2$/# lateinit var;4*0-0)/# -$.&*!-.#$)"$!/#
*)$/$*).- )*/- 4!*-4*0-'( 3+- ..$*)/*  1'0/ >
# $6 - ) (*0)/./*E+0.#F1 -.0.E+0''F>$/# lateinit var;2 /-4/*
$)$/$'$5 /# +-*+ -/4..**).$/$.+-/$';+0.#$)"1'0 $)/*$/>$/# by
lazy;2 #*+ /#/2 *)*//-4- ! - )$)"$/ !*- /# '( 3+- ..$*)2$''
2*-&?0/*) $/ is - ! - ) ;2 E+0''F$/.$)$/$'1'0 !-*(*/# -/;0.$)"/#
'( 3+- ..$*)/* -$1 /# 1'0 /#/2 2)//*0. >
by lazy $.*)  3(+' *!-* -+// -)*!+-*+ -/4 ' "/ .;2#$#2 2$''
3+'*- (0#'/ -$)/# **&>
Wait a Minute! Was That… a Semicolon?
0- label +-*+ -/4)*/*)'4# lazy +-*+ -/4 ' "/ ;0//# lazy '(
3+- ..$*)#--$/4$)*/'$)=. ($*'*)>
PROPERTIES
116
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
.2$/#1;. ($*'*)$)*/'$). -1 ...// ( )// -($)/*->*2 1 -;$)
*/'$);) 2'$) also ). -1 ..// ( )// -($)/*-;2#$#$.2#42 -- '4
. . ($*'*).0. $)/#/-*' >)/#$.. ;/# lazy '( 3+- ..$*)*)/$).
/2*.// ( )/.=*) /*'*"( .." ( )/$*)$)"/#//# '( 3+- ..$*)2.
 $)" 1'0/ ;)*) /**)1 -//# ./-$)"/*0++ -. >
Fake Properties
)*/# -24/*$)$/$'$5 +-*+ -/4?$./*#1 $/*)'4'**&/**0/.$ -.'$& $/$.
- "0'-+-*+ -/4>
0.0''4$./$)"0$.# /2 ) ..$)"+-*+ -/4)''$)"!0)/$*)4/#
3$./ ) *!+- )/# . .> anObject.someProperty - ! - ) .+-*+ -/4;2#$'
anObject.thatFunction() ''.!0)/$*)>
)- '$/4;*/'$)$.)*/) -'4/#/.$(+' >/$.+*..$' /*''!0)/$*).2$/#*0/
0.$)"+- )/# . .H forEach;!*- 3(+' ;$.!0)/$*)> 4*)/#/;/# - -
24./* 7) +-*+ -/$ ..0#/#//# 4- ''4- $(+' ( )/ 4!0)/$*).>
2$'' 3+'*- /#$.24*!. //$)"0+E!& +-*+ -/$ .F(*- (0#'/ -$)/# **&>
Constants
*/'$)#. const & 42*-;2#$#4*0)0. /* '-  -/$) val +-*+ -/$ ..
*)./)/.>#$.$..*( 2#/- ($)$. )/*! final $)1>
#$.(4. ($/*>!/ -'';4()4 7)$/$*).; val already $.*)./)/>
*0))*/#)" $/.1'0 ;!/ -''>
# - - .*( - .*).!*-#1$)" const;/#*0"#;0/2 2$''#*'*6*)/#*. 0)/$'
(0#'/ -$)/# **&>*-)*2;$!2#$' - $)"*/'$)* 4*0. /# const
& 42*-;%0./$("$) /#/$/( ).E)*; really;( )/#//#$..#*0' 
*)./)/F>
Getting Down in the Weeds
)1;2 #1 7 '.=
PROPERTIES
117
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
classclass FooFoo {
privateprivate int myField;
}
(& /# *(+-$.*)/**/'$)+-*+ -/$ .;0//#/$.*)'4)++-*3$(/$*)>
)1;.*( /$( .2  7) E" // -F)E. // -F( /#*.;).*( /$( ./#*.
# - /*E1 )F)($)"*)1 )/$*).=
classclass FooFoo {
privateprivate int myField;
int getMyField() {
returnreturn myField;
}
void setMyField(int newValue) {
myField = newValue;
}
}
*/'$)+-*+ -/4- ''4- +- . )/./# *($)/$*)*!/# . /#-  ' ( )/.=
I 7 '
I " // -!0)/$*)
I . // -!0)/$*)
/.*#++ )./#/!*-.$(+' +-*+ -/$ .H'$& /#*. 2 #1 . )$)/#$.#+/ -
)+- 1$*0.'4H/#*. /#-  ' ( )/.#1  !0'/$(+' ( )//$*)./#/-
#$ ) #$)/# .$(+' var@./4'  '-/$*)># )2 - +-*+ -/4D.1'0 ;
2 - ''4- ''$)"" // -!0)/$*)/#/- /0-)./# 1'0 *!/# +-*+ -/4D.
E&$)"7 'F># )2 2-$/ +-*+ -/4D.1'0 ;2 - ''4- ''$)". // -
!0)/$*)/#/. /./# 1'0 *!/# &$)"7 '>
*( 1) */'$).4)/3H.0#./# E!& +-*+ -/$ .F''0 /* -'$ -H
/& 1)/" *!/#$.;*1 --$$)"/#  !0'/" // -)G*-. // -/**.*( /#$)"
'. >
*-(*./*/'$)+-*"-(($)";/#*0"#;/#$.$.E$./$)/$*)2$/#*0/$6 - ) F>
-*+ -/4.4)/3- . (' . ..$)"17 '.;.*2 /#$)&*!+-*+ -/$ .. $)"
,0$1' )//*7 '.>*2 1 -;$!4*0'/ -1) /*/# EBF#+/ -.*!/#$.
**&;2 2$''- /0-)/*/#$.*) +/*!+-*+ -/$ .- ''4 $)"7 '.2$/#" // -G
. // -!0)/$*).>
PROPERTIES
118
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Visibility and Scope
*2/#/2 - ./-/$)"/* 7) '.. .2$/#+-*+ -/$ .)!0)/$*).;2 ) /*
./-//#$)&$)"*0/E2#*). 2#/FH2#/+-/$0'-.+ /.*!*0-* -
 ..$' 4*/# -.+ /.*!*0-* >-$)$+''4;/#/(*0)/./*/2*/#$)".=
S> $.$$'$/4=#/$.''*2 /*. /# . '.. .;!0)/$*).;)+-*+ -/$ .B
T> *+ =# - */# . '.. .;!0)/$*).;)+-*+ -/$ ./0''4 3$./B
Visibility
)4+-*"-(($)"')"0" .#1 1$.$$'$/4*+/$*).;/**)/-*'2#/*% /.)
- ! - ) !0)/$*).)+-*+ -/$ .!-*(*/# -*% /.>)*/#1)04;!*-
3(+' ;4*0)#1 +0'$;+-$1/ ;)+-*/ / ( /#*.>*/'$)#./#*. /**;
/#*0"#*/'$)# - .$/'*. -/*1D. 7)$/$*)*!/#*. / -(./#)04D.>
*/'$)'.*#.) internal 1$.$$'$/4*+/$*)/#/$.'$//' ./-)" >
Default = Public
).*( ')"0" .; 1 -4/#$)"$.+0'$4 !0'/>)4*% /)- ! - ) +0'$
/#$)".>)1;/#*0"#; 1 -4/#$)"$.E+&" @+-$1/ F4 !0'/;( )$)"/#/$/$.
+0'$/*/#$)".$)/# .( 1+&" ;0/+-$1/ /*/#$)".$)*/# -+&" .>
*/'$)/& ./# +0'$@4@ !0'/-*0/ >*02$'')*/.  public & 42*-0. $)
/4+$'*/'$)>1 -4/#$)"/#/$. 7) 4 !0'/$.1$.$' /*)4/#$)" '. >#$.$.
2#4* '$& /#$.H. )$) /# #+/ -*)'.. . H/0''42*-&.=
classclass FooFoo {
funfun something() {
println("Hello, world!")
119
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
}
funfun main() {
valval foo = FooFoo()
foo.something()
}
J!-*( N$)"0)/$*)./*'.. .N$)/# '..**&K
*/# Foo ) something - +0'$;).** /#/'$1 .*0/.$ *! Foo )
- ! - ) Foo J!*-- /$)"$)./) .K) something() J!*-''$)"/#/!0)/$*)*)
$)./) .*! FooK>
Private
private;4*)/-./;'$($/. ../*$)./) .*!$/.'..;))*/#$)" '. >
classclass FooFoo {
privateprivate funfun something() {
println("Hello, world!")
}
}
valval foo = FooFoo()
foo.something()
#$.$./# .( ./# +- 1$*0..)$++ /; 3 +//#/ something() $.(-& .
private>#$.2$'')*/*(+$' ;. something() $.)*/1$.$' /** *0/.$ *! Foo=
error: cannot access 'something': it is private in 'Foo'
foo.something()
-$1/ +-*+ -/$ .)!0)/$*).) - ! - ) 4*/# -+-*+ -/$ .)!0)/$*).
2$/#$)/# .( '..=
classclass FooFoo {
privateprivate funfun something() {
println("Hello, world!")
}
funfun somethingElse() {
something()
VISIBILITY AND SCOPE
120
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
}
funfun main() {
valval foo = FooFoo()
foo.somethingElse()
}
J!-*( N+-$1/  ( -.N$)/# '..**&K
#$.2*-&.; 0. 2 - ''$)" somethingElse();2#$#$.+0'$J4 !0'/K>
#$' * *0/.$ *! Foo ))*/'' something(); somethingElse() )''
something();.*/# something() ) somethingElse() - +-/*! Foo>
Protected
# protected 1$.$$'$/4(*$7 -2*-&.'*/'$& private;0/$/'.*''*2.
.0'.. ./* ../# +-*+ -/4*-!0)/$*)=
openopen classclass FooFoo {
protectedprotected funfun something() {
println("Hello, world!")
}
}
classclass BarBar : FooFoo() {
funfun somethingElse() {
something()
}
}
funfun main() {
valval notFoo = BarBar()
notFoo.somethingElse()
}
J!-*( N+-*/ /  ( -.N$)/# '..**&K
#$.2*-&.; 0. somethingElse() $.+0'$J4 !0'/K;) Bar ) ../#
protected !0)/$*)$).$ *! Foo>*2 1 -;* *0/.$ *! Foo ) Bar ))*/
 ../#/ protected !0)/$*);).*/#$.!$'.=
VISIBILITY AND SCOPE
121
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
openopen classclass FooFoo {
protectedprotected funfun something() {
println("Hello, world!")
}
}
classclass BarBar : FooFoo() {
funfun somethingElse() {
something()
}
}
valval notFoo = BarBar()
notFoo.something()
What About Top-Level Functions and Properties?
*!-;2 #1 '**& /#*2 private ) protected 2*-&2$/#.+ /.*!'.. .=
+-*+ -/$ .)!0)/$*).>
)/# . *! protected;/#/$./# *)'4+' 4*0)0. /#/+-/$0'-1$.$$'$/4>
protected $. 7) .E ..$' 4/#$.'..)$/..0'.. .F;.*$/*)'4(& .
. ). $)/# *)/ 3/*!'.. .>
private;/#*0"#;$.)*/%0./1$'' !*-.+ /.*!'.. .>'.. ./# (. '1 .)
private>-*+ -/$ .)!0)/$*). 7) *0/.$ *!'.. .)'.* (-&
. private>)/# . . .;/#  / -($)/$*)*!2#/))))*/ ../#
private $/ (.$.. *)/# *le=
I private /*+@' 1 '/#$)".- 1$.$' /**/# -/#$)".$)/# .( */'$).*0-
7'
I private /*+@' 1 '/#$)".- $) ..$' /**/# -/#$)".$)*/# -*/'$)7' .
). )-$*; 1 -4/#$)"$)/# D. $/*-$.$).$)"' 7' ;).* private
#.'$//' ( )$)"> private #.'*/(*- ( )$)"$)/-$/$*)'.*!/2- +-*% /;
2# - 4*0($"#/#1 *5 ).*-#0)- .*!.*0- 7' .># - ;4*0)0.
private !*- ..*)/-*'=
I )4/#$)"' !/.+0'$J/#  !0'/K$. ..$' 4*/# -7' .
I )4/#$)" protected $. ..$' *)'44.0'.. .$(+' ( )/ $)*/# -
7' .
VISIBILITY AND SCOPE
122
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I )4/#$)"(-& . private $.$) ..$' $)*/# -7' .
What About Package-Private?
# '*. .//#$)"/#/1#./*/#$..*-/*! private@/*@/# @7' -0' $.$/. !0'/
E+&" @+-$1/ F1$.$$'$/4>4 !0'/;)4'..;( /#*;*-7 '$.1$.$' /**/# -
* $)/# .( 1+&" ;. / -($) 4/# package .// ( )/>* $)
*/# -+&" .))*/ ../#/+&" @+-$1/ * >
*/'$)* .)*/*6 -+&" @+-$1/ 1$.$$'$/4>#$' +&" .) 0. !0'!*-
* *-")$5/$*);)(4 $(+*-/)/2# ) $)/ -*+ -/$)"2$/#1* ;*/'$)
* .)*/0. /# (/**)/-*'1$.$$'$/4>
What About Internal?
# - $.!*0-/#1$.$$'$/4*+/$*)$)*/'$);''  internal>
*-(*./.$(+' +-*% /.; internal $.0. ' ..)$. ,0$1' )//* $)"+0'$>
# - internal *( .$)/*+'4$.2# )4*0-+-*% /$.$1$ $)/**'' /$*)*!
.*( /4+ *!E(*0' .F>*- 3(+' ;$)))-*$+-*% /;4 !0'/4*0#1 
.$)"' (*0' JappK;0/4*0) ' //* 7) $/$*)'(*0' .># +- $.
 7)$/$*)*!E(*0' F + ). )/$- '4*)2# - /# */'$)* $. $)"0. J >">;
))-*$+-*% /K)$.)*/ 7) 4/# ')"0" $/. '!>
#/ is  7) $.2#/ internal ( ).=)4/#$)"(-& . internal $.1$.$' /*
* $)/# .( (*0' 0/$.)*/1$.$' /** $)*/# -(*0' .>
#$.''*2.4*0/* // - 7) /# /#/(*0'  3+*. ./**/# -(*0' .>
)4/#$)"/#/4*0(-&. internal $.1$.$' 2$/#$)4*0-(*0' ;.*4*0)0. $/
2$/#*0/$..0 ;0/*/# -(*0' ..#*0')*/ ' /* ..$/>)'4/# +0'$
) protected /#$)".2$'' 1$.$' /**/# -(*0' .J) protected $.1$.$' *)'4
/*.0'.. .K>
*;$!4*0+')*)+0'$.#$)".*( !*-(*!*/'$)'$--4; internal 2$''
$(+*-/)/>)'-" +-*% //#/4*0#1 $1$ $)/*(*0' .; internal )
0. !0'>*-.(''.$)"' @(*0' +-*% /.; internal $.+*$)/' ..>)!*-.;
internal $.%0./.$''4>
VISIBILITY AND SCOPE
123
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Scope
$.$$'$/4& 42*-.*)/-*'2#*). 2#/>
*+ *)/-*'.2# - /#$)". 3$./>#$.#.)$)$- /$(+/*)1$.$$'$/4=$!
.*( /#$)"* .)*/ 3$./;$/))*/  .. 4)4/#$)">
*2 1 -;.*+ (*- $- /'4*)/-*'./#$)".'$& "-" *'' /$*);..*+ # '+.
/* / -($) /# '$! /$( *!*% /.>
Global
*+@' 1 ' 7)$/$*).$)*/'$).*0- 7' - "'*'$).*+ >((0/' "'*'.H
'.. .; val; />H2$'' 3$./!*-/# '$! *!/# +-* ../#/$. 3 0/$)"/# */'$)
* >0/' "'*'.H.0#. var H#1 - ! - ) ./#/2$'''$1 !*- 1 -;0//#
*% /./#//#*. - ! - ) .- ! - ) ($"#/*( )"*./# 1'0 *!/#/
- ! - ) #)" .>
*;$)/#$.* ;*/#/# Foo '..)/# foo 1-$' - "'*')$((0/' =
classclass FooFoo {
funfun something() {
println("Hello, world!")
}
}
valval foo = FooFoo()
foo.something()
varvar bar = FooFoo()
bar.something()
bar = FooFoo()
bar.something()
bar /**$.*).$ - /* "'*'># $6 - ) $./#/ bar $.(0/' >*2#$'
)4/#$)"/#/ bar +*$)/./*))*/ "-" *'' / ;*) bar $.+*$)/$)"/*
.*( /#$)" '. J >">;)*/# -$)./) *! FooK;2#/ 1 - bar used /*+*$)//*)
"-" *'' / >
VISIBILITY AND SCOPE
124
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Instance
-*+ -/$ . 7) $).$ *!'..#1 E$)./) F.*+ >#$)./) *!/#
)'*.$)"'..2$''#1 $/.*2). /*!$)./) +-*+ -/$ .;$) + ) )/*!)4*/# -
$)./) *!/#/.( '..>
classclass FooFoo(valval count: IntInt)
funfun main() {
valval instanceOne = FooFoo(1)
valval instanceTwo = FooFoo(2)
println(instanceOne.count)
println(instanceTwo.count)
}
J!-*( N)./) *+ N$)/# '..**&K
 - ; #$)./) *! Foo #.$/.*2) count +-*+ -/42$/#$/.*2)$) + ) )/
1'0 >
Local
)4 val *- var 2$/#$).*( /#$)".('' -/#)'..$..*( !*-(*!'*'1-$' >
Local to Functions
val *- var $- /'4$).$ *!!0)/$*)*4$.'*'/*/#/!0)/$*)=
classclass FooFoo {
funfun something(message: StringString) {
valval length = message.length
println("'$message' has $length characters")
}
}
funfun main() {
FooFoo().something("this is a test")
}
J!-*( N0)/$*)@*'*+ N$)/# '..**&K
 - ; length $.*).$ - /* '*'/*/# something() !0)/$*)>*02*0'
VISIBILITY AND SCOPE
125
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
0)' /* .. length !-*(*/# -!0)/$*).*) Foo;' /'*) !-*(*0/.$ *! Foo>
Local to Blocks/Lambdas
1-$'  7) 2$/#$)'*&*-'(J$> >;#0)&*!* $) {}K2$'' '*'
/*/#/'*&*-'(=
classclass FooFoo {
funfun something(messages: ListList<StringString>) {
messages.forEach { message ->
valval length = message.length
println("'$message' has $length characters")
}
}
}
funfun main() {
FooFoo().something(listOf("this", "is", "a", "test"))
}
J!-*( N'*&@*'*+ N$)/# '..**&K
*2 length $.)*/'*'/* something();0/$)./ $.'*'/*/# '(
3+- ..$*).0++'$ /* forEach()>*0))*/ .. length !-*( '. 2# - 2$/#$)
something();' /'*) !-*(*/# -( /#*.*) Foo *-!-*(*0/.$ *! Foo>
VISIBILITY AND SCOPE
126
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Abstract Classes and Interfaces
*!-;2 #1 '**& /*-$)-4'.. .;.*( /$( .- ! -- /*.E*)- / F
'.. .>*0)#1 '.. .$)#$ --#4;2# - ) open '..)  3/ ) 4
.0'.. .>*2 1 -;2#$' '..) '- 2#/$. open /* *1 --$ );
*)- / '..))*/./$+0'/ .*( /#$)"/#/ must  $(+' ( )/ $).0'..>
#$' .0'..) ' //**1 --$ ) open !0)/$*);$/* .)*/#1 /**.*>
./-/'.. .)$)/ -! .*6 -/2*24./*+-*1$ E*)/-/.F /2 )'.. .
)/# $-.0'.. .># . 2*-&.$($'-/*/# $-1*0)/ -+-/.;''*2$)"4*0/*
 '- !0)/$*).2$/#)* !0'/$(+' ( )//$*);2# - /# .0'..$.- ,0$- 4
/# *(+$' -/*+-*1$ )$(+' ( )//$*)>
The Objective: Contracts
*( /$( .;2 2)//* ' /*" /$)!*-(/$*)*0/)*% /;0/2# - /#
/4+ *!/# *% /* .)*/#1 24*!&)*2$)"/#/$)!*-(/$*)>
*- 3(+' ;' /D."*&/*/# '..#$ --#4/#/2 .2$) /# #+/ -*)
'.. .;$)"$)*) (*- .+ $ .=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
classclass KomodoDragonKomodoDragon : AnimalAnimal()
($"#/2)//*&)*2$!) Animal #."$''.># /# -)4"$1 )/4+ *! Animal #.
"$''. + ).+-/'4*)/# .+ $ .)+-/'4*)$-0(./) .=
127
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I *(**-"*) #.)*"$''.
I )3*'*/'#. 3/ -)'"$''.
I !-*"(4#1  3/ -)'"$''.-$ 94!/ -#/#$)";0//# 4" /.*- 
&$)/*/# *4.#*-/'4/# - !/ -
.- .0'/;2 ))*/$(+' ( )/ hasGills() !0)/$*)*- hasGills +-*+ -/4*)
Animal 1 -4 .$'4?0)' ..2 0. ./-/'.. .*-$)/ -! .># );2 ).&)
Animal $!$/#."$''.;0//# /0'$(+' ( )//$*)2*0'  ' "/ /* Frog;
Axolotl;*- KomodoDragon>
Abstract Classes
)./-/'..#./# abstract & 42*-=
abstractabstract classclass AnimalAnimal {
abstractabstract funfun hasGills(): BooleanBoolean
}
)./-/'..$.0/*(/$''4 open .2 ''H2 *)*/) /# open & 42*-
/* ' /*- / .0'.. .>
.2$/#1;2 ))*/- / $)./) .*!)./-/'..>!2 /-4/**.*=
abstractabstract classclass AnimalAnimal {
abstractabstract funfun hasGills(): BooleanBoolean
}
valval critter = AnimalAnimal()
?2 !$'2$/#*(+$'  --*-=
error: cannot create an instance of an abstract class
val critter = Animal()
^
Abstract Members
)/# )./-/ 7)$)"( ( -.*!/# '../#/'.*#1 /# abstract
& 42*->*./'4;/#/2$'' ./-/!0)/$*).=
ABSTRACT CLASSES AND INTERFACES
128
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
abstractabstract classclass AnimalAnimal {
abstractabstract funfun hasGills(): BooleanBoolean
}
)./-/'..)#1 *-$)-4!0)/$*)..2 ''H4*0- )*/'$($/ /*%0./
*) .2$/#/# abstract & 42*->-$)-4!0)/$*).)/# (. '1 . open *-)*/;
.4*0. 7/=
abstractabstract classclass AnimalAnimal {
abstractabstract funfun hasGills(): BooleanBoolean
openopen funfun displayName(): StringString = "Animal"
funfun description() = "${displayName()}: hasGills = ${hasGills()}"
}
/$.'.*+*..$' /*- / ./-/+-*+ -/$ .;/#*0"#2 2$''#*'*6*)/#$.0)/$'
(0#'/ -$)/# **&>
Concrete and Abstract Subclasses
.0'..*!)./-/'.. $/# -=
I  ./* ./-/$/. '!;*-
I  ./*#1 $(+' ( )//$*).*!''( ( -. 7) . abstract /#/$/
$)# -$/ 
*;!*- 3(+' ;/#$.* .)*/2*-&=
abstractabstract classclass AnimalAnimal {
abstractabstract funfun hasGills(): BooleanBoolean
funfun description() = "${displayName()}: hasGills = ${hasGills()}"
}
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
classclass KomodoDragonKomodoDragon : AnimalAnimal()
" /*(+$'  --*-!*- #*!/# Animal .0'.. .=
ABSTRACT CLASSES AND INTERFACES
129
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
error: class 'Frog' is not abstract and does not implement abstract base class
member public abstract fun hasGills(): Boolean defined in Test.Animal
class Frog : Animal()
^
$) hasGills() $. abstract;/# .0'.. .) /*  $/# - abstract *-+-*1$
)$(+' ( )//$*)!*- hasGills()=
abstractabstract classclass AnimalAnimal {
abstractabstract funfun hasGills(): BooleanBoolean
funfun description() = "hasGills = ${hasGills()}"
}
classclass FrogFrog(valval hasGillsRightNow: BooleanBoolean) : AnimalAnimal() {
overrideoverride funfun hasGills() = hasGillsRightNow
}
classclass AxolotlAxolotl : AnimalAnimal() {
overrideoverride funfun hasGills() = truetrue
}
classclass KomodoDragonKomodoDragon : AnimalAnimal() {
overrideoverride funfun hasGills() = falsefalse
}
funfun main() {
println(AxolotlAxolotl().description())
println(KomodoDragonKomodoDragon().description())
println(FrogFrog(truetrue).description())
}
J!-*( N./-/'.. .N$)/# '..**&K
3/'4 how /# .0'..$(+' ( )/. hasGills() $.0+/*/#/.0'..> Axolotl )
KomodoDragon #1 #-@* - .+*). .;2#$' Frog #.$/. hasGills()  + )
0+*)*)./-0/*-+-( / ->
Interfaces
)/# 7-./  *-.**!1;/# - 2.' -$./$)/$*) /2 )./-/
'.. .)$)/ -! .=
I ./-/'.. .*0'#1 *)- / ( /#*.)7 '.;'*)"2$/#./-/
( /#*. ' "/ /*.0'.. ./*$(+' ( )/
ABSTRACT CLASSES AND INTERFACES
130
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I )/ -! .2 - +0- '4. /*!./-/( /#*.;0. !*- 7)$)"*)/-/
1Z./-/ /*'0-/# '$) .;''*2$)"$)/ -! ./**6 -E !0'/F( /#*.
J*)- / ( /#*. .$") /* *1 --$ ).)  K>
*/'$)*6 -.$/.*2)/& *)$)/ -! .;2#$#'-" '4($--*-.1ZD.++-*#>
Basic Definition
 '-$)")$)/ -! $)*/'$)$..$($'-/*#*24*0 '- /# ($)1=- +'
class 2$/# interface )J0.0''4K#1 S]( ( -.J >">;!0)/$*).K>*0*)*/
) /# abstract & 42*-!*-!0)/$*).2$/#*0/*$ .;.$/$...0( /#//#*.
- abstract 4 !0'/=
interfaceinterface AquaticAquatic {
funfun hasGills(): BooleanBoolean
}
*2 1 -;/# - $.)*- ,0$- ( )/!*-)$)/ -! /*#1 any abstract ( ( -.;
/#*0"#;.*/#$.$.' "'=
interfaceinterface AquaticAquatic {
funfun hasGills() = falsefalse
}
Basic Usage
)1;#1$)"'..$(+' ( )/)$)/ -! ./-/.2$/#/# implements & 42*-=
classclass AnimalAnimal implementsimplements AquaticAquatic {
// TODO stuff goes here
}
)*/'$);$)/ -! .- %0./'$./ '*)".$ )4.0+ -'.. .$)*((@ '$($/ 
'$./!/ -/# : $)/# '.. '-/$*)=
interfaceinterface AquaticAquatic {
funfun hasGills(): BooleanBoolean
}
openopen classclass AnimalAnimal
abstractabstract classclass AquaticAnimalAquaticAnimal : AnimalAnimal(), AquaticAquatic {
ABSTRACT CLASSES AND INTERFACES
131
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun description() = "hasGills = ${hasGills()}"
}
classclass FrogFrog(valval hasGillsRightNow: BooleanBoolean) : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = hasGillsRightNow
}
classclass AxolotlAxolotl : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = truetrue
}
funfun main() {
println(AxolotlAxolotl().description())
println(FrogFrog(truetrue).description())
}
J!-*( N)/ -! .N$)/# '..**&K
 - ;2 #1 =
I /# Aquatic $)/ -! ;/* 0. !*-)$('./#/'$1 +-$(-$'4$)/# 2/ -
I . Animal '../#/$. open !*-.0'.. .
I ) AquaticAnimal /#/ 3/ ). Animal )'.*$(+' ( )/./# Aquatic
$)/ -!
I Frog ) Axolotl 3/ )$)" AquaticAnimal )$(+' ( )/$)"/# ./-/
hasGills() !0)/$*)
*/ /#/ AquaticAnimal ) ./*  '- . abstract;.2#$' $/E$(+' ( )/.F
Aquatic;$/* .)*/./$.!4/# Aquatic *)/-/4$(+' ( )/$)" hasGills()>$)
hasGills() $. $)" ! -- /*.0'.. ./*$(+' ( )/;2 ) /*(&
AquaticAnimal  )./-/'..> *)*/) /*- + //# abstract fun
hasGills(): Boolean  '-/$*);/#*0"#H/# *) /#/2 " /!-*( Aquatic $.
.08$ )/>
'.*)*/ /#/$)/# '$./*!'.. .)$)/ -! ..+-/*!/# AquaticAnimal
 '-/$*);'.. .0. *)./-0/*-)*//$*)J+- )/# . .);$!++'$' ;
+-( / -./*+../*/# .0+ -'..D*)./-0/*-K>)/ -! .;/#*0"#;- %0.//#
)( *!/# $)/ -! ;2$/#*0/+- )/# . .>
Using Multiple Interfaces
*/'$)* .)*/''*2'../* 3/ )!-*((0'/$+' .0+ -'.. .>*2 1 -;*/'$)
* .''*2'../*$(+' ( )/(0'/$+' $)/ -! .;%0./4$)"/# (/*/#
ABSTRACT CLASSES AND INTERFACES
132
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
 '-/$*)'$./=
interfaceinterface AquaticAquatic {
funfun hasGills(): BooleanBoolean
}
interfaceinterface ReproductionModeReproductionMode {
funfun isOviparous(): BooleanBoolean
// i.e., does it lay eggs?
}
openopen classclass AnimalAnimal
abstractabstract classclass AquaticAnimalAquaticAnimal : AnimalAnimal(), AquaticAquatic, ReproductionModeReproductionMode {
funfun description() = "hasGills = ${hasGills()}, isOviparous() = ${isOviparous()}"
}
classclass FrogFrog(valval hasGillsRightNow: BooleanBoolean) : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = hasGillsRightNow
overrideoverride funfun isOviparous() = truetrue
}
classclass AxolotlAxolotl : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = truetrue
overrideoverride funfun isOviparous() = truetrue
}
classclass OrcaOrca : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = falsefalse
overrideoverride funfun isOviparous() = falsefalse
}
classclass KomodoDragonKomodoDragon : AnimalAnimal(), ReproductionModeReproductionMode {
overrideoverride funfun isOviparous() = truetrue
}
funfun main() {
println(AxolotlAxolotl().description())
println(FrogFrog(truetrue).description())
println(OrcaOrca().description())
}
J!-*( N0'/$+' )/ -! .N$)/# '..**&K
 - ;2 #1 AquaticAnimal $(+' ( )/*/# Aquatic ) ReproductionMode>
!*0-. ;.$) '')$('.- +-*0 ;2 *0'#1 Animal $(+' ( )/
ReproductionMode ).$(+'$!4/#$.$/;/#*0"#$/2$''( )/#/4 !0'/ Animal
2*0') /* ./-/=
interfaceinterface AquaticAquatic {
funfun hasGills(): BooleanBoolean
}
interfaceinterface ReproductionModeReproductionMode {
funfun isOviparous(): BooleanBoolean
// i.e., does it lay eggs?
ABSTRACT CLASSES AND INTERFACES
133
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
abstractabstract classclass AnimalAnimal : ReproductionModeReproductionMode
abstractabstract classclass AquaticAnimalAquaticAnimal : AnimalAnimal(), AquaticAquatic
classclass FrogFrog(valval hasGillsRightNow: BooleanBoolean) : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = hasGillsRightNow
overrideoverride funfun isOviparous() = truetrue
}
classclass AxolotlAxolotl : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = truetrue
overrideoverride funfun isOviparous() = truetrue
}
classclass OrcaOrca : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = falsefalse
overrideoverride funfun isOviparous() = falsefalse
}
classclass KomodoDragonKomodoDragon : AnimalAnimal() {
overrideoverride funfun isOviparous() = truetrue
}
 -$)($)/#/)4 abstract '..$.'.* open;).*2 *0' !0'//#
$(+' ( )//$*)*!$)/ -! !0)/$*).$).0+ -'.. .;/*- 0 - 0))4$/=
interfaceinterface AquaticAquatic {
funfun hasGills(): BooleanBoolean
}
interfaceinterface ReproductionModeReproductionMode {
funfun isOviparous(): BooleanBoolean
// i.e., does it lay eggs?
}
abstractabstract classclass AnimalAnimal : ReproductionModeReproductionMode {
overrideoverride funfun isOviparous() = falsefalse
}
abstractabstract classclass AquaticAnimalAquaticAnimal : AnimalAnimal(), AquaticAquatic {
overrideoverride funfun hasGills() = truetrue
}
classclass FrogFrog(valval hasGillsRightNow: BooleanBoolean) : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = hasGillsRightNow
overrideoverride funfun isOviparous() = truetrue
ABSTRACT CLASSES AND INTERFACES
134
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
classclass AxolotlAxolotl : AquaticAnimalAquaticAnimal() {
overrideoverride funfun isOviparous() = truetrue
}
classclass OrcaOrca : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = falsefalse
}
classclass KomodoDragonKomodoDragon : AnimalAnimal() {
overrideoverride funfun isOviparous() = truetrue
}
*2 Frog; Axolotl; Orca;) KomodoDragon *)'4) /**1 --$ /#*. !0)/$*).
2# - /# $-) .$6 -!-*(/#  !0'/.0++'$ 4 Animal *- AquaticAnimal>
Concrete Functions
*/*)'4)$)/ -! .#1 !0)/$*).(-& . abstract;0//# 4)#1
- "0'-*)- / !0)/$*)..2 ''>./#$..(+' .#*2.;$)/ -! .*)*/
)  ..-$'4) )4 abstract !0)/$*)./''=
interfaceinterface AquaticAquatic {
funfun hasGills() = truetrue
}
interfaceinterface ReproductionModeReproductionMode {
funfun isOviparous() = falsefalse
// i.e., does it lay eggs?
}
abstractabstract classclass AnimalAnimal : ReproductionModeReproductionMode
abstractabstract classclass AquaticAnimalAquaticAnimal : AnimalAnimal(), AquaticAquatic
classclass FrogFrog(valval hasGillsRightNow: BooleanBoolean) : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = hasGillsRightNow
overrideoverride funfun isOviparous() = truetrue
}
classclass AxolotlAxolotl : AquaticAnimalAquaticAnimal() {
overrideoverride funfun isOviparous() = truetrue
}
classclass OrcaOrca : AquaticAnimalAquaticAnimal() {
overrideoverride funfun hasGills() = falsefalse
ABSTRACT CLASSES AND INTERFACES
135
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
classclass KomodoDragonKomodoDragon : AnimalAnimal() {
overrideoverride funfun isOviparous() = truetrue
}
 - ;-/# -/#)+0//$)"/#  !0'/$(+' ( )//$*).*! hasGills() )
isOviparous() *)/# Animal ) AquaticAnimal '.. .;2 +0//# ($- /'4*)
/# $)/ -! ./# (. '1 .>
Which Do You Use?
)/# .0-! ;$/. (.'$& $)/ -! .)*) -'4 1 -4/#$)"/#/./-/'.. .
)*;)4*0"$)/# 9 3$$'$/4*! $)"' /*0. (*- /#)*) $)/ -! *)
'..>
*;2#42*0'*)  1 -0. )./-/'..B-;/*+0/$/)*/# -24;$)2#/
. )-$*.2*0'4*00. ./-/'.. .;)$)2#/. )-$*.2*0'4*00.
$)/ -! .B
State Management
# $"" .//#$)"/#/$)/ -! .'&$.)4!*-(*!.// ()" ( )/># 4))*/
#1 *)- / +-*+ -/$ .;/# 24./-/'.. .J)- "0'-'.. .K)>
*;$!4*02)//*#1 .*( /#$)"/#/()" ..// 0/- ,0$- .$(+' ( )//$*).
/*!0'7''.*( *)/-/;)./-/'..$./#  // -#*$ >
)+-$)$+' ;4*0*0'0. $)/ -! .).$(+'4!*- $(+' ( )//$*)./**.*(
*!/# .// ()" ( )//# (. '1 .>*- 3(+' ;4*0*0'#1 ./-/ getFoo()
) setFoo() !0)/$*). '- $)/# $)/ -! ;+'0.#1 *)- / * $)/#
$)/ -! - '40+*)/#*. !0)/$*).>)+-$)$+' ;/#$.)2*-&>*2 1 -;/#
$)/ -! #.)**)/-*'*1 - 3/'4#*2/#/.// $.($)/$) =
I .$/*) E!**F+ -$)./) *!/# '..B
I .$/*) E!**F+ -+-* ..J >">;/*+@' 1 ' varKB
I .$/*) E!**F+ -.*( .*-/*!*)/ 3/J >">;+ -0. -$) ++KB
!/# $)/ -! /-0'4* .)*/- )*0'#)' )4*!/#*. . )-$*.;7) >
/# -2$. ;)./-/'..2*0'  // -#*$ ;.*/# *)- / * knows
3/'4#*2/# .// ()" ( )/$. $)"#)' >
ABSTRACT CLASSES AND INTERFACES
136
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
)/# */# -#);$!4*0#1 .*( * /#/$.(*- *!E($3$)F)* .)*/
) )4+-/$0'-.// ;/# )$)/ -! .- 7) >
Consumer of Contracts
)*/# -.0/' '$($//$*)$./#/$)/ -! .))*/#1 protected !0)/$*).>#
abstract !0)/$*).*))$)/ -! - $)/-$).$''4+0'$;/#*0"#/# $-*)- /
!0)/$*).) private $! .$- >
 ) ;$!4*02)//*- ,0$- .0'.. ./*$(+' ( )/.*( !0)/$*)'$/4;0//#/
!0)/$*)'$/4.#*0')*/  ..$' /*-$/--4*/# -*% /.;)./-/'..
2$/# protected abstract !0)/$*).$./#  ./#*$ >
!;*)/# */# -#);''*!/# abstract !0)/$*).- 7) !*-*/# -*% /./*'';
/# )$)/ -! .- 7) >
I Can Haz finalfinal?
)/ -! .)$)# -$/!-*(*/# -$)/ -! .;0.$)"/# .( )*//$*).*) 0. ./*
$)# -$/2$/#'.. .=
interfaceinterface BaseBase {
funfun foo()
}
interfaceinterface SubSub : BaseBase {
funfun bar()
funfun goo() {
// do something
}
}
*2 1 -;)$)/ -! ))*/(-&)$)# -$/ abstract ( /#*. final; 1 )$!
$/+-*1$ .)$(+' ( )//$*)>*;/#$.$.7) =
interfaceinterface BaseBase {
funfun foo()
}
interfaceinterface SubSub : BaseBase {
funfun bar()
ABSTRACT CLASSES AND INTERFACES
137
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun goo() {
// do something
}
overrideoverride funfun foo() {
// this supplies an implementation
}
}
?0//#$.$.)*/=
interfaceinterface BaseBase {
funfun foo()
}
interfaceinterface SubSub : BaseBase {
funfun bar()
funfun goo() {
// do something
}
overrideoverride finalfinal funfun foo() {
// this supplies an implementation
}
}
)./ ;2 " /*(+$'  --*-=
error: modifier 'final' is not applicable inside 'interface'
override final fun foo() {
^
./-/'.. .;'$& - "0'-'.. .;)*1 --$ $)# -$/ ( /#*.) '-
/#*. *1 --$ )( /#*.. final;/*+- 1 )/!0-/# -.0'.. .!-*(*1 --$$)"
/# (>
.- .0'/;)4/#$)"$))$)/ -! #$ --#4$.+ -() )/'4 open;0)/$'4*0./-/
$(+' ( )/$)"/# $)/ -! .$)'.. .>!/#/$.+-*' (H$!4*0#1 .*(
!0)/$*)/#/4*0- ''42)//*(-&. final H0. ./-/'.. .;)*/$)/ -! .>
ABSTRACT CLASSES AND INTERFACES
138
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Data Class
) *!/# - .*).2#4*/'$)#./& )*6$)+*+0'-$/4H+-/$0'-'4!*-)-*$
++ 1 '*+( )/H$./#/1) / $*0./*2-$/ >
) 24/#/ 1 '*+ -.#1 /-$ /*- 0 /#// $0($./#-*0"#))*//$*)
+-* ..*-./#/" ) -/ 1* !*-4*0>'..$ 3(+' *!/#/$.**"' D.
0/*'0 >0/*'0 $.!*-$((0/' *% /.;*) .2#*. 7 '.#1 " // -.0/
)*. // -.>)+-/$0'-;0/*'0 )* @" ) -/ /#$)".'$& ) equals()
( /#*/#//& .''*!/# 7 '.$)/**0)/>
)*/'$);/'..*6 -./# .( .$! /0- . /.0/*'0 ;2$/#.$(+' -
.4)/3; 0. $/$.E& $)/*F/# ')"0" $/. '!>
How You Declare It
''4*0*$./# data & 42*-/*/# '.. '-/$*)=
data classdata class AnimalAnimal(valval species: StringString, valval ageInYears: FloatFloat)
# '..) ./*#1 S]+-( / -.$)$/.*)./-0/*-;)/#*. +-( / -.) 
/*  '- . val *- var?)/4+$''44*02$''0. val>
)/#/D.+- //4(0#$/>*0- 2 '*( /*#1 2#/ 1 -!0)/$*).4*0) *)
/# '..;0/)*/#$)" '. $.- ,0$- >
What You Gain
;.*2#/* ./#/ data & 42*-"$1 0.B
139
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Standard Java Methods
*/'$)2$''0/*(/$''4.0++'4$(+' ( )//$*).*!=
I equals();2#$#*(+- . #*!/# +-*+ -/$ ./*. $!/# 4-  ,0'
I hashCode();2#$#" ) -/ .) Int /* 0. $)+' .'$& HashSet )
HashMap
I toString();(*./'4!*- 0""$)"+0-+*. .;.#*2$)"/# 1'0 .*! #*!
/# +-*+ -/$ .
*-*/'$)G;/# . ( /#*.- /# ./)-1 Object ( /#*./#/$ ''4
" /*1 --$ )*)(*./'.. .?4 /*)*/; 0. 2 .&$+/# ($!2 /#$)&/#/
2 *)*/) /# (>
#$.$..0. /*!/# 1 ,0$1' )/*!/# Animal /'...#*2)*1 =
publicpublic finalfinal classclass AnimalAnimal {
@NotNull
privateprivate finalfinal StringString species;
privateprivate finalfinal float ageInYears;
@NotNull
publicpublic finalfinal StringString getSpecies() {
returnreturn thisthis.species;
}
publicpublic finalfinal float getAgeInYears() {
returnreturn thisthis.ageInYears;
}
publicpublic Animal(@NotNull StringString species, float ageInYears) {
thisthis.species = species;
thisthis.ageInYears = ageInYears;
}
publicpublic StringString toString() {
returnreturn "Animal(species=" + thisthis.species + ", ageInYears=" + thisthis.ageInYears +
")";
}
publicpublic int hashCode() {
returnreturn (thisthis.species != nullnull ? thisthis.species.hashCode() : 0) * 31 +
FloatFloat.floatToIntBits(thisthis.ageInYears);
}
DATA CLASS
140
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
publicpublic boolean equals(ObjectObject var1) {
ifif (thisthis != var1) {
ifif (var1 instanceofinstanceof AnimalAnimal) {
AnimalAnimal var2 = (AnimalAnimal)var1;
ifif (thisthis.species.equals(var2.species) && FloatFloat.compare(thisthis.ageInYears,
var2.ageInYears) == 0) {
returnreturn truetrue;
}
}
returnreturn falsefalse;
} elseelse {
returnreturn truetrue;
}
}
}
J2 2$''. 2# - /#$.* ( !-*( '/ -$)/# **&;/#*0"#$/#. )
.$(+'$7 # - !*-/# +0-+*. .*!/#$.#+/ -K
*/'$)* @" ) -/ .(*- /#)/#$.;.0#./# copy() !0)/$*) .-$ $)/#
) 3/. /$*)>0/;/#$."$1 .4*0)$ *!2#/4*0- " //$)"!*- toString();
hashCode();) equals()>
*/ ;/#*0"#;/#/*/'$)2$''*)'4" ) -/ /# . ( /#*.!*-4*0$!/# 4-
)  =
I !4*0$(+' ( )//# (4*0-. '!$)4*0- data '..;*/'$)2$''..0( /#/
4*0&)*22#/4*0- *$)"
I !4*0- data '.. 3/ ).!-*(.*( */# -'..;*/'$)2$''.&$+)4*!/#*.
!0)/$*)./#//# '..$(+' ( )/.)(-&.. final
copy()copy()
*/'$)'.*" ) -/ . copy() !0)/$*)>./# )( .0"" ./.;/#$.- / .*+4*!
)$)./) *!4*0- data '..>*2 1 -;2#//# !0)/$*)- ''4* .$. +/''*!
/# +-*+ -/$ ..!0)/$*)+-( / -.; !0'/ /*/# 0-- )/1'0 .!-*(/#
$)./) >#$.''*2.4*0/*. ' /$1 '4*1 --$ 1'0 .$)/# *+4;2#$#$."- /
+' /*0. )( +-( / -.=
data classdata class AnimalAnimal(valval species: StringString, valval ageInYears: FloatFloat)
funfun main() {
DATA CLASS
141
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
valval critter = AnimalAnimal("frog", 3.14F)
valval youngerCritter = critter.copy(ageInYears = 0.1F)
println(youngerCritter)
}
J!-*( N/'..N$)/# '..**&K
#$.- .0'/.$)=
Animal(species=frog, ageInYears=0.1)
*;*0-. *) Animal *+$ /# species 0/#.$6 - )/ ageInYears;*0-/ .4
*!/# 1'0 /#/2 +-*1$ /* copy()>
/'.. .- 1 -40. !0'$)$(+' ( )/$)" $((0/' *% /.># copy()
!0)/$*)/# ) *( .& 424/*- / - +' ( )/ $/$*)*!)*% /=-/# -
/#)#)"$)"/#  3$./$)"$)./) ;4*0- / *+42$/#''*!/# 0-- )/
$)!*-(/$*) 3 +/2#/) ./*#)" >
What You Lose
*0))*/- / .0'.. .*!/'.. .> data '..))*/ (-& 2$/#
open /*''*2$//*  3/ ) >
!.0'.. .2 - ''*2 ;$/$.+*..$' /#/.0'.. .2*0'#)" /# '..
 7)$/$*)$)24./#/($"#/(& /# * @" ) -/  equals() )*/# -( /#*.
 $)1'$> ) ;/' ./-$"#/)*2;*/'$)+-*#$$/..0'.. .*!/'.. .>
.$ @ 6 /*!/# )*@.0'.. .'$($//$*)$./#//'.. .))*/ ./-/>
Data Classes with Other Properties
*0- 2 '*( /*#1 */# -+-*+ -/$ .$)4*0-/'.. .; 4*)/#*. $)/#
*)./-0/*-=
data classdata class AnimalAnimal(valval species: StringString, valval ageInYears: FloatFloat) {
varvar isFriendly = truetrue
varvar isHungry = truetrue
valval isCommonlySeenFlyingInTornadoes = falsefalse
}
DATA CLASS
142
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*2 1 -;* @" ) -/ !0)/$*).;'$& equals() ) copy();2$''$")*- /# .
+-*+ -/$ .>#*. !0)/$*).*)'4$)*-+*-/ /# +-*+ -/$ . 7) $)/# +-$(-4
*)./-0/*->
*( /$( .;/#$.) ! /0- =4*0($"#/2)/.*( +-*+ -/$ ./* $")*- !*-
equals() ) hashCode()>*( /$( .;/#$.) 0"=4*0($"#/)*/- '$5 /#/
copy() *)'4*+$ ..0. /*!4*0-+-*+ -/$ .>
DATA CLASS
143
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
The objectobject Keyword
*./'$& '4;/# - #1  ) 1 '*+ -.*1 -/# 4 -.2#*#1 2*) - 2#4
*% /@*-$ )/ +-*"-(($)"')"0" .*)*/#1 )4.*-/*! object & 42*->
#  .$") -.*!*/'$)(4#1  )(*)"/#*.  1 '*+ -.;./# 4#1 "$1 )
*/'$).0#) object & 42*->#$." /.0. $)! 2$6 - )/+' .$)*/'$)
.4)/3
Singletons
) 24/* '- .$)"' /*).$)*/'$)$./*0. /*+@' 1 ' val  '-/$*)=
classclass TransmogrifierTransmogrifier {
funfun transmogrify() {
println("Presto, change-o!")
}
}
valval QUASI_SINGLETON = TransmogrifierTransmogrifier()
funfun main() {
QUASI_SINGLETONQUASI_SINGLETON.transmogrify()
}
J!-*( N)0'$)"' /*).N$)/# '..**&K
)/-0/#;/#*0"#;2 )#1 .()4$)./) .*! Transmogrifier .2 2)/>
QUASI_SINGLETON $.( - '4"'*'$)./) *! Transmogrifier> ))*/+- 1 )/
*/# -.!-*((&$)"/# $-*2) Transmogrifier $)./) .;./# )2 *0')*/
- / /# ,0.$@.$)"' /*)>*- 3(+' ;2 ))*/(& /# *)./-0/*-
private;'$& /#$.=
145
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
classclass TransmogrifierTransmogrifier privateprivate constructorconstructor() {
funfun transmogrify() {
// TODO
}
}
valval QUASI_SINGLETON = TransmogrifierTransmogrifier()
QUASI_SINGLETONQUASI_SINGLETON.transmogrify()
./#/- .0'/.$)*(+$'  --*-=
error: cannot access '<init>': it is private in 'Transmogrifier'
val QUASI_SINGLETON = Transmogrifier()
^
*- / /-0 .$)"' /*);''2 ) /**$.=
I  +' /# class 2$/# object
I ''!0)/$*).*)/#/ object . *)$/.)( ;-/# -/#)- /$)".*(
$)./) *0-. '1 .
objectobject TransmogrifierTransmogrifier {
funfun transmogrify() {
println("Presto, change-o!")
}
}
funfun main() {
TransmogrifierTransmogrifier.transmogrify()
}
J!-*( N% /$)"' /*).N$)/# '..**&K
) object  '- /#$.24)#1 +-*+ -/$ .)!0)/$*).'$& - "0'-'..>/
))*/#1 *)./-0/*-;2#$#(& .. ). ;.$) /# - 2$''*)'4 1 - *)
$)./) >*2 1 -;$!$/ 3/ ).'.. .;$/)J)(0./K''/# .0+ -'..
*)./-0/*-.=
openopen classclass BaseBase {
// TODO stuff here
}
objectobject TransmogrifierTransmogrifier : BaseBase() {
funfun transmogrify() {
println("Presto, change-o!")
THE OBJECT KEYWORD
146
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
}
funfun main() {
TransmogrifierTransmogrifier.transmogrify()
}
J!-*( N% /.)0+ -'.. .N$)/# '..**&K
.2$/#.$)"' /*).$))4+-*"-(($)" )1$-*)( )/;/#*0"#;/# *) ./#/2
 '- 1$ object 2$'''$1 !*-/# '$! *!*0-+-* ..;.* - !0'/#/4*0- )*/
- /$)"( (*-4' &4#1$)") object #*'*)/*(*- )(*- ./06*1 -
/$( >
Companion Objects
)*/# -0. *!/# object & 42*-$.!*- companion *!'..> companion $.
.$)"' /*);..*$/ 2$/#/# '..;2#*. !0)/$*)..*( 2#/7''/# -*' /#/
static ( /#*.*$)1>)!/;$!4*0. /0+/#*. !0)/$*)./# -$"#/24;/# 4
) '' !-*(1 as .//$( /#*.>
Declaring and Using a Companion Object
*(+)$*)*% /$..$(+'4) object;) ./ $).$ *!'..;+- 73 2$/#/#
companion & 42*-=
classclass ThingyThingy {
companioncompanion objectobject {
funfun doSomething() {
println("Ummm... is this something?")
}
}
// TODO add other properties and functions
}
funfun main() {
ThingyThingy.doSomething()
}
J!-*( N*(+)$*)% /.N$)/# '..**&K
# );*/# -+-/$ .)''/# *(+)$*)*% /D.!0)/$*)..$!/# 42 - E.//$F
!0)/$*).*)/#  )'*.$)"'..J >">; Thingy.doSomething()>
THE OBJECT KEYWORD
147
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Why Bother?
/$. )/$- '4+*..$' /#/4*0- 0)$(+- .. 4/#$.>!/ -'';2 )#1 /*+@
' 1 '!0)/$*).$)*/'$)=
funfun doSomething() {
// TODO
}
doSomething()
*2 1 -;*(+)$*)*% /.#1 *) & 41)/" =/# 4#1  ../*+-$1/
!0)/$*).)+-*+ -/$ .*!/# $- )'*.$)"'..>*;/#$.2*-&.=
classclass ThingyThingy {
privateprivate valval count = 1
companioncompanion objectobject {
funfun doSomething(thingy: ThingyThingy) {
println(thingy.count)
}
}
// TODO add other properties and functions
}
funfun main() {
ThingyThingy.doSomething(ThingyThingy())
}
J!-*( N*(+)$*)% /.)-$1/  ( -.N$)/# '..**&K
1 )/#*0"# count $.+-$1/ ; doSomething() )./$''- ! - ) $/;.$)
doSomething() $.+-/*!/# Thingy $(+' ( )//$*)>
4*)/-./;/*+@' 1 '!0)/$*)))*/- ! - ) +-$1/ !0)/$*).)+-*+ -/$ .;
.*/#$.!$'.2$/#*(+$'  --*-=
classclass ThingyThingy {
privateprivate valval count = 1
}
funfun doSomething(thingy: ThingyThingy) {
println(thingy.count)
}
THE OBJECT KEYWORD
148
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Naming a Companion Object
*( /$( .;4*02$''. *(+)$*)*% /#1 )( ;/#*0"#+-*'4)*/*)
)( !/ - *(+)$*);.2 #1 # - =
classclass ThingyThingy {
privateprivate valval count = 1
companioncompanion objectobject AmyPondAmyPond {
funfun doSomething(thingy: ThingyThingy) {
println(thingy.count)
}
}
// TODO add other properties and functions
}
funfun main() {
ThingyThingy.doSomething(ThingyThingy())
}
J!-*( N( *(+)$*)% /.N$)/# '..**&K
#$.#.'$($/ $(+/*)4*0-*/'$)* H4*0./$''''!0)/$*).*)/# '...
 !*- >/* .#1 )$(+/$!4*0- "*$)"/*/-40.$)"/# *(+)$*)*% /
!0)/$*).!-*(1;.2 2$'' 3+'*- '/ -$)/# **&>
Nested Objects
*(+)$*)*% /$.%0./.+ $'$5 . *!) ./ *% /.;2# - 2 #1
E.$)"' /*).F '- $).$ *!'..> 2$'' 3($) ) ./ *% /.$)" ) -'
(*- $)/# ) 3/#+/ ->
Object Expressions
- ,0 )/'4;2 ) /*+..)$)./) *!)*% //*.*( !0)/$*);2# - 2
) /$'*- $)./) *!/#  .$- '..;2$/#0./*(!0)/$*)'$/4>
)1;/#$.$.2# - /# E)*)4(*0.$)) -'..F*( .$)/*+'4=
void clickyClickyClicky(ViewView view) {
view.setOnClickListener(newnew OnClickListenerOnClickListener() {
@Override
THE OBJECT KEYWORD
149
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
publicpublic void onClick(ViewView v) {
// TODO something useful
}
});
}
#$..*-/*!* 2$'' !($'$-/*'*)"@/$( )-*$ 1 '*+ -.>*7)*0/2# )
/#$. ' ( )/J ViewK" /.'$& ;2 )'' setOnClickListener();+-*1$$)"
)$(+' ( )//$*)*! OnClickListener $)/ -! >/# -/#)- / ./)'*)
'..;/#*0"#;2 0. )*)4(*0.$)) -'...4)/3/*- / '..).$)"'
$)./) *!/#/'..>
*/'$)D. ,0$1' )/*!/#/.*-/*!.4)/3$./# E*% / 3+- ..$*)F>
Basic Declaration
# */'$) ,0$1' )/*!/# View ) OnClickListener !-*(*1 ($"#/'**&'$&
/#$.=
interfaceinterface OnClickListenerOnClickListener {
funfun onClick(v: ViewView)
}
classclass ViewView {
funfun setOnClickListener(listener: OnClickListenerOnClickListener) {
// TODO something with this
}
}
J/ #)$''4;$))-*$; OnClickListener $.) ./ $)/ -! $).$ *! View H2
2$'' 3+'*- /#/+// -) $))0+*($)"#+/ -K
#1 /# View '..)) OnClickListener $)/ -! ;'*)"2$/#
setOnClickListener() !0)/$*)>
# )2 2)//*'' setOnClickListener();2 *0'- / ./)'*) '..
/#/$(+' ( )/./# OnClickListener $)/ -! >-;2 *0'0. )*% /
3+- ..$*)$)./ =
funfun clickyClickyClicky(view: ViewView) {
view.setOnClickListener(objectobject : OnClickListenerOnClickListener {
overrideoverride funfun onClick(v: ViewView) {
// TODO something useful
THE OBJECT KEYWORD
150
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
})
}
# object & 42*-$)$/ ./#/2 - - /$)".$)"' *% /;)/# :
OnClickListener $)$/ ./#//#$.*% /2$''$(+' ( )//# OnClickListener
$)/ -! >-*(/# - ;$/$.)*$6 - )//#))4*/# -$)/ -! $(+' ( )//$*)=2
override /# onClick() !0)/$*))*2#/ 1 -$/$./#/2 2)//**>
Constructors and Parameters
% / 3+- ..$*).) 3/ )*/# -'.. .)$(+' ( )/$)/ -! .># )*//$*)
$.+- //4(0#/# .( .2# )4*0#1 '.. 3/ )*/# -'.. .)
$(+' ( )/$)/ -! .># $"" ./$6 - ) $./#/4*00. /# object & 42*-$)
+' *!/# '..)( >
)/# *1  3(+' ; OnClickListener $.)$)/ -! >.- .0'/;*0-*% /
3+- ..$*)*0'%0./#1 /# $)/ -! )( !/ -/# *'*)J: OnClickListenerK
/*$(+' ( )/$/; 0. $)/ -! .#1 )**)./-0/*-.>'.. .*;.*$!
OnClickListener 2 - )./-/'..;4*02*0') /*$)1*& *)./-0/*-$)
4*0-*% / 3+- ..$*)=
abstractabstract classclass OnClickListenerOnClickListener {
abstractabstract funfun onClick(v: ViewView)
}
classclass ViewView {
funfun setOnClickListener(listener: OnClickListenerOnClickListener) {
// TODO something with this
}
}
funfun clickyClickyClicky(view: ViewView) {
view.setOnClickListener(objectobject : OnClickListenerOnClickListener() {
overrideoverride funfun onClick(v: ViewView) {
// TODO something useful
}
})
}
Access Rules
*0-*% / 3+- ..$*)#. ../*)4/#$)"/#/$.1$'' /**/# -*/'$)* $)
THE OBJECT KEYWORD
151
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
/# .( .*+ >*;!*- 3(+' ;$!/# - $.'*'1-$' $)!0)/$*)2# - /#
*% / 3+- ..$*)$. '- ;/# '*'1-$' $. ..$' 4/# *% /
3+- ..$*)D.*2)* =
funfun clickyClickyClicky(view: ViewView) {
varvar clicks = 0
view.setOnClickListener(objectobject : OnClickListenerOnClickListener() {
overrideoverride funfun onClick(v: ViewView) {
clicks++
println(clicks)
}
})
}
)1;/#$.- ,0$- ./# final & 42*-/*2*-&;)4*02*0')*/ ''*2 /*
(*$!4/# 1'0 /# 242 - *$)"# - >*/'$)$.(*- 9 3$' =4*0*)*/) 
)4& 42*-.)4*0#1 )*-(' ../*/# 1-$' >
The SAM Scenario
)*.$*);4*02$''7)*/'$)* /#/. (./* &$)/*)*% / 3+- ..$*)
0/%0./#.'(!*-$/.$(+' ( )//$*)=
valval listener = OnClickListenerOnClickListener { v: ViewView -> ... }
J2# - ... $..*( 0. !0'*/'$)* K
#$.$./# .$)"' @./-/@( /#*+// -)> 2$'' 3($) $/$)"- / - /$' '/ -
$)/# **&; 0. .$//0-).*0/;/#$.*)'42*-&.$!/# $)/ -!  $)"0. 
JOnClickListenerK$. 7) $)1;)*/$)*/'$)>
THE OBJECT KEYWORD
152
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Nested Objects and Classes
) /# +- 1$*0.#+/ -;2 .2/# 0. *! companion object>#$.$..+ $'
. )-$*!*-) ./ *% /.;2# - 2 #1 object 1'0 ..+-*+ -/$ .*!'..>
*/*)'4)'..#1 ) ./ *% /.;0/$/)#1 ) ./ '.. .;2# - 2
#1 *) '.. 7) $).$ *!)*/# -*) >
Nested Objects
.2 companion object $)/# +- 1$*0.#+/ -=
classclass ThingyThingy {
companioncompanion objectobject {
funfun doSomething() {
println("Ummm... is this something?")
}
}
// TODO add other properties and functions
}
funfun main() {
ThingyThingy.doSomething()
}
J!-*( N*(+)$*)% /.N$)/# '..**&K
# 1'0 *!/# companion & 42*-$./#/!0)/$*).*)/# companion object )
 '' %0./'$& 4*0($"#/'' static !0)/$*).$)1=
ThingyThingy.doSomething()
153
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*2 1 -;4*0)0. /# object & 42*-!*-) ./ *% /.2$/#*0/)  ..-$'4
0.$)" companion>
Named Objects
) *+/$*)$./*"$1 /# *% /)( ;&$)/*#*24*0"$1 '..)( =
classclass ThingyThingy {
objectobject SomethingifierSomethingifier {
funfun doSomething() {
println("Ummm... is this something?")
}
}
// TODO add other properties and functions
}
funfun main() {
ThingyThingy.SomethingifierSomethingifier.doSomething()
}
J!-*( N(  ./ % /.N$)/# '..**&K
# );4*0- ! - ) $/1$*/)*//$*)JThingy.Somethingifier.doSomething()K>
Object Properties
(*- /4+$'++-*#;/#*0"#;$..$(+'4/*..$")/# *% //*+-*+ -/4;0.$)"
)*% / 3+- ..$*)./# +-*+ -/4$)$/$'$5 -=
interfaceinterface SomethingifierSomethingifier {
abstractabstract funfun doSomething()
}
classclass ThingyThingy {
valval somethingifier = objectobject : SomethingifierSomethingifier {
overrideoverride funfun doSomething() {
println("Ummm... is this something?")
}
}
// TODO add other properties and functions
}
funfun main() {
NESTED OBJECTS AND CLASSES
154
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
valval thingy = ThingyThingy()
thingy.somethingifier.doSomething()
}
J!-*( N% /-*+ -/$ .N$)/# '..**&K
*0)/# )- ! - ) $/.4*02*0')4*/# -+-*+ -/4
Jthingy.somethingifier.doSomething()K>
Nested Classes
1.0++*-/.) ./ '.. .=
classclass FooFoo {
classclass BarBar {
// TODO good stuff
}
BarBar bellyUp() {
returnreturn newnew Bar();
}
// TODO even more good stuff
}
FooFoo.Bar bar = newnew FooFoo().bellyUp();
$($'-'4;*/'$).0++*-/.) ./ '.. .>0/;.2$/#()4/#$)".$)*/'$);/# -
- .*( $6 - ) . /2 )1D.++-*#)*/'$)D.>
Simple Nested Classes
*0)#1  class $).$ *! class .$'4 )*0"#$)*/'$)=
classclass FooFoo {
classclass BarBar {
funfun whatever() {
println("You were expecting something profound?")
}
// TODO add good stuff
}
// TODO add even more good stuff
NESTED OBJECTS AND CLASSES
155
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
funfun main() {
valval bar = FooFoo.BarBar()
bar.whatever()
}
J!-*( N ./ '.. .N$)/# '..**&K
*2 1 -;/#$.$.)*/,0$/ /# .( ./# 1 3(+' *1 >#  !0'/ #1$*-
$)*/'$)$.(*- &$)/*1D. static '..=
classclass FooFoo {
staticstatic classclass BarBar {
// TODO good stuff
}
// TODO even more good stuff
}
FooFoo.Bar bar = newnew FooFoo.Bar();
)$)./) *! Bar $.)*//$ /*)4$)./) *! Foo>)()4- .+ /.;/# ) ./ 
'..$.*)'4*)) / /*$/.*0/ -'..4)( H4*0($"#/%0./. .$'4#1 =
classclass BarBar {
// TODO good stuff
}
classclass FooFoo {
// TODO even more good stuff
}
valval bar = BarBar()
# $"" ./1'0 *!) ./ '..;*1 -*(+' / '4$) + ) )/'..;$./#/
) ./ '..#. ../* private 1'0 ./#/- $)) '$"$' .*+ >#/2*0'
$)'0 1'0 .# '4) object $).$ *!/# *0/ -'..=
classclass FooFoo {
privateprivate objectobject DataData {
constconst valval VALUE = 1
}
classclass BarBar {
NESTED OBJECTS AND CLASSES
156
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun value() = FooFoo.DataData.VALUEVALUE
// TODO add good stuff
}
// TODO add even more good stuff
}
funfun main() {
valval bar = FooFoo.BarBar()
println(bar.value())
}
J!-*( N ./ '.. .)-$1/  ..N$)/# '..**&K
 - ; Bar ) .. Foo.Data; 1 )/#*0"#/#/*% /$. private;.$) Bar $.
) ./ $).$ *! Foo>'.. .)*% /.*0/.$ *! Foo #1 )*$- / ../* Data>
Inner Classes
 /D."*&/*/# *-$"$)'1.)$++ /=
classclass FooFoo {
classclass BarBar {
// TODO good stuff
}
BarBar bellyUp() {
returnreturn newnew Bar();
}
// TODO even more good stuff
}
FooFoo.Bar bar = newnew FooFoo().bellyUp();
)1;4 !0'/H.$)/# . 2$/# Foo ) Bar *1 H/# ) ./ '..$.)
E$)) -F'..>)$)./) *! Bar #. ../* 1 -4/#$)"$).$ *!) )'*.$)"
$)./) *! Foo>
classclass FooFoo {
int count = 1;
classclass BarBar {
int getCount() {
NESTED OBJECTS AND CLASSES
157
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
returnreturn count;
}
}
}
FooFoo.Bar bar = newnew FooFoo().bellyUp();
int theCount = bar.getCount();
0-$)./) *! Bar #./# $'$/4/*E- #$)/*F) )'*.$)"$)./) *! Foo )
 ..$/.( ( -.;$)'0$)"7 '.)( /#*.>)/#$.. ; Bar  .. ./#
count 7 '*! Foo>
#  ,0$1' )/$)*/'$)- ,0$- .4*0/*0. /# inner & 42*-2# ) '-$)"/#
$)) -'..=
classclass FooFoo {
valval count = 1
innerinner classclass BarBar {
funfun count() = count
}
}
funfun main() {
valval foo = FooFoo()
valval bar = foo.BarBar()
println(bar.count())
}
J!-*( N)) -'.. .N$)/# '..**&K
)$)./) *! Bar )- ! - ) /# count $).$ *!/#  )'*.$)"$)./) *! Foo>
)*/#1)*/'$);*)'4) instance *!/# *0/ -'..)- / )$)./) *!
/# $)) -'..>)*0-. ;2 ) )$)./) *! Foo /* ' /*.&$//*- /
!*-0.)$)./) *! Bar>)/# . *!*/'$); 0. - /$)") 2*% /.* .
)*/- ,0$- & 42*-;2 )%0./$)1*& /# Bar() *)./-0/*-*))$)./) *!
Foo>)1;/4+$''42 0. .*( ( /#**)/# *0/ -'../*- / $)./) .*!
/# $)) -'..!*-0.J >">; bellyUp()K>
'.*;/*(& /#$.2*-&;/# $)./) *!/# $)) -'..#.) implicit - ! - ) /*
/# $)./) *!/# *0/ -'..> *)*/#1 ) 3+'$$/7 '!*-$/;0/$/$./# - >
#$.0. ."-" *'' /$*)+-*' (.;. 1 '*+ -..*( /$( .!*-" //#/ Bar
#.#$ )- ! - ) /* Foo;)/# - !*- /# Foo ))*/ "-" @*'' / 
NESTED OBJECTS AND CLASSES
158
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
2#$' 2 - #*'$)"*)/* Bar>
Which thisthis is thisthis?
)) -'.. .-$. /#*-)4+-*' (=2# )2 0. this;2#/$)./) $.$/- ! --$)"
/*B
4 !0'/; this - ! -./*/# $)./) *!/# $)) -'..;.*/#$.* +-$)/. class
Bar *- class Foo$Bar; + )$)"*)2#/*/'$)+'/!*-(4*0-0)*)=
classclass FooFoo {
innerinner classclass BarBar {
funfun that() = thisthis
}
}
funfun main() {
valval foo = FooFoo()
valval bar = foo.BarBar()
println(bar.that()::classclass)
}
J!-*( N)) -'.. .)/#$.N$)/# '..**&K
J$ )*//$*)$)$/ .)$)) -'..- '/$*).#$+;) Foo$Bar ( ).E/# Bar '..
/#/$.)$)) -'..*! FooF;0//#$.2$''*)'4 . )$)*/'$)G;)*//# */'$)G
/#/'..**&0. .K
)*/# -2*-.; this 4 !0'/- ! -./*/# $)./) *!/# $)) -'..JBar;$)/#$.
. K>
.$*)''4;/#*0"#;2 ) /*.+ $7''4" /- ! - ) /*/# $)./) *!/#
*0/ -'..>)-*$1 1 '*+ -.-0)$)/*/#$.!- ,0 )/'4=
I ).*( Activity;4*0 7) ))*)4(*0.$)) -'../*0. !*-''&;
.0#. View.OnClickListener
I )( /#*$)/#/)*)4(*0.$)) -'..;4*0) - ! - ) /*/#
Activity
I *0/-40.$)" this;)4*0" /*(+$'  --*-.;.4$)"/#/ this $.)*/)
Activity J*- Context *-*/# -.0+ -'..K
1#..4)/3/*- #$)"/# *0/ -'..$)./) =
NESTED OBJECTS AND CLASSES
159
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I this - ! -./*/# $)) -'..$)./)
I OuterClass.this - ! -./*/# *0/ -'..$)./) /#/- / /# 0-- )/
$)) -'..$)./) /#/+'$) this - ! -./*
*/'$)#./# .( *) +/;2$/#$6 - )/.4)/3=
I this - ! -./*/# $)) -'..$)./)
I this@OuterClass - ! -./*/# *0/ -'..$)./) /#/- / /# 0-- )/
$)) -'..$)./) /#/+'$) this - ! -./*
classclass FooFoo {
valval count = 1
innerinner classclass BarBar {
funfun count() = count
funfun that() = thisthis
funfun theOuterThis() = thisthis@Foo
}
}
funfun main() {
valval foo = FooFoo()
valval bar = foo.BarBar()
println(bar.that()::classclass)
println(bar.theOuterThis()::classclass)
}
J!-*( N)) -'.. .)0/ -/#$.N$)/# '..**&K
#$..)$++ /+-$)/..*( /#$)"'$& =
class Bar
class Foo
this@Foo - /0-). Foo;.+ $7''4/# $)./) *! Foo /#/- / /# Bar $)./)
*)2#$#2 ''  theOuterThis()>
Nested Interfaces and Abstract Classes
*/*)'4)4*0) ./'.. .)*% /.$).$ *!'.. .;0/4*0)) ./
$)/ -! ..2 ''=
NESTED OBJECTS AND CLASSES
160
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
classclass ThingyThingy {
interfaceinterface TransmogrifierTransmogrifier {
abstractabstract funfun transmogrify(): ThingyThingy
}
// TODO good stuff here
}
valval moggle = objectobject : ThingyThingy.TransmogrifierTransmogrifier {
overrideoverride funfun transmogrify() = ThingyThingy()
}
valval thingy = moggle.transmogrify()
-;$!4*0+- ! -;4*0)) ././-/'.. .=
classclass ThingyThingy {
abstractabstract classclass TransmogrifierTransmogrifier {
abstractabstract funfun transmogrify(): ThingyThingy
}
// TODO good stuff here
}
valval moggle = objectobject : ThingyThingy.TransmogrifierTransmogrifier() {
overrideoverride funfun transmogrify() = ThingyThingy()
}
valval thingy = moggle.transmogrify()
($)"2*-&./# .( .2$/#) ./ '.. .))( *% /.=4*0- ! - ) /#
) ./ $)/ -! *-./-/'... *)/# *0/ -'..)( J >">;
Thingy.TransmogrifierK>
NESTED OBJECTS AND CLASSES
161
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Enums and Sealed Classes
**' )$.24*!(* '$)"73 . /*!.// .;2# - /# - - *)'4/2*
+*..$' .// .>
- ,0 )/'4;/#*0"#;2 7)*0-. '1 .) $)"/*(* '73 . /*!.// ./#/#.
(*- /#)/2* )/-$ .>*- 3(+' ; 1 )2$/#.*( /#$)"/#/$.)*($)''4
**' );2 ($"#/#1 E/-0 F;E!'. F;)E0) 7) F># '// -.// 2*0' !*-
. .2# - 2 #1 )*/4 /"*) /#-*0"#)4* /#/+*.$/$1 '4. /./# .// /*
E/-0 F*-E!'. F>*- 3(+' ;!*-.*( .*-/*!0. -+- ! - ) ;+ -#+./# 0. -#.
)*/1$.$/ /# E. //$)".F.- )/*.// /# $-+- ! - ) %0./4 /;.*/# $-+- ! - )
$.E0) 7) F>
# (*- 4*0'**&;/# (*- 4*07)/#$)"./#/($"#/ (* ' /#$.24=
I #  --*-- .+*). .!-*(. -1 -
I # ./ +.$)2$5-@./4' E.$")0+F. /*!.- ).
I # (* .*!/-).+*-//$*)/#/ Person *% /($"#/ /&$)"J2'&$)"B
$&$)"B-$1$)"B94$)"B.&$$)"B% /@.&$$)"BK
I ).**)
*-/# . . .;*/'$)*6 -./2*+-*"-(($)"*)./-0/.= )0( -/$*).JE )0(.FK
). ' '.. .>)/#$.#+/ -;2 2$'' 3+'*-  #*!/# . >
Enums
*( +-*"-(($)"')"0" .H+-/$0'-'4/#*. /#/"- 2*0/*!H*6 -
E )0(.F.7-./@'..*)./-0/.>*;1#.) enum=
163
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
enumenum ServerErrorServerError {
INVALID_INPUT,
OUT_OF_STORAGE_SPACE,
USER_NOT_FOUND,
CLIENT_NOT_FOUND,
SERVER_NOT_FOUND,
WHOEVER_YOU_THINK_YOU_ARE_TALKING_TO_NOT_FOUND
}
*/'$)D. enum *+/$*)- . (' ./#/*!1>
Basic Syntax and Usage
# 7-./$6 - )  /2 )*/'$))1$.$)/#  '-/$*)>)1; enum 3$./.
../)'*) & 42*->)*/'$);/# enum  *-/ . class;(0#'$& #*2 data $.
/4+ *!'..=
enumenum classclass HungerStateHungerState {
NOT_HUNGRYNOT_HUNGRY,
SOMETIMES_HUNGRYSOMETIMES_HUNGRY,
ME_ALWAYS_HUNGRYME_ALWAYS_HUNGRY,
RAVENOUSRAVENOUS,
YOU_LOOK_LIKE_FOODYOU_LOOK_LIKE_FOOD
}
*0)/# )- ! -/* enum 1'0 .0.$)"./)-*/)*//$*);.0#.
HungerState.ME_ALWAYS_HUNGRY>
$) /#$.$./4+ *!'..;4*00. ) enum $)*/'$)/# .( 24/#/4*02*0'
)4*/# -'..;.0#.$)*)./-0/*-+-( / -=
enumenum classclass HungerStateHungerState {
NOT_HUNGRYNOT_HUNGRY,
SOMETIMES_HUNGRYSOMETIMES_HUNGRY,
ME_ALWAYS_HUNGRYME_ALWAYS_HUNGRY,
RAVENOUSRAVENOUS,
YOU_LOOK_LIKE_FOODYOU_LOOK_LIKE_FOOD
}
data classdata class AnimalAnimal(
valval species: StringString,
valval ageInYears: FloatFloat,
valval hungry: HungerStateHungerState = HungerStateHungerState.SOMETIMES_HUNGRYSOMETIMES_HUNGRY
) {
ENUMS AND SEALED CLASSES
164
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
varvar isFriendly = truetrue
valval isCommonlySeenFlyingInTornadoes = falsefalse
}
Class Features
$) */'$) enum $.'..;4*0)0. ()4'..! /0- .$)) enum>
Constructors
# (*./*((*)'4. )*!/# . $./# *)./-0/*-;0. /*+-*1$ 1'0 ./*
 7) )$)$1$0' enum *)./)/=
enumenum classclass HttpResponseHttpResponse(valval code: IntInt) {
OKOK(200),
MOVED_PERMANENTLYMOVED_PERMANENTLY(301),
NOT_MODIFIEDNOT_MODIFIED(304),
UNAUTHORIZEDUNAUTHORIZED(401),
NOT_FOUNDNOT_FOUND(404),
INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR(501)
}
funfun main() {
println(HttpResponseHttpResponse.OKOK.code)
}
J!-*( N )0('..N$)/# '..**&K
 - ;2 0. *)./-0/*-/*..*$/  code 1'0 2$/# # HttpResponse
*)./)/>#$.$.%0./)*-$)-4'..+-*+ -/4;.*4*0).&*)./)/'$& OK !*-
$/. code>
Functions
) enum '..)$(+' ( )/!0)/$*).;$)'0$)"*1 --$$)". *) .'$&
toString()=
enumenum classclass HttpResponseHttpResponse(valval code: IntInt, valval message: StringString) {
OKOK(200, "OK"),
MOVED_PERMANENTLYMOVED_PERMANENTLY(301, "Moved Permanently"),
NOT_MODIFIEDNOT_MODIFIED(304, "Not Modified"),
UNAUTHORIZEDUNAUTHORIZED(401, "Unauthorized"),
NOT_FOUNDNOT_FOUND(404, "Not Found"),
INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR(501, "WTF?");
ENUMS AND SEALED CLASSES
165
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
overrideoverride funfun toString() = message
}
funfun main() {
println(HttpResponseHttpResponse.INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR.toString())
}
J!-*( N )0()0)/$*).N$)/# '..**&K
# '$./*!*)./)/.(0./ /# 7-.//#$)"$)/# enum  '-/$*)H$!2 /-$ /*
#1 *0- toString()  !*- /# OK;2 2*0'!$'2$/#*(+$'  --*->
*/ /#/$)/#$.. /# *((@ '$($/ '$./*!*)./)/.$. ) 2$/#
. ($*'*)>!/# *)'4/#$)"$)/# enum  '-/$*)$./# '$./*!*)./)/.;/#
*1 -'''*.$)"- *!/# enum $..08$ )//* )/# '$./*!*)./)/.>!4*0#1
*/# -/#$)".!/ -/# *)./)/.;/#*0"#;.0#.*0- toString() !0)/$*);4*0) 
/# . ($*'*)/**8$''4 )/# '$./*!*)./)/.>
) enum '..$.$)/-$).$''4 abstract;.*4*0) 7) ./-/!0)/$*)..2 '';
$(+' ( )/$)"/# (*)$)$1$0'*)./)/.=
enumenum classclass WizardStepWizardStep {
INTROINTRO {
overrideoverride funfun nextStep() = REGISTERREGISTER
},
REGISTERREGISTER {
overrideoverride funfun nextStep() = PERMISSIONSPERMISSIONS
},
PERMISSIONSPERMISSIONS {
overrideoverride funfun nextStep() = THANKSTHANKS
},
THANKSTHANKS {
overrideoverride funfun nextStep() = THANKSTHANKS
};
abstractabstract funfun nextStep(): WizardStepWizardStep
}
 - ; # WizardStep &)*2./# ) 3/ WizardStep $)/# . ,0 ) ;4*1 --$$)")
./-/ nextStep() !0)/$*) '- !*- WizardStep>#$." /.$/*2$/#/#
'././ +;.4 7)$/$*)/# '././ +$.'./;.*/# - $.)*E) 3/./ +F>*0($"#/
/ (+/ /*- /0-).*( /#$)"'$& null !-*( nextStep() *) THANKS?2 2$''. #*2
/#/2*-&.$) )0+*($)"#+/ ->
ENUMS AND SEALED CLASSES
166
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Limitations
) enum '..$.''*2 /*$(+' ( )/$)/ -! .;0/$/$.)*/''*2 /* 3/ )
)*/# -'..>
'.*;&$)/* data '.. .;) enum ))*/ open !*- 3/ ).$*)?4'.. .*0/.$
*!/# enum '..>#*!/#  )0( -/ *)./)/.J >">; REGISTER; THANKS $)/#
*1  3(+' K$.$) 6 /.0'..*!/# enum '..;*(+' / 2$/# override
( /#*./*(/#/# abstract *) .$)/# enum '..>0/4*0))*/- / ) 2
*)./)/.!-*(*0/.$ /# enum '..;)4*0))*/- / -$/--4*/# -
.0'.. .*!/# enum '..>
Common Properties
# )0( -/ *)./)/#./2*+-*+ -/$ .; 4*)/#*. /#/4*0($"#/ '-
4*0-. '!= name ) ordinal> name - /0-)./# .4(*'$)( /#/4*0"1 /#
*)./)/$)4*0-* ;) ordinal - /0-)./# R@. $) 3$)$/$)"2# - /#$.
*)./)/++ -.$)/# '$./*!*)./)/.>*0) .. name ) ordinal .4*0)
)4*/# -+-*+ -/4=
enumenum classclass HttpResponseHttpResponse(valval code: IntInt, valval message: StringString) {
OKOK(200, "OK"),
MOVED_PERMANENTLYMOVED_PERMANENTLY(301, "Moved Permanently"),
NOT_MODIFIEDNOT_MODIFIED(304, "Not Modified"),
UNAUTHORIZEDUNAUTHORIZED(401, "Unauthorized"),
NOT_FOUNDNOT_FOUND(404, "Not Found"),
INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR(501, "WTF?");
overrideoverride funfun toString() = message
}
funfun main() {
println(HttpResponseHttpResponse.INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR.toString())
println(HttpResponseHttpResponse.INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR.code)
println(HttpResponseHttpResponse.INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR.name)
println(HttpResponseHttpResponse.INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR.ordinal)
}
J!-*( N )0()( )*-$)'N$)/# '..**&K
#$.4$ '.=
ENUMS AND SEALED CLASSES
167
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
WTF?
501
INTERNAL_SERVER_ERROR
5
# )( *0' 0. !0'$) toString() $(+' ( )//$*).).$($'-. )-$*.>
# *-$)'$.' ..'$& '4/* 0. !0'H$)+-/$0'-;.$) $/$.+*.$/$*)@ + ) )/;
$/(4 $/!-"$' ;..*( *4- *- -$)"'$) .*!4*0-* ($"#/#)" /#
*-$)'1'0 .!*-/#*. *)./)/.>
Conversion
# enum '..$."$1 )! 2!0)/$*)./* '' *)/# '..$/. '!>) $.
valueOf()>#$.- /0-).*)./)/"$1 )/# .4(*'$)( /#/4*0"1 /#
*)./)/$)4*0-* >)*/# -2*-.;$/2*-&.'$& /# $)1 -. *!/# name +-*+ -/4
H name "$1 .4*0/# .4(*'$)( !*-*)./)/;2#$' valueOf() "$1 .4*0/#
*)./)/!*-.4(*'$)( =
enumenum classclass HttpResponseHttpResponse(valval code: IntInt, valval message: StringString) {
OKOK(200, "OK"),
MOVED_PERMANENTLYMOVED_PERMANENTLY(301, "Moved Permanently"),
NOT_MODIFIEDNOT_MODIFIED(304, "Not Modified"),
UNAUTHORIZEDUNAUTHORIZED(401, "Unauthorized"),
NOT_FOUNDNOT_FOUND(404, "Not Found"),
INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR(501, "WTF?");
overrideoverride funfun toString() = message
}
funfun main() {
println(HttpResponseHttpResponse.valueOf("NOT_MODIFIED"))
}
J!-*( N )0(1'0 !JKN$)/# '..**&K
#$.- .0'/.$)=
Not Modified
valueOf() - /0-)./# NOT_MODIFIED *)./)/> println() /# )''. toString()
$(+'$$/'4*)/#/*)./)/;)*0-*1 --$ ) toString() !0)/$*)- /0-). "Not
Modified">
ENUMS AND SEALED CLASSES
168
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Iteration
'.*;4*0)'' values() *)/# enum '..$/. '!J >">; HttpResponse.values()K>
#$.2$''''*24*0/*$/ -/ *1 -''*!/# *)./)/( ( -.*!/# enum '..;$)
/# *- -$)2#$#/# 42 -  '- > ) ;/#$.* =
enumenum classclass HttpResponseHttpResponse(valval code: IntInt, valval message: StringString) {
OKOK(200, "OK"),
MOVED_PERMANENTLYMOVED_PERMANENTLY(301, "Moved Permanently"),
NOT_MODIFIEDNOT_MODIFIED(304, "Not Modified"),
UNAUTHORIZEDUNAUTHORIZED(401, "Unauthorized"),
NOT_FOUNDNOT_FOUND(404, "Not Found"),
INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR(501, "WTF?");
overrideoverride funfun toString() = message
}
funfun main() {
forfor (constant inin HttpResponseHttpResponse.values()) {
println(constant)
}
}
J!-*( N )0(1'0 .JKN$)/# '..**&K
- .0'/.$)=
OK
Moved Permanently
Not Modified
Unauthorized
Not Found
WTF?
Exhaustive whenwhen
.2/#/4*0)0. when . ) 3+- ..$*);2# -  #*) *!/# -)# .
$).$ /# when .0++'4/# 1'0 !*-/#  3+- ..$*)2# )/#/-)#$. true>
*2 1 -;*) '$($//$*)/#/2 .22./#/4*0)  ) else *)$/$*)2# )
0.$)" when .) 3+- ..$*);.!*-)4+*..$' *)$/$*);/# when ) ./*
" ) -/ 1'0 >
)  3 +/$*)/*/#/-0' $.)E 3#0./$1 whenF. *)) enum '..>!4*0#1
*)$/$*).!*- # )0( -/ *)./)/;4*0*)*/) ) else *)$/$*);.$)
ENUMS AND SEALED CLASSES
169
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
4 7)$/$*) 1 -4+*..$$'$/42$''#1  )#)' 4*) *!/# */# -*)$/$*).>
*- 3(+' ;/#$..-$+/4$ './# .( - .0'/./# *) .#*2)*1 =
enumenum classclass HttpResponseHttpResponse(valval code: IntInt) {
OKOK(200),
MOVED_PERMANENTLYMOVED_PERMANENTLY(301),
NOT_MODIFIEDNOT_MODIFIED(304),
UNAUTHORIZEDUNAUTHORIZED(401),
NOT_FOUNDNOT_FOUND(404),
INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR(501);
overrideoverride funfun toString() = whenwhen(thisthis) {
OKOK -> "OK"
MOVED_PERMANENTLYMOVED_PERMANENTLY -> "Moved Permanently"
NOT_MODIFIEDNOT_MODIFIED -> "Not Modified"
UNAUTHORIZEDUNAUTHORIZED -> "Unauthorized"
NOT_FOUNDNOT_FOUND -> "Not Found"
INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERROR -> "WTF?"
}
}
funfun main() {
forfor (constant inin HttpResponseHttpResponse.values()) {
println(constant)
}
}
J!-*( N )0()2# )JKN$)/# '..**&K
#$./$( ; toString() )*'*)" -%0./- ..*( +-*+ -/4> )*'*)" -#1 /#*.
+-*+ -/$ .;)$)./ 0.  when 3+- ..$*)/*" //# #0()@- ' ( .." /*
"*'*)"2$/#/# - .+*). * > *)*/) ) else $)/# when;.$)
1 -4+*..$'  )0( -/ *)./)/#.$/.*2)*)$/$*)> #1 E 3#0./ F''
+*..$$'$/$ .2$/#/# .+ $7*)$/$*).;).*/#$.$.)E 3#0./$1 F when>
#$.+-/$0'- 3(+' $.- ''4.$''4H#1$)"/# . ( .." ..+-*+ -/$ .2*0'
  // -#*$ >*2 1 -;4*0(4#1 */# -+' .$)4*0-* 2# - 4*0) 
/*-)#. *)) enum 1'0 ;).*'*)".''+*..$' 1'0 .- *0)/ 
!*-;4*0*)*/) ) else>
Sealed Classes
 ' '.. .$)*/'$)#1 )*/#$)"/**2$/# 23; ,0/$((('.;*-
ENUMS AND SEALED CLASSES
170
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
(0.$$).>
)./ ;. ' '.. .- 24*!'$($/$)"'..#$ --#4>'..)*)'4
$- /'4 3/ ). ' '..$!=
I )- "0'-+-*% /;$/$.$)/# .( */'$)7' .$./# . ' '..$/. '!
I );$/$.) ./ '..$).$ *!/# . ' '..
.- .0'/;*) /#/*) 7' $.- $)4*/'$)*(+$' -;$/$(( $/ '4&)*2.
/# *(+' / +*..$' . /*!''$- /.0'.. .*!/# . ' '..>#$.$.$/
- ($)$. )/*!) enum '..;2#$#'.*#.-0' .!*-2#/).0'..$/
J.+ $7''4;*)'4$/.*)./)/ ' ( )/.K>
Um, Why?
& 4'$($//$*)*!) enum '..$./#/ #*!$/.*)./)/( ( -.$.)*/*)'4
.0'..*!/# enum '..;0/$..$)"' /*)$)./) *!/#/.0'..>*$)"&/*
/# HttpResponse . )-$*.!-*(*1 ;/# - $. 3/'4*) OK $)./) !*-/#
)/$- +-*"-(>*) enum '..'$($/.)*/*)'42# - .0'.. .)- .$ 0/
2# - $)./) .)  '- >
)*)/-./;. ' '..*)/-*'./# '..#$ --#4;0/$/* .)*/'$($/$)./)
- /$*)># - ) $)./) .*!.0'..*!. ' '..; #2$/#$/.*2)
/>
*2 1 -;.$) /# .0'.. .*!/# . ' '..- - $'4$ )/$7' H/# 4-
''$)/# .( */'$).*0- 7' H2 " /.*( *!/# .(  ) 7/./#/2 " /
2$/# enum '.. .;)*/'4/#  3#0./$1 when .0++*-/>
Basic Declaration and Usage
*- / . ' '..;./-/4$)"/# sealed & 42*-=
sealedsealed classclass BrowserLocationBrowserLocation(valval url: StringString)
 ' '.. .- ./-/;.*4*0))4/#$)"/#/4*02)//#/2*-&.2$/#
)./-/'..=+-*+ -/$ .;*)- / !0)/$*).;./-/!0)/$*).; />
# );) ./ '.. .)*% /./#/ 3/ )!-*(/# . ' '..=
ENUMS AND SEALED CLASSES
171
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
sealedsealed classclass BrowserLocationBrowserLocation(openopen valval url: StringString) {
objectobject HomePageHomePage : BrowserLocationBrowserLocation("https://commonsware.com")
data classdata class BookmarkBookmark(overrideoverride valval url: StringString, valval name: StringString) : BrowserLocationBrowserLocation(url)
data classdata class HistoryEntryHistoryEntry(overrideoverride valval url: StringString, valval title: StringString, valval lastVisited: StringString) :
BrowserLocationBrowserLocation(url)
}
funfun main() {
println(BrowserLocationBrowserLocation.HomePageHomePage.url)
valval bookmark = BrowserLocationBrowserLocation.BookmarkBookmark("https://kotlinlang.org", "Kotlin!")
println(bookmark)
}
J!-*( N ' '.. .N$)/# '..**&K
*0/# )- ! -/*/# (/# .( .4*02*0')4*/# -) ./ '.. .*-*% /.
J >">; Browser.HomePageK>*;-0))$)"/#$..-$+/- .0'/.$)=
https://commonsware.com
Bookmark(url=https://kotlinlang.org, name=Kotlin!)
*2 1 -;/# - $.)*/#$)"+-/$0'-'4("$'*0/ BrowserLocation  $)". ' 
.*!->*0*0'- +' sealed 2$/# abstract )" //# .( - .0'/.>
Exhaustive When
. ' '..;'$& ) )0(;.0++*-/.) 3#0./$1 when>''+*..$' '.. .)
*% /./#/$- /'4 3/ )/# . ' '..- &)*2)2# )/# . ' '..$.
*(+$' >*'*)".4*0- when *1 -.''*!/#*. +*..$$'$/$ .;4*0*)*/) )
else '0. =
sealedsealed classclass BrowserLocationBrowserLocation(openopen valval url: StringString) {
objectobject HomePageHomePage : BrowserLocationBrowserLocation("https://commonsware.com")
data classdata class BookmarkBookmark(overrideoverride valval url: StringString, valval name: StringString) : BrowserLocationBrowserLocation(url)
data classdata class HistoryEntryHistoryEntry(overrideoverride valval url: StringString, valval title: StringString, valval lastVisited: StringString) :
BrowserLocationBrowserLocation(url)
}
funfun main() {
valval location: BrowserLocationBrowserLocation = BrowserLocationBrowserLocation.BookmarkBookmark("https://kotlinlang.org", "Kotlin!")
valval title = whenwhen (location) {
BrowserLocationBrowserLocation.HomePageHomePage -> "Home"
isis BrowserLocationBrowserLocation.BookmarkBookmark -> location.name
isis BrowserLocationBrowserLocation.HistoryEntryHistoryEntry -> location.title
}
ENUMS AND SEALED CLASSES
172
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
println(title)
}
J!-*( N ' '.. .)2# )N$)/# '..**&K
#$.+-$)/.=
Kotlin!
Smart Casts
#//**(4. (.*( 2#/0)- (-&' >*2 1 -;.*( /#$)"1 -4$)/ - ./$)"$.
"*$)"*)2$/#/# '// -/2*-)# .*!/# when>
location $. BrowserLocation 1-$' ; 0. 2  3+'$$/'4. //#//* /# /
/4+ =
valval location: BrowserLocationBrowserLocation = BrowserLocationBrowserLocation.BookmarkBookmark("https://kotlinlang.org",
"Kotlin!")
# 7-./-)#*!/# when *(+- . location 2$/# BrowserLocation.HomePage H
$! location $./#/.$)"' /*) object;/# )2 - /0-) "Home" ./# /$/' >#/$.!$-'4
)*-('>
*2 1 -;/# . *)-)## &./*. $!/# /4+ *! location $.
BrowserLocation.Bookmark>!$/$.;/# )2 - /0-)/# name +-*+ -/4>0/ location
$. BrowserLocation 1-$' ;) BrowserLocation * .)*/#1  name +-*+ -/4>
!2 /-4- ! - )$)"$/$- /'4;2 " /*(+$'  --*->*;/#$.=
println(location.name)
?- .0'/.$)=
error: unresolved reference: name
println(location.name)
*;2#4* .$/2*-&$)/# whenB
#/$. 0. */'$)- '$5 ./#/$!2 -  3 0/$)" location.name $)/#/
-)#; location (0./  BrowserLocation.Bookmark H*/# -2$. ;2 2*0'
#1 !$' /# is # &)2*0')*/ /&$)"/#/-)#>$) */'$)&)*2.
/#/ location (0./  BrowserLocation.Bookmark;*/'$)&)*2./#/$/$..! /*
- ! - ) /# name +-*+ -/4>
ENUMS AND SEALED CLASSES
173
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
# .( ++-*#$.0. !*-/# /#$--)#> ).! '4- ! - )
location.title /# - ; 0. $!2 -  3 0/$)"/#/-)#;2 &)*2/#/
location $. BrowserLocation.HistoryEntry;2#$##. title +-*+ -/4>
#$.$.)*/# -1-$/$*)*)*/'$)D.E.(-/./.F/#/2 .2 -'$ -$)/# **&>
# */'$)*(+$' -)0. &)*2' " *!#*2* " /. 3 0/ /*''*24*0/*
1*$()0'./.; 0. 2#/4*02)//**#++ )./* 1'$!*-*% /.$)
/#/.// >
(-/./.- very 0. !0';+-/$0'-'42# ) '$)"2$/# null 1'0 .;.2 2$''
3+'*- $))0+*($)"#+/ ->
Scenario: Valid and Invalid Data
*((*)+// -)$)1*'1$)". ' '.. .*( .2# )$)/ -/$)"2$/#.*(
. -1 -*-*/# - 3/ -)'.*0- *!/>.0''4;/# - - /2*($)*0/*( .!-*(
.0#)$)/ -/$*)=
I " /1'$/
I -  $1 .*( .*-/*! --*-; $/# -!-*(/# .*0- $/. '!J >">;
. -1$ - .+*)$)"2$/#*% /$)$/$)"/#/*0-- ,0 ./!$' K*-
!-*($)!-./-0/0- -*0)/#/.*0- J >">;) IOException !-*( $)"
0)' /*- #/# )/ -) /K
) 24/*(* '/#*. - .+*). .$./*#1 . ' '..#$ --#4/#/- +- . )/.
*/#/4+ .*!*0/*( .>!2 - "*$)"/*/- /'' --*-./# .( ;2 *0'0. /#
++-*#.#*2)2$/# BrowserLocation;2# - 2 #1 ($3*!'.. .)*% /.
$)/# . ' '..=
sealedsealed classclass ThingyResponseThingyResponse {
data classdata class ThingyThingy(valval something: StringString, valval somethingElse: IntInt) : ThingyResponseThingyResponse()
data classdata class OtherThingyThatWeMightGetOtherThingyThatWeMightGet(
valval like: FloatFloat,
valval whatever: StringString,
valval dude: BooleanBoolean
) : ThingyResponseThingyResponse()
objectobject InvalidInvalid : ThingyResponseThingyResponse()
}
ENUMS AND SEALED CLASSES
174
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
classclass WebServiceApiWebServiceApi {
funfun requestThingy(): ThingyResponseThingyResponse = ThingyResponseThingyResponse.InvalidInvalid
}
 - ;*0- WebServiceApi )- /0-) ThingyResponse !-*( requestThingy();)
/#/) )*(+..*/#+*.$/$1 )) "/$1 *0/*( .>$"#/)*2;/#/!0)/$*)$.
./0 *0/4'24.- /0-)$)" ThingyResponse.Invalid;0/2 *0'#1 !0''
. -1$ - ,0 ./# - /#/+-. .- .0'/.;" ) -/ . ThingyResponse.Thingy *-
ThingyResponse.OtherThingyThatWeMightGet *% /.;).**)>
'' -.*! requestThingy() ))4/#$)" '. /#/" /. ThingyResponse )0. )
3#0./$1 when /*#)' ''*!/# +*..$$'$/$ .>
)*/# -1-$)/$./*#1 Invalid  '..$)./ *!.$)"' /*)*% />#/'..
)/# )#*' /$'.*!2#/2 )/2-*)"=
I --*-* !-*(/# . -1 -
I ) 3 +/$*) /#/2.-$. 4/# ) /2*-&G
I />
Scenario: Loading/Content/Error
*((*)+// -)!*-$.=
I ) /*.#*2.*( '*$)".// ;.0#.+-*"- ...+$)) -;2#$' $.&
*-) /2*-&G$.*)"*$)"
I # )2 " //# *)/ )//*$.+'4;$.+'4/#/
I !.*( /#$)"2 )/2-*)";.#*2.*( .*-/*! --*-.//
$($'-/*/# 1'$@)@$)1'$/. )-$**1 ; sealed class ' /.0.(* '
/# . /#- .// .2$/#.$)"' *- /4+ =
sealedsealed classclass SomethingViewStateSomethingViewState {
objectobject LoadingLoading : SomethingViewStateSomethingViewState()
data classdata class ContentContent(valval goodStuff: ListList<StuffStuff>) : SomethingViewStateSomethingViewState()
objectobject ErrorError : SomethingViewStateSomethingViewState()
}
*- 3(+' ;$))-*$++ 1 '*+( )/;4*0($"#/ ($/$)./) .*!/#$.
SomethingViewState !-*( ViewModel;+ -#+.0.$)"/#  /+& LiveData '..*-
ENUMS AND SEALED CLASSES
175
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
StateFlow !-*(*/'$)D.*-*0/$) .>*0-'4 -J/$1$/4;!-"( )/;*(+*.' K
*0'*. -1 /#*. .// .)0. ) 3#0./$1 when /*#)' ''+*..$' .//
.0@/4+ .JLoading; Content; ErrorK>
Limitation: Location
$- /.0'.. .*!/# . ' '..(0./ $)/# .( 7' /#/*)/$)./# . ' 
'..$/. '!># 4*)*/)  ..-$'4) /* ) ./ $).$ /# . ' '..;/#*0"#>
*;)*-(''4;/#$.$.+ -! /'41'$?$)(*./*/'$) )1$-*)( )/.=
sealedsealed classclass BrowserLocationBrowserLocation(openopen valval url: StringString)
objectobject HomePageHomePage : BrowserLocationBrowserLocation("https://commonsware.com")
data classdata class BookmarkBookmark(overrideoverride valval url: StringString, valval name: StringString) : BrowserLocationBrowserLocation(url)
data classdata class HistoryEntryHistoryEntry(overrideoverride valval url: StringString, valval title: StringString, valval lastVisited: StringString) :
BrowserLocationBrowserLocation(url)
funfun main() {
valval location: BrowserLocationBrowserLocation = BookmarkBookmark("https://kotlinlang.org", "Kotlin!")
valval title = whenwhen (location) {
HomePageHomePage -> "Home"
isis BookmarkBookmark -> location.name
isis HistoryEntryHistoryEntry -> location.title
}
println(title)
}
J!-*( N*)@ ./  ' '.. .N$)/# '..**&K
-'41 -.$*).*!*/'$)- ,0$- . ' '...0@/4+ ./* ) ./ $)/# . ' 
'..>'.*;.*(  )1$-*)( )/- ,0$- ) ./$)">
*2 1 -;(*./*/'$)+-*% /.- )*/2-$// )$))- 0.$)"(* -)
1 -.$*).*!*/'$);.*#1$)".0'..*!/# . ' '.. + -*!/# . ' 
'..$.7) ># E(0./@ @) ./ F-0' ++'$ .!*- .kts 7' .;)*/ .kt 7' .>
!4*0 '- $- /.0'..*!/# . ' '../* open;/#*0"#;4*0) 3/ )
/#/'..)*-(''4; 1 )!-*(*/# -.*0- 7' .>*;$)- "0'-*/'$)+-*% /J)*/
K;$!4*0#1 *) .*0- 7' 2$/#=
importimport java.time.Instantjava.time.Instant
sealedsealed classclass BrowserLocationBrowserLocation(openopen valval url: StringString)
ENUMS AND SEALED CLASSES
176
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
objectobject HomePageHomePage : BrowserLocationBrowserLocation("https://commonsware.com")
openopen classclass BookmarkBookmark(overrideoverride valval url: StringString, openopen valval name: StringString) :
BrowserLocationBrowserLocation(url)
data classdata class HistoryEntryHistoryEntry(overrideoverride valval url: StringString, valval title: StringString, valval lastVisited:
InstantInstant) : BrowserLocationBrowserLocation(url)
?)$))*/# -.*0- 7' ;4*0#1 =
classclass BrokenSealBrokenSeal(overrideoverride valval url: StringString, overrideoverride valval name: StringString) : BookmarkBookmark(url,
name)
?/# )4*0- > - ; Bookmark $.(-& .) open '..;2$/#*/#*!$/.
*)./-0/*-+-*+ -/$ . $)" open .2 ''Jurl $. '- . open 0+$)
BrowserLocationK>.- .0'/;2 ) 3/ ) Bookmark !-*(. +-/ */'$)
.*0- 7' ;. BrokenSeal * .>
ENUMS AND SEALED CLASSES
177
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Generics
!4*0#1 *) 1 1 '*+( )/;4*0- )**0/!($'$-2$/#" ) -$.;
*/# -2$. &)*2).E /#4/#*0.))"' -& /.F=
classclass TouchedByAnAngleTouchedByAnAngle<T> {
privateprivate T thingy;
publicpublic T getThingy() {
returnreturn thingy;
}
publicpublic void setThingy(T replacement) {
thingy = replacement;
}
}
!4*0'$& 1D." ) -$.;- ./..0- /#/*/'$)#.1 -4.$($'-.4./ (>
!4*0& +" //$)"*)!0. 41D." ) -$.;- ./..0- /#/;$)/$( ;4*02$''
%0./.*)!0. 4*/'$)D.$(+' ( )//$*)>
);$!4*0- )*//#/!($'$-2$/#1D." ) -$.?/#/D.2#//# ) 3/. /$*)$.
!*->
OK, What Are These For Again?
*/'$);'$& 1;$.E./-*)"'4/4+ F>#$.( ). #1-$' ;+-*+ -/4;+-( / -;
)- /0-)1'0 #1 .+ $7/4+ ;.0#. ArrayList *- Axolotl *- Thingy>#
*(+$' -2$''// (+//* ).0- /#/4*0*)'4+-*1$ *% /.*!1'$/4+ .!*-/# .
/#$)".>
179
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
.2/#/&$) /# #+/ -*)'.. .=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critter: AnimalAnimal = FrogFrog()
ifif (critter isis FrogFrog) println("Ribbit!") elseelse println("Ummm... whatever noise an axolotl makes!")
}
J!-*( N# &$)")# -$/) $$.N$)/# '..**&K
 - ; critter $.1-$' *!/4+ Animal> )..$"))$)./) *! Frog /*
critter; 0. Frog 3/ ). Animal;).* Frog $.*(+/$' /4+ >0/2
*0')*/..$")) Int /* critter;.*/'$)D. Int /4+ * .)*/ 3/ ) Animal>
*2;.0++*. /#/2 2)/ '$./*!)$('.>
*0'0. /# listOf() "'*'!0)/$*);.0++'$ 4*/'$)D../)-'$--4;/*
- / '$./*!)$('.=
valval critters = listOf(FrogFrog(), AxolotlAxolotl())
-*(/4+ .! /4./)+*$)/;/#*0"#;$/- ''4$. List *! Animal *% /.=
valval critters: ListList<AnimalAnimal> = listOf(FrogFrog(), AxolotlAxolotl())
 - 2 - .4$)"/#/ critters )*)'4#*' Animal *% /.> ))*/+0/)
Int $)/*/# '$./;. Int $.)*/) Animal>
4+ .'$& /# List $)/ -! )/# ArrayList $(+' ( )//$*)*! List )0.
" ) -$./**)./-$)/# /4+ *!*% /./#//# 42*-&2$/#># - !*- ;2# )2 -
/-4$)"/*2*-&2$/#'$./*!)$('.;2 *)*/#1 /*2*--4*0/$ )/''4
)*0)/ -$)")$)/ " -;./-$)";*-.*( /#$)" '. >
Instantiation with Generics
#1 ! 2*+/$*).!*-- /$)"*% /.) '-$)"" ) -$/4+ .>
GENERICS
180
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
The Formal Way
# *8$'24!*-''*!/#$.$./* '- /# /4+ */#2# - 2 - 0.$)"$/J >">;
+-*+ -/4K)2# )- /$)"/# $)./) =
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critters: ArrayListArrayList<AnimalAnimal> = ArrayListArrayList<AnimalAnimal>()
critters.add(FrogFrog())
critters.add(AxolotlAxolotl())
}
 - ;2 - 1 -4.+ $7''4.//$)"/#//# critters +-*+ -/4#*'.) ArrayList
*! Animal )$. $)"$)$/$'$5 2$/#) ArrayList *! Animal>
!2 2)/ ;2 *0'.&$+/# " ) -$/4+ *)/# $)$/$'$5 -=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critters: ArrayListArrayList<AnimalAnimal> = ArrayListArrayList()
critters.add(FrogFrog())
critters.add(AxolotlAxolotl())
println(critters::classclass)
println(critters.map { it::classclass.toString() }.joinToString())
}
J!-*( N ) -$.N$)/# '..**&K
 - 2 #1 /2*$/$*)''$) .;.*'..**&#..*( *0/+0/=
I +-$)//# '..*! critters>#$.2$''.#*20+. ArrayList;)*/
ArrayList<Animal>; 0. " ) -$.- *)'40. /*(+$' /$( >
GENERICS
181
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
I +-$)//# '.. .*!/# ( ( -.*!/# '$./;2#$#2$''.#*20+.
class Frog, class Axolotl
*0'/-4/*.0./$/0/ .0+ -/4+ ;.0#. List;!*-/# +-*+ -/4/4+ =
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critters : ListList<AnimalAnimal> = ArrayListArrayList<AnimalAnimal>()
critters.add(FrogFrog())
critters.add(AxolotlAxolotl())
}
? 3 +//#//#$.* .)*/2*-&=
error: unresolved reference: add
critters.add(Frog())
^
error: unresolved reference: add
critters.add(Axolotl())
^
.$//0-).*0/;*/'$)D. List /4+ $.$((0/' =4*0))*/) 2*% /./*/#
'$./!/ -$)$/$''4- /$)"$/> 2$'' 3+'*- $((0/' /4+ .(*- $))0+*($)"
#+/ ->
*-/0)/ '4;*/'$)'.*#. MutableList $)/ -! /#/ ArrayList #++ )./*
$(+' ( )/;.*2 )0. =
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critters : MutableListMutableList<AnimalAnimal> = ArrayListArrayList<AnimalAnimal>()
GENERICS
182
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
critters.add(FrogFrog())
critters.add(AxolotlAxolotl())
}
Implied Types
0-*-$"$)' 3(+' ;0.$)" listOf();$)*/ '- /4+ >*-* ./#$..'$"#/'4@
(*$7  3(+' =
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critters = mutableListOf(FrogFrog(), AxolotlAxolotl())
critters.add("ribbit!")
}
# /2*#)" .- =
I 0. mutableListOf();2#$#$."'*'!0)/$*)!-*(*/'$)D../)-
'$--4/#/- / . MutableList 2$/#.*( $)$/$'*)/ )/.<)
I /-4$)"./-$)"/*/#/'$./
#$.!$'.2$/#*(+$'  --*-=
error: type mismatch: inferred type is String but Animal was expected
critters.add("ribbit!")
^
#/#++ ) $./#//# */'$)*(+$' -'**& //# /4+ *!*% /./#/2
+.. $)/* mutableListOf() ) / -($) /# *((*).0+ -/4+ ;/# ) $ 
/#/ critters (0./ 1-$' *!/#//4+ >)/#$.. ; Frog() ) Axolotl() -
*/# Animal *% /.;.**/'$)2$''/- / critters .$!$/2 -  MutableList *!
Animal>
# & 4$./#/*/'$)2$''7"0- *0//# *((*).0+ -/4+ >!2 - ''4*2)//*
''*2./-$)".$)*0-'$./;2 *0'/ #*/'$)/#/4$)"./-$)"//# *0/. /=
GENERICS
183
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval critters = mutableListOf(FrogFrog(), AxolotlAxolotl(), "ribbit!")
critters.add("[insert axolotl sound here]")
println(critters::classclass)
println(critters.map { it::classclass.toString() }.joinToString())
}
J!-*( N(+'$  ) -$4+ N$)/# '..**&K
#$.*(+$' .%0./7) ; 0. */'$)#. $ /#/ critters $.1-$' *!
.*( /4+ /#/ Frog; Animal;) String ''#1 $)*((*)>.$//0-).*0/;/#/
*/'$)/4+ $.''  Any;)$/. -1 ../# . '..!*-''*/'$)'.. .J&$)/*
#*2 Object $./# . '..!*-''1'.. .K>
#$.$.)*/'$($/ /*.*( .*-/*!("$"'*'!0)/$*)=
importimport java.util.concurrent.atomic.AtomicReferencejava.util.concurrent.atomic.AtomicReference
data classdata class PhysicistPhysicist(valval firstName: StringString)
valval oppenheimer = AtomicReferenceAtomicReference(PhysicistPhysicist("Robert"))
 - ;2 0. 1D. AtomicReference '..;2#$#+-*1$ ./#- @.! 24/*
 ..1'0 > *)*/ 3+'$$/'4.// /#/ oppenheimer $.) AtomicReference *!
Physicist H*/'$)7"0- ./#/*0/>!2 /-$ ''$)"
oppenheimer.set("Albert");$/2*0'!$'2$/#=
error: type mismatch: inferred type is String but Test.Physicist! was expected
oppenheimer.set("Albert")
^
*;$)" ) -';4*0).&$+/# /4+  '-/$*)*!1-$' ;.*'*)"./# /4+
/#/*/'$) $ ./*0. ;. *)4*0-$)$/$'$5 -;$./# /4+ /#/4*02)//*0. >
/# -2$. ; '- /# /4+  3+'$$/'44*0-. '!>
GENERICS
184
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Applying Generics to Classes and Interfaces
*0(42)//*.0++*-/" ) -$.$)4*0-*2)'.. .)$)/ -! .>#$.2*-&.
'*/'$& #*2$/* .$)1;2# - 4*00. T )*//$*)/*$)$/ 2# - 4*0 +/
1-4$)"/4+ >
Use in Class Declaration
*- 3(+' ;4*0)- / '../#/)2*-&2$/#+-/$0'-/4+ =
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
data classdata class TransportTransport<TT>(varvar passenger: TT)
funfun main() {
valval kermit = FrogFrog()
valval critterCarrier = TransportTransport(kermit)
println(critterCarrier.passenger::classclass)
}
J!-*( N*0-2) ) -$'.. .N$)/# '..**&K
Transport )#)' )*% /*!)4/4+ ;0/+-/$0'-$)./) *! Transport
)*)'4#)' *% /.*!.$)"' /4+ J*-.0/4+ .;2# - ++'$' K># )2
+.. Frog $)./) $)/*/# Transport *)./-0/*-;2 '*&/# - .0'/$)" Transport
/*/-).+*-/$)" Frog *% /.>!2 // (+//*- ..$") passenger 2$/#)*/# -/4+ =
critterCarrier.passenger = "This is not an Animal"
?2 " /*(+$'  --*-=
error: type mismatch: inferred type is String but Test.Frog was expected
critterCarrier.passenger = "This is not an Animal"
^
#//# Transport ).0++*-/2$'' . *)/# /4+ *!/# 1-$' *-
+-*+ -/4#) /*$/>*;2 )"$1  Transport $/(*- 9 3$$'$/44+..$)"$)
) Animal;$)./ *! Frog;/*$/=
GENERICS
185
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
data classdata class TransportTransport<TT>(varvar passenger: TT)
funfun main() {
valval kermit: AnimalAnimal = FrogFrog()
valval critterCarrier = TransportTransport(kermit)
println(critterCarrier.passenger::classclass)
critterCarrier.passenger = AxolotlAxolotl()
println(critterCarrier.passenger::classclass)
}
J!-*( N. ) -$.*0)/N$)/# '..**&K
#$.2*-&. 0. )*2 kermit $./4+ .) Animal;.* critterCarrier $.
Transport *! Animal;.*2 )- +' *0- Frog 2$/#) Axoltl $! .$- >
Use with Supertypes
*0''.*- / .0/4+ /#/ '- ..0++*-/!*-.+ $7/4+ =
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
interfaceinterface TransportTransport<TT> {
abstractabstract funfun getPassenger(): TT
}
data classdata class VanVan(valval animal: AnimalAnimal) : TransportTransport<AnimalAnimal> {
overrideoverride funfun getPassenger() = animal
}
funfun main() {
valval kermit = FrogFrog()
valval critterCarrier = VanVan(kermit)
GENERICS
186
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
println(critterCarrier.getPassenger()::classclass)
}
J!-*( N ) -$.)0+ -/4+ .N$)/# '..**&K
 - ;2#$' Transport )#)' )4/4+ ; Van $.)$(+' ( )//$*)*! Transport
/#/$.- ./-$/ /*/-).+*-/$)" Animal *% /.>
*/ /#/2 )0. /# T /4+ +' #*' -!*-+-*+ -/$ .;+-( / -.;)H$)/#
. *! getPassenger() H- /0-)1'0 .>
Upper Bounds
.$(+'  '-/$*)*! T *0' )4/4+ >*( /$( .;2 ) /*'$($/$//**)'4
  -/$)/4+ .># .$(+' ./24/**/#/$./* '- T $/'$& '..*-
$)/ -! ;0.$)"*'*))/4+ /* '- 2#/ T (0./$)# -$/!-*(=
openopen classclass ThingyThingy
openopen classclass AnimalAnimal : ThingyThingy()
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
interfaceinterface TransportTransport<TT : ThingyThingy> {
abstractabstract funfun getPassenger(): TT
}
data classdata class VanVan(valval animal: AnimalAnimal) : TransportTransport<AnimalAnimal> {
overrideoverride funfun getPassenger() = animal
}
funfun main() {
valval kermit = FrogFrog()
valval critterCarrier = VanVan(kermit)
println(critterCarrier.getPassenger()::classclass)
}
J!-*( N ) -$.)++ -*0).N$)/# '..**&K
*2 Transport ))*//-).+*-/)4/#$)"H$/$.'$($/ /*$)./) .*! Thingy>
$) ) Animal $. Thingy;2 )./$''/-).+*-/)$('.>0/2 ))*/- / 
GENERICS
187
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Transport *! String; 0.  String * .)*/ 3/ )!-*( Thingy>!2 /-4
)424=
openopen classclass ThingyThingy
interfaceinterface TransportTransport<TT : ThingyThingy> {
abstractabstract funfun getPassenger(): TT
}
data classdata class StringVehicleStringVehicle(valval value: StringString) : TransportTransport<StringString> {
overrideoverride funfun getPassenger() = value
}
?2 2$)0+2$/#*(+$'  --*-=
error: type argument is not within its bounds: should be subtype of 'Test.Thingy'
data class StringVehicle(val value: String) : Transport<String> {
^
Generics, WTF?
 ) -$.- *(+'$/ ;)/#//$ .$)/*.*(  .*/ -$@'**&$)"*/'$).4)/3/#/
2 2$'' 3+'*- (0#'/ -$)/# **&=
I # )4*0. /# out & 42*-;.0#.$) class Something<out T>;/#/$.
*/'$)D.++-*#!*-  '-$)"E*1-$) F;./$+0'/$)"/#/)$)./) *!
Something $.''*2 /* produce T *% /.J >">;- /0-)/# (!-*(!0)/$*).K
0/)*/ consume /# (J >">; +//# (.+-( / -.K
I # )4*0. /# in & 42*-;.0#.$) class Something<in T>;/#/$.
*/'$)D.++-*#!*-  '-$)"E*)/-1-$) F;./$+0'/$)"/#/)$)./)
*! Something $.''*2 /* consume T *% /.0/)*/ produce /# (
I # )4*0. <reified T>?2 ''; /#/$.$80'//* 3+'$)
GENERICS
188
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Exceptions
))$ '2*-';)*/#$)"2*0' 1 -"*2-*)"2$/#*0-++.>
))$ '2*-';/# 0/#*-*!/#$.**&2*0'#1 !0''# *!#$->
#$.$.)*/)$ '2*-'>
.2$/#(*./(* -)+-*"-(($)"')"0" .;2# )/#$)"."*2-*)"; 3 +/$*).
" /-$. > 2$'') /* ' /*#)' /#*.  3 +/$*).2# )/# 4*0-;*)
24*-)*/# ->
*/'$)D. 3 +/$*).4./ ($.1 -4.$($'-/*/#/*!1; 3 +//#//# - - )*
# &  3 +/$*).>
Catching Exceptions
'..$. )-$*2# - *0-++.)!$'$.2$/# NullPointerException>
2$'' 3+'*- )0''$'$/4$)"- / - /$' $))0+*($)"#+/ -;.*/'$)/& .
./ +./*/-4/*($)$($5  NullPointerException>*2 1 -;4*0)./$''2$)0+
-.#$)"$!4*0/-4 ..$)" null $)++-*+-$/ '4;.0#.2 - *$)"# - =
varvar thisIsReallyNull: StringString? = nullnull
println(thisIsReallyNull)
println(thisIsReallyNull!!.length)
J!-*( N3 +/$*).N$)/# '..**&K
#$.*(+$' .7) ;0/$/-.# ./-0)/$( 2$/# NullPointerException> 2$''
189
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
.  3/'42#/ ? ) !! * $)/# #+/ -*))0''$'$/4>
*-)*2;/#*0"#;' /D.!*0.*)/# !//#/2 - " //$)") 3 +/$*)>)/#$.. ;
/#  3 +/$*)$.1 -4-/$7$';0//# - - ()4+' .2# - ) 3 +/$*)(4
3+ / .*( 2#/>*- 3(+' ;$!4*0/-4 ..$)"/# )/ -) /;/# - (4 ''
.*-/.*!+-*' (.- .0'/$)"$)''.*-/.*! 3 +/$*).>
Traditional Try-Catch
*/'$).0++*-/. try ) catch;0.$)".4)/31 -4.$($'-/*/#/*!1=
trytry {
varvar thisIsReallyNull: StringString? = nullnull
println(thisIsReallyNull)
println(thisIsReallyNull!!.length)
}
catchcatch (e: ExceptionException) {
println("ERROR: '$e'")
}
J!-*( N/-4)/#N$)/# '..**&K
*/#) 3 +/$*);4*0=
I -+/# * /#/($"#//-$"" -/#  3 +/$*)$)'*&;+-   4 try;
/# )
I *''*2/#/'*&2$/# catch '*&;2# - 4*0$)$/ 2#/ 3 +/$*)4*0
-  3+ /$)"
 - ;2 2-+/# null - ! - ) $)/# try '*&>!H*-;-/# -;2# )H/#/*
/#-*2.) 3 +/$*);2 2$''" /*)/-*'$)*0- catch '*&;2# - 2 )*
.*( /#$)">
 /2 )/# catch & 42*-)/# '*&$.+-( / -@./4'  '-/$*)$)
+- )/# . .;$)$/$)"=
I #/.*-/*! 3 +/$*)2 - '**&$)"/*/#
I #/)( /*"$1 /#$. 3 +/$*);/#/2 )0. $).$ /# * $)/#
catch '*&/* 3($) /#  3 +/$*)$/. '!
 - ;)4/#$)"/#/ 3/ ).!-*( Exception 2*0' 0"#/)#)' 4*0-
catch '*&>
EXCEPTIONS
190
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Cascading Catches
!4*02)//*#)' $6 - )//4+ .*! 3 +/$*).$6 - )/'4;4*0)#1 (0'/$+'
catch '*&.=
trytry {
varvar thisIsReallyNull: StringString? = nullnull
println(thisIsReallyNull)
println(thisIsReallyNull!!.length)
}
catchcatch (npe: NullPointerExceptionNullPointerException) {
println("You tried doing something unfortunate with null. Stop that.")
}
catchcatch (e: ExceptionException) {
println("ERROR: '$e'")
}
J!-*( N0'/$+' /#'*&.N$)/# '..**&K
 - ;2# )/# null - ! - ) /#-*2.) NullPointerException;2 2$'' 3 0/ /#
7-./ catch '*&>/# -2$. ;$!$/2 - /*/#-*2.*( */# -/4+ *! Exception;2
2$'' 3 0/ /# . *)/#'*&>
*/ ;/#*0"#;/#/*/'$)* .)*/.0++*-/(0'/$+' /4+ .$).$)"' catch;/# 24
4*0)$)1=
trytry {
// something that might throw multiple types of exceptions
}
catchcatch (ThisExceptionThisException | ThatExceptionThatException ex) {
// handle those two exception types
}
What You Might Catch
#  /$'.*!2#/ 3 +/$*).) /#-*2)2$''1-44$-0(./) >)
+-/$0'-;/# */'$) )1$-*)( )/2$''+'4(%*--*' # - >*- 3(+' ;
java.lang.ArithmeticException $.1/#$)"/#/4*0($"#//#*)*/'$)G
>/# -*/'$) )1$-*)( )/.;.0#.*/'$)G;2$'')*/&)*22#/
java.lang.ArithmeticException $.>
1 )2$/#$)) )1$-*)( )/;/#  /$'.(41-4>*- 3(+' ;$)1; Exception $.
.0'..*! Throwable>) Exception $..*( /#$)"/#//# 1')"0"  .$") -.
EXCEPTIONS
191
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
! '//#/++..#*0' ' /*#)' >) Error;/#*0"#;$.)*/# -/4+ *!
Throwable /#//# ')"0"  .$") -.*).$ - /* 0)- *1 -' >*- 3(+' ;
) OutOfMemoryError $..*( /#$)"/#//# 1 .$") -./#*0"#/2..*( /#$)"
/#/.#*0'- .0'/$)/#  /#*!4*0-+-* ..>
4+$''4;$) catch '*&;4*0$ )/$!4 3 +/$*)./#/4*02)//*/# 0.
4*0#1 .+ $7'*'24./*- *1 -!-*($/>!4*0) /-0 E/# 1 -4/#$)"F
catch '*&;$)*/'$)G;4*02*0'/# Throwable;)*/) Exception>
A Missed Catch
) 3 +/$*)/#/$.)*/0"#/. .0+/# E''./&F/*2#/ 1 -/# ) 3/
!0)/$*)$.=
funfun itsGonnaBlow() {
varvar thisIsReallyNull: StringString? = nullnull
println(thisIsReallyNull)
println(thisIsReallyNull!!.length)
}
funfun main() {
trytry {
itsGonnaBlow()
}
catchcatch (npe: NullPointerExceptionNullPointerException) {
println("You tried doing something unfortunate with null. Stop that.")
}
catchcatch (e: ExceptionException) {
println("ERROR: '$e'")
}
}
J!-*( N3 +/$*).)/# ''/&N$)/# '..**&K
 - ; itsGonnaBlow() /-$"" -./#  3 +/$*);0/2 *)*/#1  tryGcatch
./-0/0- $)/#/!0)/$*)>*;2# ) itsGonnaBlow() /#-*2./#  3 +/$*);$/" /.
+.. /*2#* 1 -''  itsGonnaBlow();).**)>
Try as an Expression
$& if ) when; try J2$/#$/...*$/  catch '*&.K- +- . )/.) 3+- ..$*);
)4*0)0. $/.- .0'/.4*02*0')4*/# - 3+- ..$*);.0#...$")$)"$//*
1-$' *-+-*+ -/4>
EXCEPTIONS
192
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
)/# . *! if ) when;/# - .0'/*!/#  3+- ..$*)$.. *)2#$#*!/#
-)# .4*0/& >)/# . *! try;/# - .0'/*!/#  3+- ..$*)$.=
I # 1'0 *!/# './ 3+- ..$*)*!/# try '*&;$!/# - $.)* 3 +/$*)
-$. $)/#/'*&;*-
I # 1'0 *!/# './ 3+- ..$*)*!/# catch '*&;$!*) *!/# catch '*&.
0"#/) 3 +/$*)
funfun lengthifier(nullAllowedButNotReally: StringString?): IntInt {
returnreturn nullAllowedButNotReally!!.length
}
funfun main() {
valval result = trytry {
lengthifier(nullnull)
}
catchcatch (e: ExceptionException) {
-1
}
println(result)
}
J!-*( N/-4.)3+- ..$*)N$)/# '..**&K
 - ;2 *(+0/ /# ' )"/#*! String $)!0)/$*);)2 2-+/#/!0)/$*)
''$) tryGcatch ./-0/0- > result 2$'' $/# - =
I # ' )"/#*!/# ./-$)";*-
I -1 $!2 -.#;.0#.2$/# NullPointerException
)/#$.. ;2 - /-4$)"/*" //# ' )"/#*! null;.*2 " / -1 ./# - .0'/*!*0-
try 3+- ..$*)>
Finally, Don’t Forget finallyfinally
.2$/#1;*/'$)'.**6 -. finally '*&/#/4*0)#$)*)/*/#  )*!
4*0- tryGcatch ./-0/0- >#$.'*&D.* 2$''  3 0/  $/# -=
I !/ -/# try '*&D.*)/ )/.;$!/# - - )* 3 +/$*)./#-*2)4/#/
'*&;*-
I !/ -/# catch '*&D.*)/ )/.;$!/#/ catch 0"#/) 3 +/$*)-$. 4
/# try '*&
EXCEPTIONS
193
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
4+$''4;4*00. /# finally '*&/* ).0- /#/ 1 -4/#$)"$.' ) 0+!-*(
2#/ 1 -(4J*-(4)*/K#1 #++ ) $)/# try=
trytry {
// TODO something that might blow up
}
catchcatch (e: ExceptionException) {
// TODO something to deal with the blow up when it blows up
}
finallyfinally {
// TODO clean up afterwards
}
*/ /#/$!4*00. /# try .) 3+- ..$*);/# finally '*&#.)*$(+/*)
/# - .0'/*!/#  3+- ..$*)># - .0'/2$''./$'' . *)/# try - .0'/*-/#
catch - .0'/H/# finally * $. 3 0/ 0/* .)*/#)" /# - .0'/>
Raising Exceptions
- ,0 )/'4;2 *)*/) /*-$. *0-*2) 3 +/$*).> %0./ '2$/#/# *) .
/#/-$. !-*(/# * /#/2 2-$/ ;2# - /#*.  3 +/$*)." /-$. 4*/'$);
'$--$ .; />
*2 1 -;!-*(/$( /*/$( ;4*0($"#/2)//*/#-*2) 3 +/$*)4*0-. '!>
 - ;*) "$);*/'$)2*-&.'$& 1>*/#-*2) 3 +/$*)=
I - / )$)./) *!) 3 +/$*)'..;.0#. IllegalArgumentException
I . /#/2$/#/# throw & 42*-
funfun lengthifier(nullAllowedButNotReally: StringString?): IntInt {
ifif (nullAllowedButNotReally == nullnull) throwthrow ExceptionException("Please do not pass null to me!")
returnreturn nullAllowedButNotReally!!.length
}
funfun main() {
println(lengthifier(nullnull))
}
J!-*( N#-*2$)"3 +/$*).N$)/# '..**&K
 - ;2 ()0''4# &/*. $!2 -  $1 null $) lengthifier();)2 /#-*2
*0-*2) Exception $!/#/$./# . >*;$!2 -0)/#$.;2 " /*0- Exception=
Exception: Please do not pass null to me!
EXCEPTIONS
194
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Defining Exceptions
*0($"#/#1 ) /*- / 4*0-*2)0./*( 3 +/$*).>
*- 3(+' ;.0++*. 4*0- ''$)" . -1$ >*02$''" / --*-.&!-*(
/#/ . -1$ $)1-$ /4*!24.;.0#.=
I --*-- .0'/* .
I --*-$)!*-(/$*) )* $)/# J*-2#/ 1 -K+4'*. )/&4
/# . -1 -
*0($"#/ $ /**)1 -//#*.  --*-*)$/$*).$)/* 3 +/$*).>
.2$/#1;4*0)- / .0'.. .*! Exception /*- +- . )/4*0-0./*(
3 +/$*).=
classclass GoAwayGoAway(message: StringString) : ExceptionException(message)
funfun lengthifier(nullAllowedButNotReally: StringString?): IntInt {
ifif (nullAllowedButNotReally == nullnull) throwthrow GoAwayGoAway("Please do not pass null to me!")
returnreturn nullAllowedButNotReally!!.length
}
funfun main() {
valval result = trytry {
lengthifier(nullnull)
}
catchcatch (away: GoAwayGoAway) {
-2
}
catchcatch (e: ExceptionException) {
-1
}
println(result)
}
J!-*( N0./*(3 +/$*)'.. .N$)/# '..**&K
 - ;2 - / 0./*( GoAway 3 +/$*))/#-*2$/-/# -/#)) Exception>
!4*0#1 . /*!- '/  3 +/$*)/4+ .;'..#$ --#4J0.$)" open; abstract;
*- sealedK(4 0. !0'=
openopen classclass MathBoomMathBoom(message: StringString) : ExceptionException(message)
classclass DivideByZeroBoomDivideByZeroBoom : MathBoomMathBoom("Please do not divide by zero")
EXCEPTIONS
195
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun divider(numerator: DoubleDouble, denominator: DoubleDouble): DoubleDouble {
ifif (denominator == 0.0) throwthrow DivideByZeroBoomDivideByZeroBoom()
returnreturn numerator / denominator
}
valval result = trytry {
divider(1.0, 0.0).toString()
}
catchcatch (e: MathBoomMathBoom) {
"whatever"
}
println(result)
Checked vs. Unchecked Exceptions
#$' /# *) +/*!# &  3 +/$*).$.)*/0)$,0 /*1;$/$.! /0- (*./
*((*)'4..*$/ 2$/#/#/')"0" >)1;# &  3 +/$*)$. '- .
+-/*!( /#*;2#$#$)$/ .2#/.*-/.*! 3 +/$*).$/($"#//#-*2=
publicpublic void doSomethingPotentiallyTroublesome(FileFile f) throwsthrows FileNotFoundExceptionFileNotFoundException {
ifif (!f.exists()) throwthrow newnew FileNotFoundExceptionFileNotFoundException(f.getAbsolutePath()+" does not
exist!");
// TODO other stuff, now that we have a valid value
}
'' -.*!( /#*/#//#-*2.# &  3 +/$*). must #)' /#*.  3 +/$*).
.*( #*2; $/# -4 3+*.$)"/#*.  3 +/$*)..+-/*!/# $-*2)( /#**-4
/#$)"/# ()*$)".*( /#$)";.0#./#-*2$)".*( /4+ *!
RuntimeException;./#*. - 0)# &  3 +/$*).=
publicpublic void troublesomeWrapper(FileFile f) {
trytry {
doSomethingPotentiallyTroublesome(f);
}
catchcatch (FileNotFoundExceptionFileNotFoundException e) {
throwthrow newnew IllegalStateException("I'm in a bad state", e);
}
}
# &  3 +/$*).#1  ) / '*/*1 -/# 4 -.>/++ -./#//#
*). ).0.*+$)$*)$./#/# &  3 +/$*).2 - )$)/ - ./$)"$ /#/0. 
EXCEPTIONS
196
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
(*- +-*' (./#)/# 42 - 2*-/#>
.- .0'/;*/'$)* .)*/*6 -# &  3 +/$*).>)./ ;*/'$)/- /.''
3 +/$*)..0)# & ;2#$#$./# ++-*#/& )4()4*/# -+-*"-(($)"
')"0" .>
EXCEPTIONS
197
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Annotations
*./1 1 '*+ -.- !($'$-2$/#))*//$*).># . - /# @@+- 73 1'0 .
/#/ *-/ '.. .;( /#*.;7 '.;).**)=
@RunWith(AndroidJUnit4AndroidJUnit4.class)
publicpublic classclass ExampleInstrumentedTestExampleInstrumentedTest {
@Test
publicpublic void useAppContext() {
ContextContext appContext=InstrumentationRegistryInstrumentationRegistry.getTargetContext();
assertEquals("com.commonsware.jetpack.hello", appContext.getPackageName());
}
}
 - ; @RunWith ) @Test - ))*//$*).># . #++ )/* !-*()$/
J.+ $7''4)$/VK)- 0. /*/ #)$// ./-0)) -=
I *2/*-0)+-/$0'-/ ./'..JE/#$./ ./'..) ./* -0)2$/#/#
..$./) *! AndroidJUnit4FK
I #$#( /#*.- +- . )// ./( /#*./#/.#*0'  3 0/ 2# )
-0))$)"/# / ./
*/'$)D.))*//$*).- 1 -4.$($'-/*1D.;2$/#! 2.4)/3$6 - ) .>
Where Annotations Come From
# 1 3(+' *1 .#*2.+$-*!))*//$*).!-*()$/V>)$/V$.
))*//$*)@. <$/.0++'$ .,0$/ ! 2))*//$*).>
1 -'';/# - - ! 2$6 - )/+' .2# - ))*//$*).)*( !-*(>
199
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Kotlin
*/'$)#..*( ))*//$*)./#/- +-/*!/# */'$)./)-'$--4>) /#/2
2$'' 3($) $)/#$.#+/ -$.*/'$)D./& *)/# @Deprecated ))*//$*);0. /*
 )*/ .*( * J'..;( /#*;7 '; />K/#/.#*0')*'*)" - 0. >
Java
!4*0- 0.$)"*/'$)GJ >">;!*-)-*$ 1 '*+( )/K;1D.))*//$*).-
'.*1$'' /*4*0>*0- 0)'$& '4/*0. ()4*!/# ($- /'44*0-. '!H!*-
3(+' ;1D. @Override ))*//$*)$.- +' 4*/'$)D. override & 42*->
*2 1 -;/ #)$''4;/# 4- 1$'' /*4*0>
Libraries
*./))*//$*).*( !-*('$--$ .># . '$--$ .2$''*( 2$/#E))*//$*)
+-* ..*-.F/#/&)*2#*2/*.)* . !*-))*//$*).)*.*( /#$)"
0. !0'2$/#/# (; $/# -=
I /*(+$' /$( !*-* " ) -/$*);*-
I /-0)/$( /*..$./2$/#*  3 0/$*)
)$/V!''.$)/# '// -/ "*-4;0.$)"))*//$*)./*# '+"0$ #*2/* 3 0/
4*0-/ ./.>"" -H+*+0'- + ) )4$)% /$*)!-( 2*-&!*-1H0. .
/# (/*(+$' /$( !*-* " ) -/$*)>
You!
*0- 2 '*( /*- / 4*0-*2)))*//$*).>
*./ 1 '*+ -.2$''#1 '$//' ) /**/#$.>*( 2$''- / ))*//$*).
 .$") /* 0. 4*/# -))*//$*)+-* ..*-.;.0#.0.$)"0./*(
))*//$*).2$/#"" -/*# '+"0$ $/. + ) )4$)% /$*)-0' .>)! 2
)/ -+-$.$)" 1 '*+ -.2$''- / /# $-*2)))*//$*)+-* ..*-.>#$.**&2$''
)*/" /$)/*#*2/*$(+' ( )/)))*//$*)+-* ..*-;/#*0"#2 2$''. .*(
.$(+' 0./*())*//$*).>
Applying Annotations
$-./;/#*0"#;' /D. 3+'*- #*22 )))*// *0-*/'$)* ;0.$)"))*//$*).
ANNOTATIONS
200
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
/#/.*( *4 '. - / >
Basic Syntax
*-.$(+' 0. *!))*//$*).;/# .4)/3$.) -'4$ )/$'/*/#/*!1>*-
3(+' ;# - $./# */'$) ,0$1' )//*/# )$/V 3(+' .#*2)*1 =
@RunWith(AndroidJUnit4AndroidJUnit4::classclass)
classclass ExampleInstrumentedTestExampleInstrumentedTest {
@Test
funfun useAppContext() {
valval appContext = InstrumentationRegistryInstrumentationRegistry.getTargetContext()
assertEquals("com.commonsware.jetpack.hello", appContext.packageName)
}
}
*0)' -)(*- *0/)$/V$))-*$$)/# N*0-$)"/#
./.N#+/ -*! Elements of Android JetpackA
.2$/#1;*/'$)))*//$*).- +- 73 2$/# @ )- 0. /* *-/ '.. .;
!0)/$*).;+-*+ -/$ .; />*( ))*//$*)./& +-( / -.;.0#./# @RunWith
))*//$*)H2 2$'' 3($) /#$.(*- '*. '4 '/ -$)/#$.#+/ ->
);.2$/#1;2#$/ .+ * .)*/(// ->)))*//$*)* .)*/#1 /*
++ -*). +-/ '$) !-*(2#/$/))*// .># *1  3(+' *0'
- 2-$// ).=
@RunWith(AndroidJUnit4AndroidJUnit4::classclass) classclass ExampleInstrumentedTestExampleInstrumentedTest {
@Test funfun useAppContext() {
valval appContext = InstrumentationRegistryInstrumentationRegistry.getTargetContext()
assertEquals("com.commonsware.jetpack.hello", appContext.packageName)
}
}
Specialized Scenarios
))*//$)"'.. .;!0)/$*).;)+-*+ -/$ .$.!$-'4./-$"#/!*-2-=%0./+0//#
))*//$*)$(( $/ '4 !*- /#  ' ( )//#/$/$.))*//$)">
*2 1 -;/# - - ! 2/#$)"./#/) ))*// /#/(4)*/ ,0$/ .
ANNOTATIONS
201
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*1$*0.>
Constructors
*./*/'$)'.. .#1 .$)"' *)./-0/*-;2-$// )2$/#/# *)./-0/*-
+-( / -'$./$(( $/ '4!/ -/# '..)( =
classclass FooFoo(varvar count: IntInt) {
funfun something() {
count += 1
println("something() was called $count times")
}
}
.2 .2&$) /# #+/ -*)'.. .;/#$.$..#*-/#)- +' ( )/!*-/#
!*-('++-*#=
classclass FooFoo constructorconstructor(varvar count: IntInt) {
funfun something() {
count += 1
println("something() was called $count times")
}
}
!4*0) /*))*// *)./-0/*-;4*02$'') /*0. /# !*-('++-*#;.*
4*0)+' /# ))*//$*)$(( $/ '4 !*- /# constructor & 42*-=
classclass FooFoo @MyAnnotation constructorconstructor(varvar count: IntInt) {
funfun something() {
count += 1
println("something() was called $count times")
}
}
Lambda Expressions
/$.+*..$' /*))*// '( 3+- ..$*);4+0//$)"/# ))*//$*)
$(( $/ '4 !*- /# *+ )$)"- =
valval lambdaLambdaLambda = @MyAnnotation { doSomethingHere() }
) -/# *1 -.;'( 3+- ..$*)E 3+).F$)/*)$)./) *!'..2$/#
.$)"' invoke() !0)/$*);2$/#/# *4*!/# '( 3+- ..$*)!*-($)"/# *4
ANNOTATIONS
202
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*!/#/ invoke() !0)/$*)>))*//$)"/# '( 3+- ..$*);$) 6 /;))*// .
/#/ invoke() !0)/$*)>
Annotation Parameters
*( ))*//$*)./& +-( / -.;&$)/*'..*)./-0/*-># .4)/3$..$($'-
/*'..*)./-0/*-.2$/#.*( ($)*-$6 - ) .>*2 1 -;(*./*!/# /$( ;/# 4
2$'''**&&$)/*#*2))*//$*).++ -$)1>*;!*- 3(+' ;/# .$(+' ./!*-(
*!0.$)"/# @Deprecated ))*//$*)%0./+.. .$) String /#/$./#  3+')/$*)
!*-/#  +- /$*)=
@Deprecated("do not use this, because reasons")
funfun thisSeemsPerfectlyFine() {
// do something
}
Named Parameters
*0*)*/#1 /*0. )( +-( / -.2$/#*/'$)))*//$*).>#$.-0).
*0)/ -/*1;2# - /# - - . )-$*.2# - 4*0#1 /*)( /# +-( / -.
0. $)))*//$*).>
*2 1 -;$!4*02)//*0. )( +-( / -.;4*0- 2 '*( /**.*=
@Deprecated(message = "do not use this, because reasons")
funfun thisSeemsPerfectlyFine() {
// do something
}
Arrays
*( /$( .)))*//$*)2$''/& )--4*!1'0 .!*-+-( / -;-/# -/#)
.$)"' 1'0 >*-/#/;4*0#1 /2**+/$*).=
I . /# arrayOf() "'*'!0)/$*)=
@Index(arrayOf("otherId", "yetAnotherId"))
I . --4'$/ -'.4)/3J.,0- @-& /)*//$*)K=
@Index(["otherId", "yetAnotherId"])
ANNOTATIONS
203
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
--4'$/ -'.4)/32. /**/'$)$)1 -.$*)S>T;.*.*( *' -(/ -$'.($"#/
)*/( )/$*)$/.)*+/$*)>
Nested Annotations
/$.+*..$' /#/*) *!/# +-( / -./*)))*//$*)$.)*/# -))*//$*)>)
/#/. ;/# ) ./ ))*//$*)* .)*/" //# @ +- 73=
@Deprecated("do not use this, because reasons", ReplaceWithReplaceWith("thisIsMuchBetter()"))
funfun thisSeemsPerfectlyFine() {
// do something
}
#$.$.$6 - )//#)1D.++-*#;2#$#- /$)./# @ +- 73*)) ./ 
))*//$*).>
Class References
*( /$( .;+-( / -/*)))*//$*)$.'../#/$./* 0. 4/#
))*//$*)+-* ..*->
*- 3(+' ;$))-*$ 1 '*+( )/;**($.'$--4!*-(++$)"1'.. ./*
- '/$*)'/. /' .;.+ $7''4!*-)-*$D.$/ /. >**(#.
RoomDatabase '../#/) ./* ))*// 2$/# @Database ))*//$*)>#/
))*//$*);$)/0-);/& .)--4*!'.. ./#/- +- . )//# E )/$/$ .FJ/# 1
(* ''.. ./#/2$''#1 *-- .+*)$)"/' .$)/# /. K>
*-. .'$& /#$.;2 0. .$(+' ::class )*//$*)/*$ )/$!4/# '..=
@Database(entities = [SimpleEntitySimpleEntity::classclass], version = 1)
abstractabstract classclass SimpleDatabaseSimpleDatabase : RoomDatabaseRoomDatabase() {
// TODO add in the rest of the stuff that Room needs
}
 - ; ::class $ )/$7 ./# */'$)'..J$> >; SimpleEntity::class $./# */'$)
SimpleEntity '..K>
)- '$/4;/#$.))*//$*)) . Java '..;0//#/$.#)' 4/# */'$)
*(+$' -2# )$/+-* .. .))*//$*).> 2$'' 3+'*- /# $6 - )  /2 )
*/'$)'..*% /.)1'..*% /. '/ -$)/# **&>
ANNOTATIONS
204
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Defining Annotations
.$*)''4;4*0(4#1 /# ) /*- / 4*0-*2)0./*())*//$*).>
)*/'$);))*//$*).- .$(+'4'.. . 7) 0.$)"/# annotation & 42*-=
annotationannotation classclass MyAnnotationMyAnnotation
@MyAnnotation
classclass FooFoo
*0- 2 '*( /**)./-0/*-+-( / -./*4*0-))*//$*)=
annotationannotation classclass MyAnnotationMyAnnotation(valval isThisGood: BooleanBoolean)
@MyAnnotation(isThisGood = truetrue)
classclass FooFoo
*2 1 -;/# - - '$($/.*)/# //4+ .>*-*/'$)G 1 '*+( )/;*/'$)
))*//$*)." /*)1 -/ $)/*1))*//$*).;)/#*. #1 '$($/.*)/# $-/
/4+ .>*0)0. =
I Int ) Long
I Float ) Double
I Boolean
I String
I )4 )0(/#/4*0 7)
I )4*/# -))*//$*)
I '.. .J//4+ $. KClassK
ANNOTATIONS
205
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Kotlin Essentials
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Nullability
# -   NullPointerException #. )/# ) *!()41 1 '*+ -.>
)1/0''4#)' . null 1'0 .- '/$1 '42 '';*(+- /*')"0" .'$& G
]];2# - )0''+*$)/ -.)#1 (*- ("$)" 6 /.>
#  .$") -.*!*/'$) ' / /*/-4/**.*( /#$)"*0/$/;4$)")0''$'$/4
/*/4+ .;.*/#/ 1 '*+ -.) (*- 2- *!2# - null $.J)$.)*/K''*2 ;
/#-*0"#*(+$' @/$( /4+ # &$)">/$.)*/+ -! /.*'0/$*);0/$/* ."*
'*)"24/*2-. '$($)/$)" NullPointerException )- '/ 0".>
Introducing Nullable Types
*!-$)/#$.**&;2 #1 0. /4+ .'$& Int; Boolean; String;) Axolotl>
# . /4+ .- )*-('*/'$)/4+ .?)/# 4*)*/''*2 null>
*;$!4*0/-4/#$.=
valval notGonnaHappen : StringString = nullnull
?4*0" /=
error: null can not be a value of a non-null type String
val notGonnaHappen : String = null
^
#$.$.)*/( - '4'$($//$*)*!..$")( )/.>)4+' 2# - /# /4+ * .)*/
''*2 null;4*0))*/0.  null 1'0 >*;/#$./**!$'.2$/#*(+$'  --*-=
209
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun gotNoTimeForNull(parameter: StringString) {
println(parameter)
}
funfun main() {
gotNoTimeForNull(nullnull)
}
*.4/#/ null $.1'$1'0 ;4*0++ ) ? /*/# /4+ ;"$1$)"4*0/4+ .'$& Int?;
Boolean?; String?;) Axolotl?>*;/#$.2*-&.=
valval notGonnaHappen : StringString? = nullnull
funfun gotNoTimeForNull(parameter: StringString?) {
println(parameter)
}
funfun main() {
gotNoTimeForNull(notGonnaHappen)
}
J!-*( N0''' 4+ .N$)/# '..**&K
*0($"#/ 2*--$ *0//#/ println(parameter) .// ( )/>! parameter )
null;($"#//#/-.#2$/# NullPointerExceptionB# ).2 -$.)*;/#/
.// ( )/$..! ;4$ '$)"=
null
*2 1 -;$)" ) -';/# *) -)$.1'$=4*0) /*(& .0- /#/2#/4*0+..
)0''' /4+ /*).0++*-/ null>*2 1 -;/#/$./# $"1)/" *!/#$. $)"
+-/*!*/'$)D./4+ .4./ (=(*./*!/# /$( ;$! null $.)*/''*2 ;)0''' /4+
$.)*/''*2 ;)4*0!$'2$/#.*( !*-(*!*(+$'/$*) --*->!*/'$)*
''*2.4*0/*+.. null H. println() * .H/# )$/$.0+/*/#/* /*#)'
null "- !0''4>
Expressions with Nullable Types
!*0-. ;/# - 2$'' /$( .2# )2 #1 /**+ 2$/# null> 2)//**
 -/$)/#$)".$!/# 1'0 $.)*/ null;)*.*( /#$)" '. $!/# 1'0 $. null>
# - - 1-$ /4*!/#$)".$)*/'$)/#/)# '+4*02*-&2$/# null 1'0 .>
NULLABILITY
210
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Intrinsically Safe Stuff
# - - ()4/#$)".$)*/'$);.0++'$ 4/# ')"0" *-$/../)-'$--4*!
'.. .)!0)/$*).;/#/.0++*-/ null>#$.$)'0 ./#$)"./#/4*0($"#/)*/
3+ />
*- 3(+' ; CharSequence? J!-*(2#$# String? $)# -$/.K#.) isNullOrEmpty()
!0)/$*)>.*) ($"#/ 3+ /;$/- /0-). true $!/# 1'0 $. null *-#.)*
#-/ -.J >">;$/$./#  (+/4./-$)"; ""K>*;/#$.2*-&.=
funfun foo(message: StringString?) {
println(message.isNullOrEmpty())
}
funfun main() {
foo("Hello, world!")
foo("")
foo(nullnull)
}
J!-*( N0)/$*).*)0''' 4+ .N$)/# '..**&K
" /=
false
true
true
*/ 1 -4/#$)"$.''*2 H()4!0)/$*).-  7) *)/# *- )*)@)0'''
/4+ ;-/# -/#)*)$/.)0''' *0)/ -+-/>
*;!*- 3(+' ; Int #. dec() !0)/$*)/#/- /0-)./# 1'0  - ( )/ 4
*) >#/$. 7) *) Int;)*/ Int?;).*/#$.* * .)*/2*-&=
valval one = 1
println(one.dec())
valval maybeZero : IntInt? = nullnull
println(maybeZero.dec())
0/;.$) /#$.$.*(+$' @/$(  --*-;/# - $.)*#-($)/-4$)"A)/# *(+$'
--*-#$)/./*0+' *!.*'0/$*)./*/# +-*' (=
NULLABILITY
211
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
error: only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable
receiver of type Int?
println(maybeZero.dec())
^
Safe Calls
*-(''4;/*''!0)/$*)*))*% /;4*00. */)*//$*)J.K>
) *+/$*)!*-''$)"!0)/$*)*)1-$' ;+-( / -;*-+-*+ -/4/#/$.*!
)0''' /4+ $./*0. /# .! @''*+ -/*-J?.K># );*) *!/2*/#$)".2$''
#++ )=
I !/# 1'0 $. null;4*0-!0)/$*)''$.$")*- ;) null $./# - .0'/
I !/# 1'0 $.)*/ null;4*0-!0)/$*)''$.( .)*-('
*;/#$.2*-&.=
valval one : IntInt? = 1
println(one?.dec())
valval maybeZero : IntInt? = nullnull
println(maybeZero?.dec())
J!-*( N! ''.N$)/# '..**&K
#$.+-$)/.=
0
null
*;/# 7-./ dec() ''#++ )..)*-('; 0. one $.)*/ null;)/# . *)
dec() ''$.- +' 4 null;. maybeZero $. null>
# /4+ *!/# - .0'/*! ?. !0)/$*)''$.)0'''  $/$*)*!2#/ 1 -/4+ /#
!0)/$*)'')*-(''4- /0-).>''$)" dec() *)) Int - /0-).) Int;0/''$)"
dec() *)) Int 1$ ?. - /0-).) Int?>
#$.( )./#/4*0)#$) ?. ''.=
NULLABILITY
212
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
valval three : IntInt? = 3
println(three?.dec()?.dec()?.dec())
valval maybeZero : IntInt? = nullnull
println(maybeZero?.dec()?.dec()?.dec())
J!-*( N! ''#$).N$)/# '..**&K
#$.+-$)/./# .( - .0'/=
0
null
The Elvis Operator
?. !0)/$*)''. #1 $6 - )/'4 + )$)"*)2# /# -/# E-  $1 -FJ/# *% /
*)2#$#4*0- ''$)"/# !0)/$*)K$. null *-)*/>
$($'-'4;/# E'1$.*+ -/*-FH ?: H- /0-).$6 - )/1'0 . + )$)"*)
2# /# -/# ' !/@#).$ *!/# *+ -/$*)$. null *-)*/=
I !/# ' !/@#).$ $.)*/ null;/# '1$.*+ -/*-- /0-)./# ' !/@#)
1'0
I !/# ' !/@#).$ $. null;/# '1$.*+ -/*-- /0-)./# -$"#/@#)1'0
valval one : IntInt? = 1
println(one ?: "um, this should not be printed")
valval maybeZero : IntInt? = nullnull
println(maybeZero ?: "Elvis has not left the building")
J!-*( N# '1$.+ -/*-N$)/# '..**&K
#$.+-$)/.=
1
Elvis has not left the building
)/# 7-./ println() ''; one $.)*/ null;.*2 +-$)//# 1'0 *! one>)/#
. *) println() ''; maybeZero $. null;.*2 +-$)//# ./-$)"/#/++ -./*/#
-$"#/*!/# *+ -/*->
NULLABILITY
213
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*/ /#/ return ) throw - 1'$/#$)"./*#1 *)/# -$"#/@#).$ *!)
'1$.*+ -/*-=
funfun printOrNot(value: IntInt?) {
valval nonNullable = value ?: returnreturn
println(nonNullable)
}
funfun main() {
valval one : IntInt? = 1
printOrNot(one)
valval maybeZero : IntInt? = nullnull
printOrNot(maybeZero)
}
J!-*( N'1$. /0-).AN$)/# '..**&K
 - ;2 *)'4" /*) '$) *!*0/+0/=
1
)/# . 2# - 2 '' printOrNot() 2$/# null 1'0 ;/# 7-./'$) *!/#
!0)/$*)2$'' return;4+..$)"/# println() ''>
0/)*2;,0$&=
Why Is This Called the “Elvis Operator”?
!4*0/0-)4*0-# /*/# .$ )'**&//# ?: *+ -/*-;$/'**&.$/'$& +$-
*! 4 .;*1 2#$#$. +*(+*0-#$-./4' >'1$.- .' 4!(*0.'42*- #$.#$-
$)+*(+*0-$)#$. -'4- ->
Who is Elvis Presley?
.&4*0-+- )/.>
My Parents Are Asking: Who is Elvis Presley?
'1$.- .' 4 2.)( -$)-*&@)@-*''$*)*!/# S[WRD./#-*0"#S[YRD.>
NULLABILITY
214
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Isn’t This FAQ a Bit of a Sidetrack for a Programming Book?
*)D/ -0 '>
Null Checks and Smart Contracts
*/'$)D.*(+$' -)# '+1*$.*( *!/# +$)*! '$)"2$/#)0''' /4+ .>!
/# *(+$' -&)*2./#/.*( /#$)"))*/ null;0 /*.*( +-$*-# &;$/
- '3 ./# -0' .- ,0$-$)".! ''.J >">; ?.K>
*- 3(+' ;' /D.. $!*0- Int? $. 1 );*;*- null=
funfun evenOrOddOrNull(value: IntInt?) {
ifif (value != nullnull) {
ifif (value.rem(2) == 0) {
println("Even!")
}
elseelse {
println("Odd!")
}
}
elseelse {
println("Null!")
}
}
funfun main() {
valval one : IntInt? = 1
evenOrOddOrNull(one)
valval maybeZero : IntInt? = nullnull
evenOrOddOrNull(maybeZero)
}
J!-*( N(-/*)/-/.N$)/# '..**&K
rem() $.!0)/$*)*) Int /#//& .)*/# - Int;$1$ ./# /2*;)- /0-)./#
- ($) ->*; rem(2) 2$''- /0-) 0 !*- 1 ))0( -.) 1 !*-*)0( -.>
#$.+-$)/.2#/4*0($"#/ 3+ /=
Odd!
Null!
NULLABILITY
215
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*2 1 -;$/(4)*/ *1$*0.2#4/#$. 1 )*(+$' .> value $.) Int?>/2*0'
. (/#/ value.rem(2) .#*0'!$'2$/#/# .( .*-/*!*(+$' - --*-/#/2 .2
-'$ -$)/# #+/ -JE*)'4.! JB>K*-)*)@)0''.. -/ JAA>K''.- ''*2 *)
)0''' -  $1 -*!/4+ )/BFK>
*2 1 -;2 *)'4/-4''$)" rem() *) value $).$ *! null # &Jif (value !=
null)K>*/'$)D.*(+$' -&)*2./#/$).$ *!/#/'*&; value ))*/ null;
 0. 2 %0./# & /*. $!$/2. null *-)*/>*/'$)D.*(+$' -/# - !*-
- '3 ./# .! @''- ,0$- ( )/;)2 )%0./0. . !*-*0- rem() '';-/# -
/#) ?. J)#1 /* '2$/#+*/ )/$''4 null - .0'/K>
#$.*)'42*-&.2# )/# *(+$' -$. sure /#//# 1'0 ))*/ null;/#*0"#>
#/#++ ).,0$/ $/;0//# - 2$'' . .2# )/# *(+$' -))*/
 -/$)) !0'/./*/# .! @''- ,0$- ( )/=
funfun numberizer(): IntInt? = 1
funfun evenOrOddOrNull() {
ifif (numberizer() != nullnull) {
ifif (numberizer().rem(2) == 0) {
println("Even!")
}
elseelse {
println("Odd!")
}
}
elseelse {
println("Null!")
}
}
evenOrOddOrNull()
 - ;2 " /*0-)0( -!-*( numberizer() !0)/$*)>#/!0)/$*)$. '- /*
- /0-)) Int?; 1 )/#*0"#$/.$(+' ( )//$*)#++ )./*'24.- /0-) 1>*/'$)D.
*(+$' -* .)*/// (+//* 3($) /# $(+' ( )//$*)*! numberizer()>/'**&.
//# - /0-)/4+ ;. ./#/$/$. Int?;)..0( ./#/$/*0' null>*-
$(+*-/)/'4;%0./ 0. if (numberizer() != null) .0  )2 2 )/$)/*
/# if '*&;/# *(+$' -#.)*"0-)/ /#/.*( !0/0- numberizer() ''2$''
- /0-))*)@null 1'0 ;.*$/"$1 .0.*(+$'  --*-!*- numberizer().rem(2);
 ()$)".! ''/# - >
NULLABILITY
216
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Dammit, It’s Not Null
*( /$( .;/#*0"#;4*0&)*2 // -/#)/# *(+$' ->*0- sure /#/ -/$)
1'0 $.)*/ null; 1 )/#*0"#/# *(+$' -/#$)&.*/# -2$. >
*-/#/;/# - $. !!>*0)0. /#$.*+ -/*-//#  )*!1'0 *- 3+- ..$*);
)$/.. -/./*/# *(+$' -/#//# 1'0 *- 3+- ..$*)2$'')*/ null; 1 )$!
!-*(/4+ ./)+*$)/$/*0' >
# +- 1$*0. 3(+' $.. 2# - 4*0&)*2/#/ numberizer() *0') 1 -
- /0-) null;0//# *(+$' -* .)*/># -$"#/.*'0/$*)$)/#$.. 2*0' /*
#1 numberizer() - /0-) Int $)./ *! Int?>*2 1 -;/# - 2$'' . .2# -
4*0*)*/#1 *)/-*'*1 -/# - /0-)/4+ *!/# !0)/$*);.*4*0))*/#)"
$/>
)E73F/# +- 1$*0. 3(+' )*/# -24;1$ !!=
funfun numberizer(): IntInt? = 1
funfun evenOrOddOrNull() {
ifif (numberizer() != nullnull) {
ifif (numberizer()!!.rem(2) == 0) {
println("Even!")
}
elseelse {
println("Odd!")
}
}
elseelse {
println("Null!")
}
}
evenOrOddOrNull()
 - ;2 ++ ) !! /*/# . *) numberizer() '';/*!*- /# *(+$' -/*/- /$/
.- /0-)$)") Int $)./ *!.- /0-)$)") Int?> ) ;/#$..)$++ /*(+$' .
)-0).>
'.*.2/#$.$)/# +-  $)"#+/ -;2# - 2 2 - !*-$)"
NullPointerException=
NULLABILITY
217
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
varvar thisIsReallyNull: StringString? = nullnull
println(thisIsReallyNull)
println(thisIsReallyNull!!.length)
J!-*( N3 +/$*).N$)/# '..**&K
 - ;2 - .. -/$)"/#/ thisIsReallyNull $.)*/ null 1$ !!>#/- .0'/.$)
NullPointerException;.$) thisIsReallyNull $.- ''4 null>
!! $.$/*!E* .( ''F>. $/.+-$)"'4;.$!4*0-  1 -2-*)"$)4*0-
.. -/$*);4*02$''-.#2$/# NullPointerException *-/#  ,0$1' )/;.2 $
$)/# *1 .)$++ />
Nullable Types and Generics
4+ ./#/4*00. $)" ) -$.) )0''' =
valval listOfNullables : MutableListMutableList<StringString?> = mutableListOf()
listOfNullables.add("this is not null")
listOfNullables.add(nullnull)
println(listOfNullables)
J!-*( N0'''  ) -$4+ .N$)/# '..**&K
 - ; listOfNullables $. MutableList /#/)#*' String? $)./ *!%0./
String>*;2 )+0/*/# String *% /.) null $)/*/# '$./>
*;2 )- / '$.//#/)*)/$))0''>#/$!2 2)//# '$./$/. '!/*
+*..$'4 nullB)/#/. ;/# ? "* .!/ -/# " ) -$/4+ =
valval nullableList : MutableListMutableList<StringString>? = mutableListOf()
nullableList?.add("this is not null")
nullableList?.add("this is also not null")
println(nullableList)
J!-*( N0'''  ) -$4+ .;*)/$)0 N$)/# '..**&K
 - ; nullableList ) $/# -  MutableList *- null>!$/$. MutableList;
/#*0"#;/#/'$./)*)'4#*' String *% /.>
NULLABILITY
218
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
!2 2)/;2 )*($) /# /2*=
valval nullableListOfNullables : MutableListMutableList<StringString?>? = mutableListOf()
nullableListOfNullables?.add("this is not null")
nullableListOfNullables?.add(nullnull)
println(nullableListOfNullables)
*)*2)*/*)'4) nullableListOfNullables $/. '! null;0/$!$/$.)/0'
MutableList;/# '$./)#*' null 1'0 .;$)$/$*)/* String *% /.>
)$!4*07)''*!/#$.*)!0.$)"?2 '';+ -#+./#/$.)*/# -- .*)2#4 ? 2.
#*. )./# .4(*'/*0. !*-)0''$'$/4>
Nullable Types and Casts
# )$/*( ./*./.))0''' /4+ .;4*0)0. /# )0''' !*-(*! as Jas?K=
valval nullableList : MutableListMutableList<StringString>? = mutableListOf()
nullableList?.add("this is not null")
nullableList?.add("this is also not null")
valval simplerList = nullableList asas? ListList<StringString>
println(simplerList)
J!-*( N0''' ./.N$)/# '..**&K
)/#$.. ; simplerList 2$).0+ $)" List? *! String *% /.>!/# *% /
 $)"./$. null;4*02$)0+2$/# null 1'0 >
as? $.'.*0. !0'!*-. .2# - /# *% /($"#/)*/ *!/#  .$- /4+ =
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval kermit : AnimalAnimal = FrogFrog()
valval notAnAxolotl = kermit asas? AxolotlAxolotl
NULLABILITY
219
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
println(notAnAxolotl)
}
J!-*( N0''' ./.;*)/$)0 N$)/# '..**&K
as? - .0'/.$) null 1'0 $! $/# -=
I # *% / $)"./$/. '!$. null;*-
I # *% / $)"./$.)*/*!/# /4+ /#/4*0- /-4$)"/*./$//*
)/#$.. ; kermit $.)*/) Axolotl>.- .0'/; notAnAxolotl 2$).0+ $)"
null;2$/#/# 1-$'  $)"/4+ . Axolotl?>
Objective: Minimize Nulls
 .+$/ ''*!/# . /#$)".; null ./$''2$).0+ $)"+$)>* "- ;/#/$.
$)/ )/$*)'>4(&$)"2*-&$)"2$/# null ))*4$)";/# */'$)')"0"  .$") -.
- /-4$)"/*./ -4*0/*($)$($5 4*0-0. *! null 1'0 .> /;//# .( /$( ;
4*0)./$''2*-&2$/#'$--$ .)!-( 2*-&.!*-2#$# null $.+*..$$'$/4J >">;
1'$--$ .0. 4*/'$)G+-*% /.K>
)+-/$0'-;*) *((*)0. *! null /#/4*0)/-4/* '$($)/ $.0.$)"$/.
.*( E("$F*- !0'/1'0 >)'..$+-*"-(($)";2 *!/ )0. null /*.$")$!4
E2 *)*/#1 /#$.1'0 4 /F*-E/# - $.)*1'0 !*-/#$.+-*+ -/4F>)*/'$);2
)0. /#$)".'$& . ' '.. ./*1*$/# null>*;$)./ *!=
data classdata class CustomerCustomer(valval name: StringString)
data classdata class OrderOrder(valval customer: CustomerCustomer? = nullnull)
valval order = OrderOrder()
?4*0*0'#1 =
sealedsealed classclass OrderingEntityOrderingEntity {
data classdata class CustomerCustomer(valval name: StringString) : OrderingEntityOrderingEntity()
objectobject UnassignedUnassigned : OrderingEntityOrderingEntity()
}
data classdata class OrderOrder(valval customer: OrderingEntityOrderingEntity = OrderingEntityOrderingEntity.UnassignedUnassigned)
valval order = OrderOrder()
NULLABILITY
220
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#$.$..*( 2#/(*- 1 -*. >*2 1 -;$/(*- *-- /'4(* './# .$/0/$*)J2
*)*/#1 0./*( -4 /;.*$/$.0)..$") K>);2 1*$)4+*..$'
NullPointerException *-#1$)"/* '2$/#'*/.*! ?. *- ?: )/# '$& /*(&
0. *!/# customer +-*+ -/4>
NULLABILITY
221
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Scope Functions
.2 .2 $)) -'$ -#+/ -;E.*+ F- ! -./*2# - 1-$' .) - ! - ) >
*- 3(+' ;!0)/$*)- / ..*+ <1-$' . '- $)/#/!0)/$*)- )*/
 ..$' *0/.$ *!/#/!0)/$*)>
$($'-'4;1-$' . '- $).$ *!'( 3+- ..$*)- )*/ ..$' *0/.$
*!/#/'( 3+- ..$*)>*2 1 -;.0#'( 3+- ..$*).) /* ++'$ /*
.*( /#$)";.0#. $)"0. 4 forEach()>
*/'$)#..$3"'*'!0)/$*)./#/''*24*0/*- / '( 3+- ..$*)/*
 '- '$($/ .*+ $).$ *!!0)/$*)>
#/(4.*0)*/# .*/ -$J2#42*0'*) - BK)- + /$/$*0.J*2 - ''4
)  six *!/# . B)D/2 0. %0./*) BK>
)/-0/#;/# . E.*+ !0)/$*).F" /0. '*/$)*((*)*/'$) 1 '*+( )/;.*$/
$.$(+*-/)//*0) -./)2#//# 4- )#*2/# 4" /0. >
let()let()
) /# +-  $)"#+/ -;2 .2'*/.*!24.*! '$)"2$/#1'0 ./#/($"#/
+*/ )/$''4 null; 0. /# 4- - ! - ) 41-$' .*-+-*+ -/$ .2$/#
)0''' /4+ .>
)*/# -+*+0'-24*! '$)"2$/#)0''' /4+ .$./*0. /# 7-./*!/# .*+
!0)/$*).= let()=
valval one = 1
one.let { println(it.dec()) }
223
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
valval maybeZero : IntInt? = nullnull
maybeZero?.let { println(it.dec()) }
J!-*( N' /JKN$)/# '..**&K
# let() !0)/$*)) '' *))4*% />//& .'( 3+- ..$*);)$/
+.. .2#/ 1 - let() 2.'' *)$)/*/# '( 3+- ..$*).+-( / ->
)- ! -/*/#/+-( / -. it;.$)/# *1  3(+' .;*- 3+'$$/'4)( $/
J >">; one.let { value -> println(value.dec()) }K>
#$.(4. (.$''4>);$)/# . *!/# 7-./ let();2# - 2 - ''$)"$/*)
one;$/+-*'4$..$''4>
# . *). )-$*;/#*0"#;$.0. !0'> 0. 2 - 0.$)".! ''J?.K; let() $.
*)'4'' 2# ) maybeZero $.)*/ null>/# -2$. ;/# let() ''$..&$++ >.
- .0'/; it $.) Int;)*/) Int?; 0. /# */'$)*(+$' -&)*2./#/4
 7)$/$*)/# +-( / -+.. /*/# '( 3+- ..$*)))*/ null H
*/# -2$. ;2 2*0'#1 )*/''  let() $)/# 7-./+' >*2#$' maybeZero #.
/* - ! - ) 0.$)".! ''.)- '/ / #)$,0 .;* inside the lambda
expression * .)*/#1 /*2*--4*0//#/>) 6 /;2 #1 E@null@ F/#
1'0 >0/;$)*0-. ; maybeZero $. null;.*/#/ let() $..&$++ ;)2 *)'4
+-$)/ 0 !*-/# 7-./ let()>
#$.$."- /!*-. .2# - 4*0#1 0)#*!2*-&/#/4*02)//*+ -!*-(*)
1'0 $!$/$.)*/ null;)4*0- #++4/*.&$+*1 -/#/2*-&2# )$/$. null>
# let() !0)/$*)- /0-).2#/ 1 -/# './.// ( )/$.$).$ /# '(
3+- ..$*);)2 )0. /#//*+*+0'/ 1-$' ;.+-( / -/*!0)/$*);
*-2#/ 1 -=
valval one = 1
println(one.let { it.dec() })
valval maybeZero : IntInt? = nullnull
println(maybeZero?.let { it.dec() })
J!-*( N' /JK /0-)'0 N$)/# '..**&K
 - ;2 *)'4*/# dec() ''$).$ *!/# '( 3+- ..$*);)2 +-$)/
2#/ 1 - let() - /0-).>#$.- .0'/.$)=
SCOPE FUNCTIONS
224
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
0
null
*;$)/# maybeZero . )-$*; ?. . ./#/ maybeZero $. null ).&$+./# let()
'';- /0-)$)" null;).*2 +-$)/ null /*/# *0/+0/>
).0((-4; let()=
I .'' *).*( *% /
I & .2#/ 1 -4*0''$/*))+.. ./#/$)/*/# '( 3+- ..$*).
+-( / -
I  /0-).2#/ 1 -/# './.// ( )/*!/# '( 3+- ..$*) 1'0/ ./*
apply()apply()
*./+-*"-(( -.#1 -0)$)/*. .2# - /# 4#/*- + /- ! - ) /*
1-$' *-.*( /#$)"'*/=
importimport java.util.Calendarjava.util.Calendar
valval sometime = CalendarCalendar.getInstance()
sometime.setset(CalendarCalendar.YEARYEAR, 1980)
sometime.setset(CalendarCalendar.MONTHMONTH, 1)
sometime.setset(CalendarCalendar.DAY_OF_MONTHDAY_OF_MONTH, 22)
sometime.setset(CalendarCalendar.HOUR_OF_DAYHOUR_OF_DAY, 17)
sometime.setset(CalendarCalendar.MINUTEMINUTE, 0)
sometime.setset(CalendarCalendar.SECONDSECOND, 0)
sometime.setset(CalendarCalendar.MILLISECONDMILLISECOND, 0)
println(sometime)
*0#1 .*( *% /)4*0) /*''2#*' 0)#*!( /#*.*)$//*
*)7"0- $/;.0#.. //$)"/# $)$1$0'7 '.*! Calendar *% /;.2 -
*$)"# - >
*0'.$(+'$!4/#$.2$/# let()=
importimport java.util.Calendarjava.util.Calendar
valval sometime = CalendarCalendar.getInstance()
sometime.let {
it.setset(CalendarCalendar.YEARYEAR, 1980)
SCOPE FUNCTIONS
225
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
it.setset(CalendarCalendar.MONTHMONTH, 1)
it.setset(CalendarCalendar.DAY_OF_MONTHDAY_OF_MONTH, 22)
it.setset(CalendarCalendar.HOUR_OF_DAYHOUR_OF_DAY, 17)
it.setset(CalendarCalendar.MINUTEMINUTE, 0)
it.setset(CalendarCalendar.SECONDSECOND, 0)
it.setset(CalendarCalendar.MILLISECONDMILLISECOND, 0)
}
println(sometime)
*2 1 -; apply() 2*-&. 1 ) // -!*-/#$.. )-$*>*0''$/*).*( *% /;
.0++'4$)"'( 3+- ..$*)># *% / *( . this $).$ *!/# '(
3+- ..$*);''*2$)"4*0/*%0./''!0)/$*).*)$/2$/#*0/#1$)"/*)( $/>
'.;*/'$)G* .)*/#1 java.util.Calendar;.*2 ))*/0. /#/$)/#
'..**&>*;' /D.'**&/)'/ -)/$1 *)./-0/=
data classdata class IntPropertyBagIntPropertyBag(privateprivate valval pieces: MutableMapMutableMap<StringString, IntInt> = mutableMapOf()) {
funfun set(key: StringString, value: IntInt) {
pieces[key] = value
}
}
funfun main() {
valval sometime = IntPropertyBagIntPropertyBag()
sometime.apply {
setset("ID", 330258648)
setset("YEAR", 1979)
setset("HOW_MANY_ROADS_MUST_A_MAN_WALK_DOWN", 42)
}
println(sometime)
}
J!-*( N++'4JKN$)/# '..**&K
 - ;+- / )/#/ IntPropertyBag $..*( /-0'42!0''../#/4*0- " //$)"
!-*(.*( /#$-@+-/4'$--4>*0#1 )*#*$ 0//*0. $/; .+$/ /# !//#/
$/D.$.!$-'4'$($/ =4*0)"$1 $/*) )( $)/ " -//$( 1$ set()> - ;
-/# -/#).+ ''*0//# sometime. +-/*!''$)" set() /#- /$( ./*. //#-
$)/ " -+-*+ -/$ .;2 0. apply() /*(& this  /# ";.*2 )'' set()
2$/#*0/- ! - )$)"/# "$- /'4>
'.*; apply() - /0-).2#/ 1 -*% //#/4*0'' $/*);.*2 )!0-/# -
.$(+'$!4/#$..=
SCOPE FUNCTIONS
226
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
data classdata class IntPropertyBagIntPropertyBag(privateprivate valval pieces: MutableMapMutableMap<StringString, IntInt> = mutableMapOf()) {
funfun set(key: StringString, value: IntInt) {
pieces[key] = value
}
}
funfun main() {
valval ultimateStuff = IntPropertyBagIntPropertyBag().apply {
setset("ID", 330258648)
setset("YEAR", 1979)
setset("HOW_MANY_ROADS_MUST_A_MAN_WALK_DOWN", 42)
}
println(ultimateStuff)
}
J!-*( N++'4JK /0-)'0 N$)/# '..**&K
#$.$./# .( ./# +- 1$*0. 3(+' ; 3 +//#/2 - %0./#$)$)" apply()
-$"#/*)/*/# ''/*/# IntPropertyBag *)./-0/*->
!*0-. ;2 *0''.*+0//# println() $)# - =
valval ultimateStuff = IntPropertyBagIntPropertyBag().apply {
setset("ID", 330258648)
setset("YEAR", 1979)
setset("HOW_MANY_ROADS_MUST_A_MAN_WALK_DOWN", 42)
println(thisthis)
}
-;2 *0' '$($)/ ultimateStuff )/$- '4=
println(IntPropertyBagIntPropertyBag().apply {
setset("ID", 330258648)
setset("YEAR", 1979)
setset("HOW_MANY_ROADS_MUST_A_MAN_WALK_DOWN", 42)
})
).0((-4; apply()=
I .'' *).*( *% /
I & .2#/ 1 -4*0''$/*))(& .$/ /# E0-- )/F*% /H this H
$)/# .*+ *!/# '( 3+- ..$*)
I  /0-).2#/ 1 -*% /4*0'' $/*)
SCOPE FUNCTIONS
227
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
run()run()
*( *!/# */# -.*+ !0)/$*)..$(+'4- ($3! /0- .*! apply() ) let() $/
$6 - )/'4> run();!*- 3(+' ;2*-&.$/'$& apply();$)/#/2#/ 1 -4*0''
run() *) *( . this $)/# .*+ *!/# '( 3+- ..$*)/#/4*0.0++'4>
*2 1 -; run() '.*2*-&.$/'$& let();$)/#//# - .0'/*! run $.2#/ 1 -/#
'./.// ( )/*!/# '( 3+- ..$*)- /0-).>
run $.0. !0'2# )4*02)//*''0)#*!!0)/$*).*).*( *% /;0//# )
/#  )- .0'/$.)*//# *% /$/. '!;0/.*( /#$)" '. ?.0#./# - .0'/*!*)
*!/#*. !0)/$*).=
data classdata class IntPropertyBagIntPropertyBag(privateprivate valval pieces: MutableMapMutableMap<StringString, IntInt> = mutableMapOf()) {
funfun set(key: StringString, value: IntInt) {
pieces[key] = value
}
}
funfun main() {
valval ultimateStuff = IntPropertyBagIntPropertyBag().run {
setset("ID", 330258648)
setset("YEAR", 1979)
setset("HOW_MANY_ROADS_MUST_A_MAN_WALK_DOWN", 42)
toString()
}
println(ultimateStuff)
}
J!-*( N-0)JKN$)/# '..**&K
 - ;2 '' run() *) IntPropertyBag $)./) ;%0./.2 $2$/# apply() $)/#
+- 1$*0.. /$*)>*2 1 -; ultimateStuff $.)*//# IntPropertyBag *% /;0/
$)./ /# - .0'/*! toString() '' *)/# IntPropertyBag *% />)/# *)/ 3/
*! println();/# - $.)*+-/$'$6 - ) ;. println() 2$'''' toString() /*
" ) -/ 2#//*+-$)/>*2 1 -;$)*/# -.$/0/$*).;/# 1'0 /#/4*0" ) -/
!-*(/# run '( 3+- ..$*)($"#/ (*- $./$)/'4$6 - )/>
).0((-4; run()=
I .'' *).*( *% /
I & .2#/ 1 -4*0''$/*))(& .$/ /# E0-- )/F*% /H this H
$)/# .*+ *!/# '( 3+- ..$*)
I  /0-).2#/ 1 -/# './.// ( )/*!/# '( 3+- ..$*) 1'0/ ./*
SCOPE FUNCTIONS
228
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
with()with()
with() 2*-&.) -'4$ )/$''4/* run()>*/#0. )*% //* *( /# this
$).$ /# '( 3+- ..$*);)*/#- /0-)/# - .0'/.*!/# './.// ( )/$)
/#/'( 3+- ..$*)>
# $6 - ) '$ .$)2# - /# *% /*( .!-*(=
I $/# run();4*0'' run() *)/# *% /
I $/# with();4*0+../# *% /.+-( / -=
data classdata class IntPropertyBagIntPropertyBag(privateprivate valval pieces: MutableMapMutableMap<StringString, IntInt> = mutableMapOf()) {
funfun set(key: StringString, value: IntInt) {
pieces[key] = value
}
}
funfun main() {
valval ultimateStuff = with(IntPropertyBagIntPropertyBag()) {
setset("ID", 330258648)
setset("YEAR", 1979)
setset("HOW_MANY_ROADS_MUST_A_MAN_WALK_DOWN", 42)
toString()
}
println(ultimateStuff)
}
J!-*( N2$/#JKN$)/# '..**&K
*2 1 -;/#$.$)/-*0 ..0/' #)" = run() $.(*- 9 3$' 2$/#)0''' /4+ .
/#)$. with()>$/# run();4*0)0. ?. /**)$/$*)''4'' run() $!/# *% /
J/# E-  $1 -FK$.)*/ null=
varvar thing: StringString? = "foo"
valval nonNullResult = thing?.run { thisthis.isNotBlank() }
println(nonNullResult)
thing = nullnull
valval nullResult = thing?.run { thisthis.isNotBlank() }
println(nullResult)
J!-*( N-0)JK)! ''.N$)/# '..**&K
SCOPE FUNCTIONS
229
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
#$.+-$)/.=
true
null
)*/#. .;2 - 0.$)" thing?.run { this.isNotBlank() };2# -
isNotBlank() - /0-). true $!/# ./-$)"#.*) *-(*- )*)@2#$/ .+ #-/ -.>
)/# . 2# - thing $. "foo"; run() $.'' ; 0. thing $.)*/ null># -
thing $. null;/#*0"#;/# ?. .&$+./# run() '';).* nullResult '.*2$).0+
. null> *)*/)  ?. *)/# isNotBlank() ''; 0. /# */'$)*(+$' -
&)*2./#//# ?.run() ))*/2$)0+2$/# null 1'0 >
$/# with();/#*0"#;/# .! @''*+ -/*-J?.K$.)*/)*+/$*)H2 +../#
+*..$'4@)0''1'0 ./# +-( / ->0-'( 3+- ..$*)2*0') /* '
/* '2$/#/# +*..$'4@)0''1'0 >
).0((-4; with()=
I . passed .*( *% /
I & .2#/ 1 -4*0''$/*))(& .$/ /# E0-- )/F*% /H this H
$)/# .*+ *!/# '( 3+- ..$*)
I  /0-).2#/ 1 -/# './.// ( )/*!/# '( 3+- ..$*) 1'0/ ./*
also()also()
$& run(); also() $.$/*!E(.#@0+F*! let() ) apply() ! /0- .>$& let();
/# *% /4*0'' also() *)$.( 1$'' .+-( / -/*4*0-'(
3+- ..$*);.*4*0)- ! -/*$/. it *-.*( 0./*()( >$& apply();/#*0"#;
also() - /0-)./# *% /4*0''$/*)>
data classdata class IntPropertyBagIntPropertyBag(privateprivate valval pieces: MutableMapMutableMap<StringString, IntInt> = mutableMapOf()) {
funfun set(key: StringString, value: IntInt) {
pieces[key] = value
}
}
funfun main() {
valval ultimateStuff = IntPropertyBagIntPropertyBag().also {
it.setset("ID", 330258648)
it.setset("YEAR", 1979)
it.setset("HOW_MANY_ROADS_MUST_A_MAN_WALK_DOWN", 42)
}
println(ultimateStuff)
}
SCOPE FUNCTIONS
230
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
J!-*( N'.*JKN$)/# '..**&K
).0((-4; also()=
I .'' *).*( *% /
I & .2#/ 1 -4*0''$/*))+.. ./#/$)/*/# '( 3+- ..$*).
+-( / -
I  /0-).2#/ 1 -*% /4*0'' $/*)
use()use()
use() '**&.)2*-&.'*/'$& let();2$/#/2*(%*-$6 - ) .>
$-./;4*0))*/'' use() *))4*% />/#./* .*( /#$)"/#/$(+' ( )/.
/# 1 Closeable $)/ -! >#$.$)/ -! ; $)1W;- +- . )/..*(
- .*0- /#/) '*. ;.0#../- (*-.*& />
 *);!/ - 3 0/$)"4*0-'*&*!* ; use() ''. close() *)/#/ Closeable>/
2$''*/#$. 1 )$!4*0-'*&/#-*2.) 3 +/$*);4( ).*!0.$)" tryGfinally
./-0/0- > ) ;40.$)" use();4*0&)*2/#/2#/ 1 -/#$.- .*0- $.2$''
+-*+ -'4'*. >
).0((-4; use()=
I .'' *).*( *% //#/$(+' ( )/./# Closeable $)/ -!
I & .2#/ 1 -4*0''$/*))+.. ./#/$)/*/# '( 3+- ..$*).
+-( / -
I  /0-).2#/ 1 -/# './.// ( )/*!/# '( 3+- ..$*) 1'0/ ./*
I ''. close() *)/# Closeable *% /
Summary
*- +;# - - /# $6 - ) .(*)"/# .$3.*+ !0)/$*).*1 - # - =
SCOPE FUNCTIONS
231
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Function How You Call It
How You Reference the
Object
What It
Returns
let() ''*))*% / it '*&- .0'/
apply() ''*))*% / this /# *% /
run() ''*))*% / this '*&- .0'/
with()
..*% /.
+-( / -
this '*&- .0'/
also() ''*))*% / it /# *% /
use() ''*) Closeable it '*&- .0'/
SCOPE FUNCTIONS
232
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Functional Programming
% /@*-$ )/ JK+-*"-(($)"#. )0. $)E. -$*0.2*-&F!*-  .>
)4*!/# (*./+*+0'-')"0" .H1;]];Q;% /$1 @;2$!/;04H
- . *)*% /@*-$ )/ +-*"-(($)">/# -')"0" .;.0#.1-$+/;'.*
' 1 -" *% /.;0/0.$)".*( 2#/$6 - )/++-*# .JE+-*/*/4+ @. F1.>
E'..@. F*% /@*-$ )/ +-*"-(($)"K>
/$. .4/*!*-" //#//# - - */# -24./*2-$/ +-*"-(($)"')"0" >
) '/ -)/$1 $.!0)/$*)'+-*"-(($)"># - - +' )/4*!')"0" .;.0#.
'*%0- ;$.+;)# ( ;/#/#1 /# $-!*0)/$*).$)!0)/$*)'+-*"-(($)">
# . ')"0" .- )*/) -'4.+*+0'-./# $-*0)/ -+-/.>*2 1 -;
' ( )/.*!!0)/$*)'+-*"-(($)"-  $)"$)/-*0 $)/**/# -')"0" .;
' )$)"!0)/$*)'+-*"-(($)"2$/#>*/'$)$.*) .0#')"0" >
)/#$.#+/ -;2 2$'' 3+'*- 2#/!0)/$*)'+-*"-(($)"( ).)#*24*0
2*-&2$/#$/$)*/'$)>
Your App Might Not Be Functional
*-*-$)-4+ *+' 2#*.+ &)"'$.#;E!0)/$*)'F( ).E$/2*-&.. 3+ / F>
..0#;.*( *4/ ''$)"4*0/#/4*0-++$.)*/!0)/$*)'($"#/ $/*!)
$).0'/>
*2 1 -;*(+0/ -+-*"-(( -.($"#/2 ''( )E!0)/$*)'F$)/# *)/ 3/*!
!0)/$*)'+-*"-(($)">)/#/. ;(*./++.- )*/!0)/$*)';.!0)/$*)'
+-*"-(($)"#. )- '/$1 '4)$# / #)$,0 ;/#*0"#*) /#/$."$)$)"
($)./- ((*( )/0(>
233
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
./# )( .0"" ./.;!0)/$*)'+-*"-(($)"D.+-$(-40)$/*!* $.!0)/$*);
%0./.*% /@*-$ )/ +-*"-(($)"D.+-$(-40)$/*!* $.)*% />.2$/#
*/# -!*-(.*!+-*"-(($)";!0)/$*)$)!0)/$*)'+-*"-(($)"/& ..*(
$)+0/;+ -!*-(..*( 2*-&;)- /0-)..*( *0/+0/>
# $ '/4+ *!!0)/$*)$)!0)/$*)'+-*"-(($)"$.E+0- F!0)/$*)>+0-
!0)/$*)#.)*.$  6 /.;.0#.#)"$)"/# *)/ )/.*!/. >!4*0''
+0- !0)/$*)*) ($''$*)/$( .2$/#/# .( $)+0/;4*0.#*0'" //# .(
*0/+0/ #*!/#*. ($''$*)/$( .>
0)/$*)'+-*"-(($)"'.* (+#.$5 .!0)/$*)*(+*.$/$*);0.$)"E#$"# -@
*- -F!0)/$*).>*- 3(+' ;4*0($"#/#1  sort() !0)/$*)/#/).*-/.*(
*'' /$*)*!/>*2 1 -; sort() $/. '!) . .0++'$ )*/*)'42$/#/# /
0/2$/#.*( */# -!0)/$*)/#/&)*2.#*2/**(+- /2*/ ' ( )/./*
 / -($) /# $-*- -> sort();/# - !*- ;($"#//& /2*+-( / -.=
I # //* .*-/ 
I *( $ )/$7 -*-- ! - ) /*/# !0)/$*)/*0. !*-*- -*(+-$.*)
)/#$.. ; sort() $.*).$ - /* E#$"# -@*- -F!0)/$*);.$//& .)*/# -
!0)/$*).+-( / -/*# '+ 7) /# *1 -''2*-&/* + -!*-( >
# ))*)@!0)/$*)'+-*"-(($)"')"0" ../-//*$)/-*0 !0)/$*)'
/ #)$,0 .;$/$.*!/ )2$/#) 4 /*2-../- (+-* ..$)"> - ;E./- (F*0'
 =
I *'' /$*)*!*% /.
I . -$ .*! 1 )/.;.0#.0. -$)+0/ 1 )/.*-/. #)"  1 )/.
I '$/ -'E./- (F;0.0''4- ! --$)"/*.*( .*0- *!4/ .*!/;.0#.
7' *-.*& /
#/$.#*2*/'$)++-*# .!0)/$*)'+-*"-(($)"=(&$)"$/ .4/*0.
!0)/$*)'/ #)$,0 .$) -/$).$/0/$*).;2$/#*0/)  ..-$'4(&$)"!0)/$*)'
+-*"-(($)"/# *) @)@*)'4++-*#*6 - 4/# ')"0" >
Where Immutability Comes Into Play
((0/$'$/4$.*!/ )& 4! /0- *!!0)/$*)'+-*"-(($)"')"0" .>
((0/$'$/4# '+./* ).0- /#/$(+0- !0)/$*).*)*/- &+0- !0)/$*).4
#)"$)"/E #$)/# &F*!/# +0- !0)/$*).>
FUNCTIONAL PROGRAMMING
234
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*- 3(+' ;/# - $. first() !0)/$*)1$'' !*-0.*) List /#/- /0-)./#
7-./ ' ( )/$)/# '$.//#/(/# ..*( -0' +-*1$ 1$'( 3+- ..$*)=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval leetEvent = events.first { it.id == 1337 }
println(leetEvent)
}
J!-*( N7-./JKN$)/# '..**&K
2$'' 3+'*- first() $)"- / - /$' '/ -$)/#$.#+/ ->/$.) 3(+' *!
!0)/$*)'+-*"-(($)"$)*/'$);2# - first() $.+0- !0)/$*)/#//.*)$/.
$)+0/J ListK)- /0-)$/.*0/+0/J/# 7-./ ' ( )/!*-2#$#/# '(
3+- ..$*)- /0-). trueK>
*2 1 -;2#$' first() $.+0- H 0. /# 0/#*-.*!*/'$) ).0- /#/$/2.
H$/$.+*..$' /#/ the lambda expression ($"#/ $(+0- )#1 .$  6 /.>)
+-/$0'-;2 ($"#/" /./-)" - .0'/.$!=
I # '( 3+- ..$*)*0'(*$!4/# Event /#/$/2.*(+-$)"
I # '( 3+- ..$*)*0'(*$!4/# events '$./2#$' 2 2 - /-4$)"/*
7).*( /#$)"$).$ *!$/
.- .0'/;2 /-4/*0. $((0/' *% /.2# - +*..$' ;/*- 0 /# '$& '$#**
/#/2 2$''(& /#*. .*-/.*!($./& .>
#$.'.*# '+.'*/2$/#+-'' '+-*"-(($)";2# - 2 - + -!*-($)"*+ -/$*).
$)+-'' '-*..(0'/$+' /#- .> 1 '*+ -.2#*#1 #/* '2$/#/#- 
.! /4 !*- &)*2!0''2 ''#*2)./4$/) /*/-&*2)E/$($)"0".F2# -
#)" .!-*(*) /#- 6 /)*/# -/#- D.2*-&>0- !0)/$*).)
$((0/' *% /.(& $/ .$ -/* ).0- /#- .! /4;.*) /#- ))*/
- $'46 /)*/# -/#- D.2*-&>
Examples of Functional Kotlin
'*/*!#$"# -@*- -!0)/$*).- 1$'' !*-'$./.)--4.;/#*0"#.*( -
1$'' *)*/# -/4+ .;$)'0$)"*) /#/2 2$''. '/ -$)/#$.#+/ -> " /
'$./.''/# /$( ;!-*(/. ,0 -$ ./* . -1$ ''.>*/'$)D.#$"# -@*- -
!0)/$*).(& $/ .4/*()$+0'/ /#*. '$./.>
FUNCTIONAL PROGRAMMING
235
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*-/#*. *!4*02$/#31 3+ -$ ) ;'*/*!/# . 2$'''**&!($'$->31
// (+/./*$(+' ( )/!0)/$*)'*)/*+*!1!*-+-* ..$)"./- (.*!
/>31*+ -/*-.- &$)/*#$"# -@*- -!0)/$*).;+-/$0'-'42# )4*00.
/# (*)1Z]))0. '( 3+- ..$*).>*2 1 -;2#$' 31$.!*0. 
*)+-* ..$)"./- (..4)#-*)*0.'4;*/'$)* .)*/- ../#- $)"$- /'4
2$/#$/.#$"# -@*- -!0)/$*).>
*1 - .*( *!/# . &$) /# #+/ -*)*'' /$*).>
first()first() / firstOrNull()firstOrNull()
*/'$)*6 -.- ''4.$(+' first() !0)/$*)/#/- /0-)./# 7-./ ' ( )/!-*(/#
'$./=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
println(events.first())
}
J!-*( N7-./JK).(N$)/# '..**&K
*2 1 -;/# (*- +*2 -!0'!*-(*! first() /& .'( 3+- ..$*))- /0-).
/# 7-./ ' ( )//#/(/# .;.2 .2*1 =
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval leetEvent = events.first { it.id == 1337 }
println(leetEvent)
}
J!-*( N7-./JKN$)/# '..**&K
first() 2$''/#-*2 NoSuchElementException;/#*0"#;$!/# - $.)*(/#$)"
' ( )/>*0)' ..4*0- $)+*.$/$*)/*/#/#/ 3 +/$*)*-&)*2!*- -/$)
/#//# - 2$'''24. (/#; first() $.))*4$)">
firstOrNull() 2*-&./# .( . first();0/$/- /0-). null $!/# - $.)*(/#=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
FUNCTIONAL PROGRAMMING
236
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
valval noEvent = events.firstOrNull { it.id == 2343 }
println(noEvent)
}
J!-*( N7-./-0''JKN$)/# '..**&K
$/#/# '1$.*+ -/*-;/#$.'.*(& .$/ .4!*-4*0/*$(+' ( )/7-./@*-@
 !0'/+// -)=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval oneEvent = events.firstOrNull { it.id == 2343 } ?: EventEvent(2343)
println(oneEvent)
}
J!-*( N7-./-0''JK)/# '1$.+ -/*-N$)/# '..**&K
filter()filter()
# - . first() ) firstOrNull() *)'4"$1 4*0*)  ' ( )/J/(*./K; filter()
"$1 .4*0)*/# -'$./*---42$/#/# .0. /(/#$)".*( 0.$) ..-0' J$> >;
2# - '( 3+- ..$*)- /0-). trueK;.2 .2&$)/# #+/ -*)
*'' /$*).=
valval things = listOf("foo", "bar", "goo")
things.filter { it[1]=='o' }.forEach { println(it) }
J!-*( N7'/ -JKN$)/# '..**&K
$"# -@*- -!0)/$*).-  .$") /* #$) /*" /# -;.*4*0)0.
filter() ) firstOrNull() /*7)/# 7-./ 1 )/2$/#) "/$1  1 )=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval evenNegativeEvent = events.filter { it.id % 2 == 0 }.firstOrNull { it.id < 0 }
println(evenNegativeEvent)
}
J!-*( N#$)$)"$"# -@- -0)/$*).N$)/# '..**&K
FUNCTIONAL PROGRAMMING
237
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
 - ;*0- filter() '( 3+- ..$*)- /0-)./#*. 2#*. .-  1 )J/#
(*0'**!T ,0'.RK>0- first() '( 3+- ..$*)/# )7)./# 7-./*!/#*.
2# - /# $.) "/$1 >
)/#$.. ; firstOrNull() *0'%0./$(+' ( )/*/#-0' .;/#*0"#;0.$)"/#
'*"$'@)*+ -/*-J&&K=
data classdata class EventEvent(valval id: IntInt)
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42),
EventEvent(-6))
valval evenNegativeEvent = events.firstOrNull { it.id % 2 == 0 && it.id < 0 }
println(evenNegativeEvent)
map()map()
*( /$( .;2 #1 /$)*) !*-(/#/2 ) /**)1 -/$)/*)*/# -!*-(>*-
/#/;/# - $./# map() *+ -/*-;.2 .2$)/# #+/ -*)*'' /$*).=
valval things = listOf("foo", "bar", "goo")
things
.map { it.toUpperCase() }
.forEach { println(it) }
J!-*( N(+JKN$)/# '..**&K
map() /& .'( 3+- ..$*);+.. . # ' ( )/$)/# ./- (/*/#/'(
3+- ..$*);)*'' /./# *% /.- /0-) 4/# '( 3+- ..$*)>
any()any()
*( /$( .;2 2)//*-0))'"*-$/#(*)*'' /$*))" /.*( .$)"' - .0'/
&>*- 3(+' ; any() - /0-). true $!)4*!/#  ' ( )/.$)/# *'' /$*)- .0'/
$) true 1'0  $)"- /0-) !-*(.0++'$ '( 3+- ..$*)=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
println(events.any { it.id < 0 })
println(events.any { it.id > 100000 })
}
FUNCTIONAL PROGRAMMING
238
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
J!-*( N)4JKN$)/# '..**&K
 - ;/# 7-./ any() - /0-). true; 0. *) *!/#  ' ( )/.* .#1 ) "/$1
1'0 ># . *) any() - /0-).!'. ; 0. )*) *!/#  ' ( )/.#.)
*1 SRRRRR>
*/ /#/ any() *)'4/ ./. ' ( )/.0)/$'$/" /. true - .0'/J*-- # ./#  );
2#$# 1 -*( .7-./K=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
println(events.any {
println(it)
it.id > 10
})
}
J!-*( N)4JK)#*-/$-0$/.N$)/# '..**&K
 - ;2 $)/-*0 .$ @ 6 /$)/**0-'( 3+- ..$*)=+-$)/$)"/# 0-- )/
$/ ( $)" 3($) 4 any()>0))$)"/#$."$1 .0.=
Event(id=1)
Event(id=5)
Event(id=1337)
true
) any() " /. true 1'0 ;$/./*+.)- /0-). true;.*2 *)*/2$)0+2$/#
+-$)/ *0/+0/*!/# */# - 1 )/.$)/# '$./>
fold()fold()
# . .*-/.*!#$"# -@*- -!0)/$*).*)*/#1 /* ..$(+' ./# *) ..#*2)
*1 ># 4) .*(+' 3.$.)  ..-4;)/#/$)/0-)($"#/- ,0$- (*-
*(+' 3'( 3+- ..$*).>
*- 3(+' ;.*( /$( .4*02)//*+ -!*-('0'/$*)*) #*!/#  ' ( )/.
$)*'' /$*))" ) -/ .$)"' - .0'/!-*(/#*. >*-/#/;2 #1 fold()>
fold() /& ./2*+-( / -.=
I )$)$/$'1'0
I '( 3+- ..$*)/#//& .)E0(0'/*-F1'0 )) ' ( )/!-*(
FUNCTIONAL PROGRAMMING
239
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
/# *'' /$*))- /0-).) 21'0 . *)/#*. /2*
# 7-./$)1*/$*)*!/# '( 3+- ..$*)" /./# $)$/$'1'0 ./#
0(0'/*-1'0 ># . *)$)1*/$*)" /./# - .0'/*!/# 7-./$)1*/$*).
/# 0(0'/*-1'0 >).**)/#-*0"#/# *'' /$*)=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval joined = events.fold("") { str, event ->
ifif (str.length==0) event.toString() elseelse "$str,$event"
}
println(joined)
}
J!-*( N!*'JKN$)/# '..**&K
#/$( *0-'( 3+- ..$*)" /.$)1*& ;$/" /./# *)/ )/ - .0'/..*!-
+'0./# ) 3/ 1 )/$)/# '$./>0-'( 3+- ..$*). .$!/# *)/ )/
- .0'/.-  (+/4;)$!.*%0./- /0-). String - +- . )//$*)*!/# 7-./ 1 )/>
/# -2$. ;$/.*(()/# String - +- . )//$*)*!/# 0-- )/ 1 )/>.
- .0'/;/#/0(0'/  String & +."-*2$)"=
Invocation strstr Value eventevent Value
S "" Event(1)
T "Event(1)" Event(5)
U "Event(1),Event(5)" Event(1337)
V "Event(1),Event(5),Event(1337)" Event(24601)
W "Event(1),Event(5),Event(1337),Event(24601)" Event(42)
X "Event(1),Event(5),Event(1337),Event(24601),Event(42)" Event(-6)
# ) fold() - # ./#  )*!/# *'' /$*);$/- /0-).2#/ 1 -/# './'(
3+- ..$*)- /0-) >
!*0-. ;*/'$)*6 -. joinToString() !0)/$*)/#/2*0'#)' /#$.!*-0.
(*- .$(+'4=
FUNCTIONAL PROGRAMMING
240
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval joined = events.joinToString(",")
println(joined)
}
J!-*( N%*$)*/-$)"JKN$)/# '..**&K
Function Types
# . ($"#/'**&'$& ("$;*-/' ./'$& /# .*-/*!/#$)"/#/)*)'4
+-*1$ .+-/*!*- ')"0" $(+' ( )//$*)>)- '$/4;/# . #$"# -@*- -
!0)/$*).- )*//#/*(+'$/ ;0//# 4/& 1)/" *!& 4! /0- *!
*/'$)=!0)/$*)/4+ .>!0)/$*)/4+ ''*2.4*0/*+..2#/'**&.'$& !0)/$*)
.+-( / -/*)*/# -!0)/$*)>
*- 3(+' ;/# hasMatch() !0)/$*)$)/#$.* .)$++ /$.#$"# -@*- -!0)/$*)
/#/($($./# !0)/$*)'$/4*! any()=
funfun <TT> hasMatch(list: ListList<TT>, predicate: (TT) -> BooleanBoolean): BooleanBoolean {
list.forEach { item ->
ifif (predicate.invoke(item)) returnreturn truetrue
}
returnreturn falsefalse
}
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
println(hasMatch(events) { it.id < 0 })
println(hasMatch(events) { it.id > 100000 })
}
J!-*( N0)/$*)4+ .N$)/# '..**&K
Functions and Generics
*-) any()@./4' !0)/$*);2 ) /2*/#$)".=
I # *'' /$*)*!$/ (./*# &;)
I # '( 3+- ..$*)/#/2 0. /*# & #$/ (0)/$'2 " /(/#
)/# .0-! ;/# *'' /$*). (. .4> *0'%0./#1 +-( / -/#/$.
FUNCTIONAL PROGRAMMING
241
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
List *-) Array *-.*( /#$)">*2 1 -;$ ''4;2 2*0' '- /#$.!0)/$*)/*
 ' /*#)' '$./*!)4/4+ *!/>0-!0)/$*)* .)*/- 2# /# -/#$.
$.'$./*! String *% /.;'$./*! Event *% /.;*-'$./*! Axolotl *% /.>''*0-
!0)/$*)) ./**$./*+.. ' ( )/.!-*(/# '$.//*/# '( 3+- ..$*)>*
'*)"./# '$./)/# '(- $)"- ( )/*)/4+ .;2#//# /4+ $.* .)*/
(// ->
hasMatch() 0. ." ) -$.)*///# '..' 1 '0///# !0)/$*)' 1 '># <T>
!/ -/# fun & 42*-.4.E# 4;/#$.!0)/$*))*+ -/ *)*% /.*!.*( /4+ TF>
)/# )0. T .+' #*' -!*-/# E- 'F/4+  '. 2# - $)/# !0)/$*)
 '-/$*)>)+-/$0'-;*0- list +-( / -$. List<T>> #1 )**)./-$)/.
*)2#/ T ) ;.*2# )2 0. hasMatch() List<Event> 2*-&.7) !*-
List<T>>
Declaring a Function Type Parameter
# . *)+-( / -/* hasMatch() .#*0' *0-'( 3+- ..$*)>-*(/#
./)+*$)/*!*0-!0)/$*);/#*0"#;/# . *)+-( / -$.!0)/$*)/4+ > foo:
String  '- .+-( / -)(  foo *!/4+ String;2# - /# +-( / -)(
+-   ./# *'*)>$($'-'4; predicate: (T) -> Boolean  '- .+-( / -
)(  predicate>*2 1 -;/# /4+ $.!0)/$*)/4+ >
!0)/$*)/4+ #./#-  ' ( )/.=
I # /4+ .*!/# +-( / -./*/# !0)/$*); )'*. $)+- )/# . .
I # --*2*+ -/*-J->K
I # /4+ - /0-) 4/# !0)/$*)
)*0-. ;2 - .4$)"/#/2 2)/;./# . *)+-( / -;!0)/$*)/#/
/& .$) T )- /0-). Boolean> hasMatch() * .)*/- 2#//# !0)/$*)$.*-
* .;.*'*)".$/ +/. T )- /0-). Boolean>
Passing a Function Type
$1 )/#/2 #1 #$"# -@*- -!0)/$*)'$& hasMatch();2 ) /*.0++'4
!0)/$*)./$.!4$)"/# !0)/$*)+-( / -># - - ! 224.$)2#$#2 )*
/#/>
FUNCTIONAL PROGRAMMING
242
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Lambda Expressions
# (*./*((*)++-*#;)/# *) 0. $)/# *1  3(+' ;$./*+..
'( 3+- ..$*)>
#)$''4;/# '( 3+- ..$*)*0"#//*++ -.)/0'!0)/$*)+-( / -
*! hasMatch()=
println(hasMatch(events, { it.id < 0 }))
println(hasMatch(events, { it.id > 100000 }))
 - ;*0-'( 3+- ..$*)$.+-/*!/# *((@ '$($/ '$./*!+-( / -.
+.. /* hasMatch()>
*2 1 -;/# */'$)*(+$' -''*2.4*0/*+0//# '( 3+- ..$*)!/ -/#
'*.$)"+- )/# .$.;$!/# !0)/$*)/4+ +-( / -$./# './+-( / -$)/#
!0)/$*)D.+-( / -'$./>#/$.2#4 hasMatch() /& ./# '$./7-./)/# )/#
!0)/$*)/4+ +-( / -;.*2 )2-$/ /#$.$)./ =
println(hasMatch(events) { it.id < 0 })
println(hasMatch(events) { it.id > 100000 })
!!0)/$*)/& .(*- /#)*) !0)/$*)/4+ +-( / -;*-$!/# !0)/$*)/4+
+-( / -$.)*/'./$)/# '$./;4*0#1 /*+../# '( 3+- ..$*).- "0'-
+-( / -;$).$ /# +- )/# . .*!/# !0)/$*)''>*- 3(+' ;$!2 #
subscribe() !0)/$*)/#//**&/2*!0)/$*)/4+ +-( / -.!*- onSuccess )
onError;/(*./2 *0'#1 /# . *)*!/#*.  *0/.$ /# +- )/# . .>
Function References
#$' '( 3+- ..$*).- +*+0'-;/# 4- )*//# *)'4*+/$*)>
# ) 3/@(*./*((*).*'0/$*)$./*0. !0)/$*)- ! - ) >#$.$.24/*
$ )/$!4) 3$./$)"!0)/$*))+..$/.+-( / ->!/# !0)/$*)D..$")/0-
(/# .2#/$.- ,0$- 4/# !0)/$*)/4+ ;/# )/# *(+$' -2$'' #++4>
# (*./*((*).4)/3!*-/#$.$./*0. *0' @*'*) !*- /# !0)/$*))( =
funfun <TT> hasMatch(list: ListList<TT>, predicate: (TT) -> BooleanBoolean): BooleanBoolean {
list.forEach { item ->
ifif (predicate.invoke(item)) returnreturn truetrue
}
FUNCTIONAL PROGRAMMING
243
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
returnreturn falsefalse
}
data classdata class EventEvent(valval id: IntInt)
funfun lessThanZero(event: EventEvent) = event.id < 0
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
println(hasMatch(events, ::lessThanZero))
}
J!-*( N0)/$*) ! - ) N$)/# '..**&K
 - ;2 #1  lessThanZero() !0)/$*)/#//& .) Event )- /0-). Boolean>
)0. /#//*./$.!4/# !0)/$*)/4+ !*- hasMatch() 40.$)"
::lessThanZero ./# . *)+-( / -/*/# hasMatch() '';- +'$)"*0-
'( 3+- ..$*)>*/'$)D./4+ $)! - ) +$'$/4( )./#/2# )2 ''
hasMatch(events, ::lessThanZero);*/'$). ./#/=
I events $. List<Event>
I lessThanZero() /& .) Event
#$../$.7 .*/#*!/# " ) -$.0. $)/# hasMatch() !0)/$*).$")/0- ;.*/#
*(+$' -$.#++4>
#$..4)/32*-&.!*-/*+@' 1 '!0)/$*).;.0#./#$.$(+' ( )//$*)*!
lessThanZero()>/'.*2*-&.!*-( ( -!0)/$*).;$!/# !0)/$*)- ! - ) $.
$).$ /# .( *% /=
data classdata class EventEvent(valval id: IntInt)
classclass ThingyThingy {
funfun <TT> hasMatch(list: ListList<TT>, predicate: (TT) -> BooleanBoolean): BooleanBoolean {
list.forEach { item ->
ifif (predicate.invoke(item)) returnreturn truetrue
}
returnreturn falsefalse
}
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
funfun lessThanZero(event: EventEvent) = event.id < 0
funfun matchify() {
println(hasMatch(events, ::lessThanZero))
}
}
FUNCTIONAL PROGRAMMING
244
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
funfun main() {
ThingyThingy().matchify()
}
J!-*( N0)/$*) ! - ) !*- ( -0)/$*).N$)/# '..**&K
 - ; 1 -4/#$)"$.+-/*! Thingy '..;).* ::lessThanZero 2$''- .*'1 /*
/# lessThanZero() ( ( -!0)/$*)*! Thingy># $(+'$/$*)$./#//# -  $1 -
H/# *% /*)2#$# lessThanZero() 2$'' '' H$. this;*-/# 0-- )/
$)./) *! Thingy 2# - 2 - /-4$)"/*0. lessThanZero()>
!4*02)//*0. !0)/$*)*).+ $7-  $1 -;+0/$/.)(  !*- /# *0' @
*'*)=
funfun <TT> hasMatch(list: ListList<TT>, predicate: (TT) -> BooleanBoolean): BooleanBoolean {
list.forEach { item ->
ifif (predicate.invoke(item)) returnreturn truetrue
}
returnreturn falsefalse
}
data classdata class EventEvent(valval id: IntInt)
classclass ThingyThingy {
funfun lessThanZero(event: EventEvent) = event.id < 0
}
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval thingy = ThingyThingy()
println(hasMatch(events, thingy::lessThanZero))
}
J!-*( N0)/$*) ! - ) .)  $1 -.N$)/# '..**&K
 - ; lessThanZero() $.!0)/$*)*) Thingy;) thingy $.)$)./) *! Thingy;
.*2 )0. thingy::lessThanZero /*- ! - )  lessThanZero() !0)/$*)$)/#
*)/ 3/*! thingy>
Anonymous Functions
# /#$-++-*#H/# )*)4(*0.!0)/$*)H* .)*/. (/* /#/2$ '4
0. ;.*2 2$'' 3+'*- $/ (0#'/ -$)/# **&>
Using a Function Type Parameter
*0#1 /2**+/$*).!*-0.$)"/# !0)/$*)/4+ +-( / -/* 3 0/ )"$1
FUNCTIONAL PROGRAMMING
245
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
4*0- .0'/!*-.*( $)+0/=''$)" invoke() *)$/*-0.$)"/# !0)/$*)/4+ .$!$/
2 - )/0'!0)/$*)>
Calling invoke()invoke()
$1 )/#/4*0#1 +-( / -/#/$.!0)/$*)/4+ ;/*''/#/!0)/$*);''
invoke() *)/# +-( / ->#/$.2#/2 - *$)"$)/# hasMatch() #$"# -@
*- -!0)/$*)=
funfun <TT> hasMatch(list: ListList<TT>, predicate: (TT) -> BooleanBoolean): BooleanBoolean {
list.forEach { item ->
ifif (predicate.invoke(item)) returnreturn truetrue
}
returnreturn falsefalse
}
invoke() 2$''/& /# +-( / -./#/-  '- !*-/# !0)/$*)/4+ >
hasMatch()  '- ./#/ predicate /& . T $)./) ;.*2 (0./.0++'4 T
$)./) /* invoke()>
$($'-'4; invoke() - /0-).2#/ 1 -/# !0)/$*)/4+ .4.$/.#*0'- /0-)>
predicate $. '- .- /0-)$)" Boolean;.* invoke() - /0-). Boolean>
Calling Directly
*0)'.*.&$+/# invoke() )/- //# !0)/$*)/4+ .)/0'!0)/$*)=
funfun <TT> hasMatch(list: ListList<TT>, predicate: (TT) -> BooleanBoolean): BooleanBoolean {
list.forEach { item ->
ifif (predicate(item)) returnreturn truetrue
}
returnreturn falsefalse
}
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
println(hasMatch(events) { it.id < 0 })
println(hasMatch(events) { it.id > 100000 })
}
J!-*( N0)/$*)4+ .)$- /)1*/$*)N$)/# '..**&K
FUNCTIONAL PROGRAMMING
246
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
 - ;2 /- / predicate .$!$/2 - predicate();''$)"$/'$& !0)/$*);+..$)"
/# +-( / -)" //$)"/# - .0'/>#  6 /$./# .( .2$/# invoke() $/. '!=
I # )'' ;/# !0)/$*)/4+ (0./ +.. /# .( +-( / -J.K./#
!0)/$*)/4+ ''.!*-
I # )'' ;/# !0)/$*)/4+ - /0-).)*% /*!/# /4+ /#//#
!0)/$*)/4+ $. '- /*- /0-)
Multi-Parameter Function Types
*/ /#/!0)/$*)/4+ .- )*/'$($/ /*.$)"' +-( / -># 4).0++*-/)4
)0( -*!+-( / -.>*- 3(+' ;/#$. collectify #$"# -@*- -!0)/$*)0. .
!0)/$*)/4+ /&$)"/2*+-( / -.=
funfun <KK, VV, TT> collectify(input: MapMap<KK, VV>, transform: (KK, VV) -> TT): ListList<TT> {
valval result = mutableListOf<TT>()
forfor ((key, value) inin input) { result.add(transform(key, value)) }
returnreturn result.toList()
}
funfun main() {
valval stuff = mapOf("foo" to "bar", "goo" to "baz")
println(stuff)
println(collectify(stuff) { key, value -> "${key.toUpperCase()}: ${value.toUpperCase()}" } )
}
J!-*( N0)/$*)4+ .)-( / -.N$)/# '..**&K
# *% /$1 *! collectify() $./**)1 -//# Map $)/* List *!*% /.;0.$)"
.*( .0++'$ !0)/$*)/4+ /*/&  #& 4G1'0 +$-!-*(/# Map )- / 
*-- .+*)$)"*% /!*-/# List>
*- # )/-4$)/# Map;2 $)1*& /# transform *)/# & 4)1'0 > /&
/# - .0'/*! transform() )++ )$//* MutableList> /# )- /0-)/#/'$./;
*)1 -/ /*)$((0/' List 1$ toList()>
*0. collectify();2 ) /*+...*( $(+' ( )//$*)*!/# !0)/$*)/4+ >
# .-$+/.#*2)*1 0. .'( 3+- ..$*)/#/- /0-)../-$)"( 0+*!
/# 0++ -. - )$/$*).*!/# & 4)1'0 >
!4*0-0)/#$..-$+/;4*0" /*/#/# *-$"$)' Map +'0./# collectify() - .0'/=
FUNCTIONAL PROGRAMMING
247
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
{foo=bar, goo=baz}
[FOO: BAR, GOO: BAZ]
)/#$.. ;/#*0"#; collectify() $.+*$)/' ..;. Map '- 4.0++*-/./#
0)!*-/0)/ '4@)(  map() !0)/$*);2#$#* ./# .( /#$)">*2 1 -; map()
0. . Entry *% /.$)./ *!/# & 4.)1'0 .!*-/# !0)/$*)/4+ =
valval stuff = mapOf("foo" to "bar", "goo" to "baz")
println(stuff)
valval mappedMap = stuff.map { entry -> "${entry.key.toUpperCase()}:
${entry.value.toUpperCase()}" }
println(mappedMap)
*;*0-'( 3+- ..$*)" /.) Entry )) ./*- /-$ 1 /# key ) value
!-*($/>
Scope Functions and Function Types
*./+' ./#/4*0. .*( /#$)"'$& '( 3+- ..$*);/# - $.!0)/$*)
2$/#!0)/$*)/4+ +-( / -/#/$)1*& ./#/'( 3+- ..$*)>
*- 3(+' ;+- 1$*0.'42  3($)  */'$)D..*+ !0)/$*).;.0#. let() )
apply()>#*. - $(+' ( )/ 0.$)"!0)/$*)/4+ +-( / -.>
*2 1 -;$!4*0'**&//# */'$)*0( )//$*)!*-/# . ;/# 42$'''**&$/
./-)" >*- 3(+' ;/#$.$. /#  '-/$*)!*- let()=
inlineinline funfun <TT, RR> TT.let(block: (TT) -> RR): RR
T ) R - " ) -$/4+ .;) block $.!0)/$*)/4+ /#/ +/. T +-( / -
)- /0-).) R>*2 1 -;/# - - /2* ' ( )/.*!/#$. '-/$*)/#/2 #1
)*/*1 - 4 />
$-./$. inline>#/$.)*+/$($5/$*))$.*+/$*)';.*2 2$''*1 -$/ (0#'/ -
$)/# **&>
# */# -+-/$. T.let> let $./# !0)/$*))( ;0/2#/$./# T.B
#$.$./# 24/#/4*0 '- )E 3/ ).$*)!0)/$*)F;2# - 2 )!0)/$*).
/* 3$./$)"/4+ .; 1 )!*-'.. .)./06/#/2 $)*/- / >3/ ).$*)
FUNCTIONAL PROGRAMMING
248
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
!0)/$*).- +*2 -!0'H).*( 2#/)" -*0.H+$'$/4$)*/'$);)2
2$'' 3+'*- /# ($) )0+*($)"#+/ ->
Type Aliases
) +-*' (2$/#!0)/$*)/4+ .$./#//# 4- $/'*)";+-/$0'-'4$!4*0#1 
0)#*!+-( / -./* '- >*( /#$)"'$& (K, V) -> T $.)*/; 0. 2
/ )/*0. .$)"' @' // -$ )/$7 -.!*-" ) -$.>0/;/#$.*0' .$'4 *(
(String, Restaurant) -> CustomerOrder *-.*( /#$)"'$& /#/;2#$#$.'*)"
//4+ >
1 )/# );$/(4)*/ ;$!4*0*)'40. /#/!0)/$*)/4+ $)*) +' >
*2 1 -;$!4*00. /#/!0)/$*)/4+ $)'*/.*!+' .;/# ' )"/#" /.(")$7 >
'0.;$/ *( .'*/(*- 2*-&/*#)" /# (;$!4*0) /*- +' Restaurant
2$/#.*( /#$)" '. J >">; FoodProviderK>
) 24/*# '+()" /#$.$.2$/#/4+ '$. .>./# )( .0"" ./.;/4+ '$. .
''*24*0/**( 0+2$/#4*0-*2)E.#*-/#)F$ )/$7 -/#/(+./*.*( */# -
/4+ >*0)0. /4+ '$. .!*-)4.*-/*!//4+ ;0//# 4- +-/$0'-'4
0. !0'2$/#!0)/$*)/4+ .>
 '-$)"/4+ '$.$.1 -4 .4=0. typealias;!*''*2 44*0- .$- '$.; =;
)/# /4+ /#//# '$.(+./*=
typealiastypealias StringMapStringMap = MapMap<StringString, StringString>
typealiastypealias StringPairStringPair = PairPair<StringString, StringString>
typealiastypealias PairmongerPairmonger = (StringString, StringString) -> StringPairStringPair
/4+ '$.) 1 )- ! - ) )*/# -/4+ '$.> - ;/# Pairmonger /4+ '$.$.
 7) $)/ -(.*! StringPair;2#$#$/. '!$./4+ '$.>
*0)/# )0. /#*. '$. .2# - 1 -4*0($"#/0. /# /0'/4+ =
typealiastypealias StringMapStringMap = MapMap<StringString, StringString>
typealiastypealias StringPairStringPair = PairPair<StringString, StringString>
typealiastypealias PairmongerPairmonger = (StringString, StringString) -> StringPairStringPair
funfun collectify(input: StringMapStringMap, transform: PairmongerPairmonger): ListList<StringPairStringPair> {
valval result = mutableListOf<StringPairStringPair>()
forfor ((key, value) inin input) { result.add(transform(key, value)) }
returnreturn result.toList()
FUNCTIONAL PROGRAMMING
249
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
}
funfun main() {
valval stuff = mapOf("foo" to "bar", "goo" to "baz")
println(stuff)
println(collectify(stuff) { key, value -> key to value } )
}
J!-*( N4+ '$. .N$)/# '..**&K
.$)"/**()4'$. .2$''(& $/$80'/!*-) 2*( -./*0) -./)4*0-* ;
./# 4- ,0$- ' 1 '*!E( )/'$)$- /$*)F/#/2$''/& /$( /*' -)>-4/*'$($/
4*0-'$. ./*2# - /# '$.$..$")$7)/'4.$(+' -/#)/# /4+  $)"'$. >)
/#  3(+' *1 ; StringMap ) StringPair 2*0')*/ 2*-/#'$.$)"$)(*./
+-*% /.;2#$' Pairmonger (4 (*- - .*)' >
Arrays, Collections,… And Sequences
map(); fold();).$($'-#$"# -@*- -!0)/$*).$)*/'$)) 0. 2$/#--4.
)()4/4+ .*!*'' /$*).;.0#. List>
# 4)'.* 0. 2$/# Sequence>)'$& List H2#$##.$/.*-$"$).$)/#
1 java.util.List /4+ H Sequence $.*/'$)@.+ $7/4+ >)/# .0-! $/)
 0. '*/'$&  List;+-/$0'-'4$)!0)/$*)'+-*"-(($)">)- '$/4; Sequence
$..0./)/$''4$6 - )/*)./-0/>
What’s a Sequence?
#)$''4; Sequence $.(*- *(+-' 2$/#1D. Iterable>*/#$)/ -! .#1
) iterator() ( /#*/#/- /0-)..*( Iterator /*$/ -/ *1 -./06>)1;''*!
/# *) @$( ).$*)'*'' /$*)'.. ./#/4*0- 0. /*;'$& ArrayList )
HashSet;#1 $(+' ( )//$*).*! Iterable>
# $6 - ) '$ .$)/# $(+'$ *)/-/># ..0(+/$*)$./#/) Iterable
- +- . )/.)/0'*'' /$*)*!./06;2# - /#/./06 3$./.$)( (*-4))
$/ -/ *1 ->.- .0'/; Iterable '.*#.( /#*.'$& forEach() /#//&
1)/" *!/#$.*)- / *'' /$*)> Sequence;4*)/-./;$. lazy;( )$)"/#/
/# - .#*0' )*..0(+/$*)/#//# /*1 -2#$#4*0- $/ -/$)"/0''4
3$./.+-$*-/* $)"#) /*/# $/ -/*->
FUNCTIONAL PROGRAMMING
250
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
Ummmm… Why Bother?
*/'$)D. 1 '*+ -. .$") $/.#$"# -@*- -!0)/$*)./#/2*-&*) Sequence /*
$/ (@/@@/$( ;2# - ./# 4)  /*(& /# $-#$"# -@*- -!0)/$*)./#/2*-&
*) Iterable /* *'' /$*)@/@@/$( >
 /D./0-)&/*) 3(+' !-*( -'$ -$)/# #+/ -=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval evenNegativeEvent = events.filter { it.id % 2 == 0 }.firstOrNull { it.id < 0 }
println(evenNegativeEvent)
}
J!-*( N#$)$)"$"# -@- -0)/$*).N$)/# '..**&K
 - ;2 #$)/2*#$"# -@*- -!0)/$*).= filter() ) firstOrNull()># . -
 $)"++'$ /* List;2#$#$(+' ( )/. Iterable>.- .0'/;2#/#++ ).$.=
I - /  List *!.$3 Event *% /.
I filter() - / . List *!/2* Event *% /.J/#*. 2$/#)/#/$. 1 )K
I firstOrNull() - /0-)..$)"' Event
 /D.#)" /#$.$/;4$)" map() *+ -/$*))#)"$)"/# *- -*!/#
1 )/..*/#//# -6 *) $.. *)=
valval events = listOf(EventEvent(1), EventEvent(-6), EventEvent(5), EventEvent(1337), EventEvent(24601),
EventEvent(42))
valval evenNegativeEvent = events
.map { it.id }
.filter { it % 2 == 0 }
.firstOrNull { it < 0 }
println(evenNegativeEvent)
*2;/# 9*2$.=
I - /  List *!.$3 Event *% /.
I map() - / . List *!.$3 Integer *% /.
I filter() - / . List *!/2* Integer *% /.J/#*. /#/-  1 )K
I firstOrNull() - /0-)..$)"' Integer
FUNCTIONAL PROGRAMMING
251
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
*-./-/ -'$./*!.$3*% /.;/#/$.)*//**>0/$("$) /#//# '$./#
X;RRR*% /.>*2;2 - ''*/$)"( (*-4!*-/# X;RRR@$/ ($)$/$''$./;/#
X;RRR@$/ ('$./!-*( map();+'0..*( .('' -'$./!-*( filter()>
$/# Sequence; #$/ ($.+-* .. /#-*0"#/#  )/$- #$)*))$/ (@4@
$/ (.$.>$/#/#/;*0-9*22*0' =
I - /  Sequence *!.$3 Event *% /.
I map() ($/./# 7-./ Integer J1K
I filter() .&$+./#/$/ (;.$/$.)*/ 1 )
I map() ($/./# . *) Integer J-6K
I filter() ($/. -6;.$) $/$. 1 )
I firstOrNull() - /0-). -6;.$) $/$.) "/$1
I ?)2 - *)
#$.$.(0#(*- ( (*-4 8$ )/;.2 - )*/- /$)"$)/ -( $/ List
*% /.>#*. "$).) ,0$/ .$")$7)/!*-'-" '$./.)*(+' 3#$).*!
#$"# -@*- -!0)/$*).>
How Do I Get a Sequence?
# - - ()424./*" /4*0-#).*) Sequence; $/# -4- /$)"*) !-*(
.-/#*-" //$)"*) !-*(.*( /#$)" '. >
sequenceOf()sequenceOf()
0./.2 #1 arrayOf(); listOf();).*!*-/#;2 #1  sequenceOf() "'*'
!0)/$*)$)*/'$)/*- /  Sequence !-*(. /*!$/ (.&)*2)/*(+$' /$( >
*;!*- 3(+' ;/# Sequence . )-$**0/'$) *1 '**&.'$& =
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = sequenceOf(EventEvent(1), EventEvent(-6), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42))
valval evenNegativeEvent = events
.map { it.id }
.filter { it % 2 == 0 }
.firstOrNull { it < 0 }
println(evenNegativeEvent)
}
J!-*( N ,0 ) .). ,0 ) !JKN$)/# '..**&K
FUNCTIONAL PROGRAMMING
252
Published under the Creative Commons
Attribution-ShareAlike 4.0 International license.
Visit https://commonsware.com/licenses to learn more!
asSequence()asSequence()
!4*0'- 4#1  List *- Array;4*0)'' asSequence() *)$//*" /
Sequence . *)/# List *- Array *)/ )/.>#$.(4 0. !0'$!.*( *
*/# -/#)4*0-.$.- /$)"/# List *- Array;.*4*0))*/./-/2$/# Sequence
4*0-. '!>
generateSequence()generateSequence()
# generateSequence() "'*'!0)/$*)$)*/'$)- / . Sequence /#/$.
2-++ -*0)!0)/$*)/4+ J >">;'( 3+- ..$*)K/#/4*0.0++'4># -
/#/!0)/$*)/4+ " /.$/./!-*($.0+/*4*0>*0-!0)/$*)2$'' '' . #
$/ ($)/# Sequence $.)  ;0)/$'.0#/$( .4*0-!0)/$*)- /0-). null /*
.$")'/#/4*0- *0/*!/>
importimport kotlin.random.Randomkotlin.random.Random
funfun percentileDice() = RandomRandom.nextInt(1,100)
funfun main() {
valval sequence = generateSequence {
percentileDice().takeIf { it < 95 }
}
println(sequence.toList())
}
J!-*( N" ) -/  ,0 ) JKN$)/# '..**&K
# percentileDice() !0)/$*)" ) -/ .-)*()0( -!-*(S/*SRR;0.$)"
Random.nextInt() !-*(/# */'$)./)-'$--4>0- generateSequence() ''
/# )0. .'( 3+- ..$*)/#/=
I ''. percentileDice() /*" /-)*()0( -
I . . takeIf() /*. $!$/$.' ../#)[WH takeIf() - /0-)./# *% /$/$.
'' 0+*)$!/# '(- /0-). true;*/# -2$. $/- /0-). null
# '( 3+- ..$*) ).2$/#/#/ takeIf() '';.*/# '( 3+- ..$*)
- /0-)./# -)*()0( -J$!$/$.' ../#)[WK*- null># - .0'/$./#/2 #1 
Sequence *!-)*()0( -*!-)*()0( -.> /# )0. toList() /**'' /
/# 1'0 .$) List;/# )+-$)//#/ List>
0))$)"/#$.($"#/"$1 4*0=
FUNCTIONAL PROGRAMMING
253
[33, 94, 19, 62, 81]
*-=
[32, 84, 39, 21, 22, 51, 21, 78, 82, 3, 53, 30, 48, 64, 89, 33, 16, 28, 16, 2, 90,
38, 67, 34, 86, 71, 13]
*-=
[]
#/'// -. $.2# - /# 1 -47-./-)*()0( -2.*1 -[W;.*/# Sequence
/ -($)/ $(( $/ '4>
*/ /#//# null@( ).@*) $(+' ( )//$*)*! generateSequence() ( )./#/
4*0))*/0. $//*" ) -/  Sequence *)/$)$)" null *% /.>
Elsewhere
# )2*-&$)"2$/#.;2# /# -!-*(/# */'$)./)-'$--4*- '. 2# - ;.
$!/# 4#1 *+/$*)./*- /0-) Sequence $)./ *! List *- Array>
FUNCTIONAL PROGRAMMING
254
Extension Functions
# - - '*/.*!!0)/$*)./#/2 )''*)+- //4(0#)4*% />*( ;'$&
toString();- /# .*-/.*!/#$)"./#/4*0($"#/ 3+ /)4*% //*.0++*-/>
/# -.;'$& ()4*!/# .*+ !0)/$*). J >">; let(); apply()K;($"#/. ($/
*/#/4*0)''*))4/#$)">
/$''*/# -.($"#/)*/ 1 ) *1$*0./#//# 4- !0)/$*).>&$) /# #+/ -
*)*'' /$*).;2 .2- /$)" Map 0.$)" mapOf()=
println(mapOf("foo" to "bar", "baz" to "goo"))
to() $./0''4!0)/$*)/#/2 - ''$)"*) "foo" ) "baz";2# - to()
- /0-). Pair ( 0+*!2#/2 - ''$)"$/*))/# +-( / -/*/#
!0)/$*)J/# 1'0 !/ - toK>
# -**/'..*!/# */'$)'..#$ --#4$. Any>'''.. . 3/ )!-*( Any>!4*0
'**&/ /# */'$)- ! - ) /* Any;4*0($"#/ 3+ //*. '*/.*!/# . !0)/$*).
'$./ >)./ ;/# - - *)'4/#- !0)/$*).*) Any=
I equals()
I hashCode()
I toString()
#*. (+/*/# $-1 ,0$1' )/.$)*/'$)G)#1 */# -$(+' ( )//$*).
$)*/'$)G)*/'$)G/$1 >
*?2# - - ''/# . */# -/#$)".;'$& let() ) apply() ) to() *($)"!-*(;
$!/# 4- )*/!0)/$*).*) AnyB
#*. -  7) . 3/ ).$*)!0)/$*).;2# - 4*0)+-*1$ )$(+' ( )//$*)
255
*!!0)/$*)!*-'../#/4*0$)*/- / >)/#$.#+/ -;2 2$'' 3+'*- 2#4
2 #1 /# . /#$)".;#*22 . //# (0+;)2# /# -/# 4- - ''4"**$ 
*-)*/>
The Case of the Utility Function
,0 ./$*)*!/ )-$. .$)*% /@*-$ )/ +-*"-(($)"=E2# - .#*0'/#$.!0)/$*)
"*BF
!/ );/# ).2 -$.*1$*0.;./# 2*-&..*$/ 2$/#/# !0)/$*)$./$ /*
.*( '../#/4*0- 2*-&$)"*)>0//$)"/#/!0)/$*)*)/#/'..$.'*"$'
(*1 /*(& >
0/.*( /$( .2 #1 !0)/$*)./#/*)*/)  ..-$'47//#/+// -)>
*- 3(+' ;.0++*. /#/4*02)//* ' /*1'$/ 0. -@ )/ -  String /*
. $!$/'**&.'$& 1'$ ($'- ..>.$//0-).*0/; lots *!- ''4./-)" /#$)".
) 1'$ ($'- .. .;./#  ($'- .../)-$.1 -4'**. )
!*-"$1$)">*2 1 -;$!4*0#0)/-*0)*)/#  ;4*02$''7)1-$*0.- "0'-
3+- ..$*)./#/) 0. /*1'$/ - .. .;2# - /#*. - "0'- 3+- ..$*).
)2*-&!*-(*./*((*)'4@. )- .../-0/0- .>
*/'$).0++*-/.- "0'- 3+- ..$*).1$ Regex '..>*0)- / *) 0.$)"
- "0'-@ 3+- ..$*)+// -));(*)"*/# -/#$)".;'' matches() *)$//*. $!
+-/$0'- String (/# ./# +// -)=
valval EMAIL_PATTERN = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.]+[a-zA-Z0-9-.]+(\s)*"
funfun main() {
valval emailRegex = RegexRegex(EMAIL_PATTERNEMAIL_PATTERN)
println(emailRegex.matches("[email protected]"))
println(emailRegex.matches("this is not an email address"))
}
J)*/ =/#$.$.)*/+-/$0'-'4"**- "0'- 3+- ..$*)!*- 1'0/$)" ($'
- .. .;0/$/$.!$-'4.#*-/)$. ,0/ !*-**&K
!2 *)'4)  /#$.'*"$$)*) .+*/$)/# ++;2 *0'%0./#1 !0)/$*)$)
/#/*) .+*//#/#)' ./#$.* >0/2 ($"#/) /*1'$/  ($'- .. .
$)! 2+' .=
EXTENSION FUNCTIONS
256
I )/# 0. -- "$./-/$*).- );.2 - 0.$)" ($'- .. ..0. -.
I )/# '*"$).- ); 0. *0-0. -.-  ($'- .. .
I )/# E!-$ )F.- )
I )/# E.#- /#$.*)/ )/F.- )
I ).**)
*;2# - * ./#$.E$./#$.1'$ ($'- ..BF* "*B
Pointless Superclasses
'..$*% /@*-$ )/ +-*"-(($)".*'0/$*)$./*)*/ /#/''!*0-$/ 0. .-
$).- ).;.*/# - .#*0' .*( . '../#/#./#$.!0)/$*)/#/ 1 -4*)
'. )$)# -$/!-*(>*- 3(+' ;$))-*$;/#/*0' =
I BaseActivity /#/ 3/ ).!-*( Activity )#./#$.!0)/$*)
I BaseFragment /#/ 3/ ).!-*( Fragment )#./#$.!0)/$*)
I BasePresenter !*-.*( * '@$ 2@- . )/ -JK!-( 2*-&/#/#.
/#$.!0)/$*)
I -2#/ 1 -
*2 1 -;$)" ) -';!*-$)"+-/$0'-$)# -$/) (* ';%0.//*+-*1$  ../*
.*( .$(+' !0)/$*);$.)*/"** .$")++-*#>
Utility Classes
# */# -/4+$'++-*#$./*#1 .*( 0/$'$/4'..2$/#/#$.!0)/$*)>#/
*0' .*( 2#/.+ $'$5 ;.0#. DataValidator /#/&)*2.#*2/*+ -!*-(
1-$*0./4+ .*!1'$/$*)>-;$/*0'  Util '../#/. -1 ..#*( !*-''
.*-/.*!242-$/.*!* /#/#1 )**/# -'$& '4#*( >
#$.2*-&.>*2 1 -;/# (*- " ) -$/# 0/$'$/4'..;/# (*- '$& '4/#/$/
 *( .E%0)&-2 -F;"-0''4*'' /$)"(*- )(*- * ;0)/$'4*0#1
/#$.(..$1 '../#/)**4'$& ./*($)/$)>
Global Functions
/)*/# -++-*#;+-/$0'-'4$)*/'$);$./*0. /*+@' 1 '!0)/$*)>*0*0'
%0./#1 ) isValidEmail() !0)/$*)9*/$)"-*0).*( 2# - /#/+ -!*-(./#$.
1'$/$*)>
#$.$.)*/(0#*!)$(+-*1 ( )/*1 -/# 0/$'$/4'..;/#*0"#>)!/;$/./-*)"'4
EXTENSION FUNCTIONS
257
' ).$/. '!/*2-.E%0)&-2 -F(* '.;2# - 4*0#1 .*( */'$).*0- 7'
2$/#-)*(*'' /$*)*!/*+@' 1 '!0)/$*)./#/)  /*'$1 .*( 2# - >
*-. ;''/*+@' 1 '!0)/$*)..#- )( .+ >0++*. /#/4*0#1 )
isValidEmail() !0)/$*);))*24*0.*( '$--42# - it #.$/.*2)
isValidEmail() /*+@' 1 '!0)/$*)>*24*02$''#1 0$' --*-;./# *(+$' -
2$''7)0+'$/ !0)/$*).2$/#/# .( )( >
Monkeying Around
)*/# -++-*#/#/.*( ')"0" .#1 0. $./*''*2 1 '*+ -./*
!0)/$*)./* existing classes;$)'0$)"/#*. /#/($"#/ .0++'$ .+-/*!.*(
./)-'$--4>#/2*0'''*24*0/*;.4;#1 ) isValidEmail() !0)/$*)*)
String $/. '!=
println("[email protected]".isValidEmail())
println("this is not an email address".isValidEmail())
)/# . *!1-$+/;/#$.$.( - '4(// -*! 3/ )$)"/# +-*/*/4+ *!/#
 .$- '..=
String.prototype.isValidEmail = functionfunction() {
// TODO implementation goes here
}
)04;4*0)*(+'$.#.$($'-/#$)";*!/ )- ! -- /*.E(*)& 4@+/#$)"F
'..=
classclass StringString
defdef isValidEmail
# TODO implementation goes here
endend
endend
)*/#. .; isValidEmail() $. /*/#/ )1$-*)( )/D. String /4+ ;.*4*0
)'' isValidEmail() $- /'4*) String *% />
*/'$).0++*-/./#$./**;1$2#/- &)*2). 3/ ).$*)!0)/$*).>)!/; lots *!
./06$)*/'$)D../)-'$--4$.$(+' ( )/ 1$ 3/ ).$*)!0)/$*).;$)./ *!
4$(+' ( )/$)"/# !0)/$*).$- /'4*)/# 6 / /4+ .>
EXTENSION FUNCTIONS
258
Declaring Extension Functions
 '-$)") 3/ ).$*)!0)/$*)'**&.)2*-&.'*/'$&  '-$)")4*/# -
!0)/$*)># $6 - ) $./#//# /4+ /#  3/ ).$*)!0)/$*)$. $)" '- 
0+*)$.$)'0 .+- 73/*/# !0)/$*))( ;. +-/ 4 .>
privateprivate valval EMAIL_REGEX = RegexRegex("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.]+[a-zA-Z0-9-.]+(\s)*")
funfun StringString.isValidEmail() = EMAIL_REGEXEMAIL_REGEX.matches(thisthis)
funfun main() {
println("[email protected]".isValidEmail())
println("this is not an email address".isValidEmail())
}
J!-*( N3/ ).$*)0)/$*).N$)/# '..**&K
 - ;.2$/#/# 1-$+/)04 3(+' .;2 - $)") isValidEmail()
!0)/$*)/**/'$)D. 3$./$)" String /4+ > )/# )'' isValidEmail() *)
)$/  ($'- .. .;2# /# -/#*. *( !-*(0. -.*-- #-@* / ./
1'0 .;.0#. "[email protected]">
The Syntax
#  3/ ).$*)!0)/$*) #1 .'*/'$& - "0'-!0)/$*)/#/4*0($"#/#1 *)
/# '..>)+-/$0'-; this $.#*24*0- ! -/*/# $)./) *!/# /4+ >*;$)
String.isValidEmail(); this $./# String *)2#$#4*0''  isValidEmail()>
# /4+ *)2#$#4*0 '- /#  3/ ).$*)!0)/$*)) )41'$*/'$)/4+ >
#$.$)'0 . )0''' /4+ .;/#*0"#/# !0)/$*)$(+' ( )//$*)(42$)0+
 $)"(*- *(+' 3.- .0'/=
privateprivate valval EMAIL_REGEX = RegexRegex("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.]+[a-zA-Z0-9-.]+(\s)*")
funfun String?.isValidEmail() = thisthis?.let { EMAIL_REGEXEMAIL_REGEX.matches(thisthis) } ?: falsefalse
funfun main() {
println("[email protected]".isValidEmail())
println("this is not an email address".isValidEmail())
println(nullnull.isValidEmail())
}
J!-*( N3/ ).$*)0)/$*).*)0''' 4+ .N$)/# '..**&K
 - ;2  '- isValidEmail() *) String? $)./ *! String;''*2$)"0./*''
isValidEmail() *) null .2 ''.*))/0' String>*2 1 -; matches() *)
Regex * .)*/ +/ null +-( / -;.*2 0. let() )/# '1$.*+ -/*-/*
EXTENSION FUNCTIONS
259
.0++'4 false . !0'/1'0 !*-/# null . >
The Location and Visibility
*0) '- ) 3/ ).$*)!0)/$*))42# - /#/4*0) '- - "0'-
!0)/$*)>*;$)/# *1  3(+' ;/#  3/ ).$*)!0)/$*)$./*+@' 1 '!0)/$*)>
*0)(&  3/ ).$*)!0)/$*). +0'$J/#  !0'/K*- private;.)  >
#$.' ./*/#- *((*)+// -).=
I 0'$/*+@' 1 ' 3/ ).$*)!0)/$*).
I -$1/ /*+@' 1 ' 3/ ).$*)!0)/$*).
I -$1/ ( ( -!0)/$*).
Public Top-Level
+0'$/*+@' 1 ' 3/ ).$*)!0)/$*)./#/!0)/$*)/*/#  .$")/ /4+ !*-
)4/#$)"0.$)"4*0-* >#$.$."**!*-0/$'$/4!0)/$*)./#/2$'' 0. 2$ '4
$)/# +-*% />
Private Top-Level
private /*+@' 1 ' 3/ ).$*)!0)/$*)$.0.' *)'42$/#$)/# .*0- 7' /#/
*)/$)./#/!0)/$*)>#$.$."**!*-0/$'$/4!0)/$*).2#*. $(+' ( )//$*)$.
!$-'4/$"#/'4/$ /*/# * $)/# .*0- 7' )2*0')*/ 0. !0' '. 2# - >
# private )/0- ( )./#/4*02$'')*/*''$ 2$/#*/# - private
$(+' ( )//$*).$)*/# -.*0- 7' .>
Class Members
*0)'.*#1 '..( ( -./#/-  3/ ).$*)!0)/$*).;'$($/$)"/# $- ..
/*$)./) .*!/#/'..=
privateprivate valval EMAIL_REGEX = RegexRegex("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.]+[a-zA-Z0-9-.]+(\s)*")
data classdata class UserUser(valval userId: StringString)
classclass UserRegistrationUserRegistration {
privateprivate funfun StringString.isValidEmail() = EMAIL_REGEXEMAIL_REGEX.matches(thisthis)
funfun isValidUser(user: UserUser) = user.userId.isValidEmail()
}
EXTENSION FUNCTIONS
260
funfun main() {
valval registrar = UserRegistrationUserRegistration()
println(registrar.isValidUser(UserUser(userId = "[email protected]")))
println(registrar.isValidUser(UserUser(userId = "this is not an email address")))
}
J!-*( N-$1/ 3/ ).$*)0)/$*).N$)/# '..**&K
 - ; isValidEmail() $.) 3/ ).$*)!0)/$*)*) String;0//#/ 3/ ).$*)
!0)/$*)$.*)'4 ..$' /*$)./) .*! UserRegistration>
#$.*0'%0./. .$'4 2-$// ).=
classclass UserRegistrationUserRegistration {
privateprivate funfun isValidEmail(value: StringString) = emailRegex.matches(value)
funfun isValidUser(user: UserUser) = isValidEmail(user.userId)
}
 -/$)'4;/#$.. *)++-*#$.(*- *)1 )/$*)'>)/#  );$/*( .*2)/*
*% / .$")>)/#$.. ;.#*0' String &)*2$!$/$.1'$ ($'- ..B-$.
/#/- ''4- .+*).$$'$/4*!.*( /#$)" '. ;.0#. UserRegistrationB
Calling Extension Functions
''$)") 3/ ).$*)!0)/$*)$.$)$./$)"0$.#' !-*(''$)"- "0'-!0)/$*)>*
/# '' -;/# 4'**&/# .( ;)/*/#  1 '*+ -;/# $-.4)/3$.$ )/$'># - $.
)*- '24/*'**&/!0)/$*)$)1*/$*)) / -($) 2# /# -$/$.''$)"
- "0'-!0)/$*)*-) 3/ ).$*)!0)/$*)?/' ./!-*(*/'$)>
$) */# -')"0" .*)*/)  ..-$'4#1 /#$..*-/*!+$'$/4;4*0(4 '
/*/ ''/# $6 - ) 2# )*/# -* ''.$)/**/'$)* ;/-4$)"/*0. )
3/ ).$*)!0)/$*)> 2$''. ) 3(+' *!/#$.$) )0+*($)"#+/ -*)1
$)/ -*+ -$'$/4>
The Limitations
3/ ).$*)!0)/$*).'$1 *0/.$ *!/# '.. ./#//# 4-  3/ )$)">)/#  );
3/ ).$*)!0)/$*).- E.4)//$.0"-F)-  ,0$1' )//*- "0'-/*+@' 1 '
!0)/$*)>*;$)/# . *! String.isValidEmail();/# isValidEmail() 3/ ).$*)
!0)/$*)*) String $.- ''4)*$6 - )//#)/*+@' 1 ' isValidEmail(value:
EXTENSION FUNCTIONS
261
String) !0)/$*)/#/*+ -/ *)/# .0++'$  String +-( / ->
.- .0'/; 3/ ).$*)!0)/$*).#1 )*.+ $' ../*/# $(+' ( )//$*)*!/#
'.. $)" 3/ ) 4/# !0)/$*)>)+-/$0'-; 3/ ).$*)!0)/$*).#1 )*
 ../* private *- protected ( ( -.>
classclass SomethingSomething {
valval thisIsPublic = truetrue
privateprivate valval thisIsNot = falsefalse
}
funfun SomethingSomething.kindaGrabby() = thisthis.thisIsPublic && thisthis.thisIsNot
#$.* !$'.2$/#*(+$'  --*-=
error: cannot access 'thisIsNot': it is private in 'Something'
fun Something.kindaGrabby() = this.thisIsPublic && this.thisIsNot
^
$) thisIsNot $. private; kindaGrabby() #.)* ../*$/>
3/ ).$*)!0)/$*).'.*#1 )*$'$/4/*) 2+-*+ -/$ ./*/# '../#//# 4
-  3/ )$)">.- .0'/; 3/ ).$*)!0)/$*).#1 )*$'$/4/*./*- ) 2/>''
/# 4)*$.()$+0'/ /# //#//# '..'- 4./*- .>
EXTENSION FUNCTIONS
262
Java Interoperability
# - $.  )/#) /#/4*0- $)/ - ./ $)0.$)"*/'$)!*-)-*$++
 1 '*+( )/;./# - $.'*/*!*/'$)(*( )/0($)/#/.+ >
*0($"#/'.* $)/ - ./ $)0.$)"*/'$)$)*/# -+' .'*)".$ 1=
I ++ 1 '*+( )/
I  .&/*+.*!/2-  1 '*+( )/J >">;*-)*K
I  ) -'/**'. 1 '*+( )/
#-*0"#*0//# **&/*/#$.+*$)/;/# - #1  )! 2)*/ .*0/*/'$)
-0))$)"*)/# 1$-/0'#$) JK>1 -4/#$)"/#/4*0#1 ' -) .*!-
2*-&.!*-*/'$)G>)/#$.#+/ -;2 !*0.*)/#$)"./#/4*0(4#1 /**/*
" /4*0-*/'$)* /*2*-&'*)".$ 1* ;2# /# -/#/$.*/'$)''$)"$)/*
1J >">;/# )-*$K*-1''$)"$)/**/'$)J >">;' "41* $)4*0-
++K>
Recap of Interoperability
.2.*1 -  &$)/# 7-./#+/ -;/# - - )0( -*!'4 -./**/'$)2$/#
- .+ //* )1$-*)( )/.>
'*/*!*/'$)$.2#/$.*).$ - )*24./* */'$)G*((*)>#$.*
2*-&.*)'' )1$-*)( )/./#/*/'$))-0)*)>
*/'$)/# )#./#- 1-$)/.J/+- . )/K!*--0))$)"*).+ $7 )1$-*)( )/.=
I */'$)G!*-*(+$'$)"*/'$)/*14/ * !*-0. *)/# 
I */'$)G!*-E/-).+$'$)"F*/'$)$)/*1-$+/!*-0. $)/# -*2. -*-
263
2$/#. -1 -@.$  )"$) .'$& * J*-$)/# '..**&AK
I */'$)G/$1 !*-*(+$'$)"*/'$)/*!*-0. $)+'/!*-(.'$& $
# $(+' ( )//$*).*!.*( /#$)".(41-4. *) )1$-*)( )/>*- 3(+' ;
/# Regex '.. !*-2*-&$)"2$/#- "0'- 3+- ..$*).#.$6 - )/-0' .!*-*/'$)G
;*/'$)G;)*/'$)G/$1 >#$' /# '..'24. 3$./.;/# - "0'-
3+- ..$*)+// -)')"0" (4$6 -;. Regex  ! -./*) )1$-*)( )/@.0++'$ 
$(+' ( )//$*)>
0/ 4*)/#/;4*0(4) /*.*( ./06/*4*0-* . /* ' /*
$)/ -*+ -/ 2$/#* .+ $7!*-) )1$-*)( )/;.0#.1* !*-*/'$)G
*-1-$+/* !*-*/'$)G>#$' */'$)$(./*(& $/- '/$1 '4+$)' ../*
-$" ')"0" .;/# - - ./$''$6 - ) ./#/) /* - .*'1 >#$.#+/ -
2$''!*0.*)/#*. $)1*'1$)"1>
Kotlin Calling Java
-/$0'-'4!*-) 2+-*% /.;(*./'$& '44*02$'' !*0. *)*/'$)* ''$)"
$)/*1* >#$' */'$)'$--$ .- "-*2$)"$))0( -;/# 4+' $)
*(+-$.*)/*/# 1./--4*!1'$--$ .>)+-/$0'-;()4(%*-!-( 2*-&.
- $(+' ( )/ $)1;.0#./# )-*$)+-$)">#$' /# 4(4" /
.*( */'$)2-++ -.;()4 1 '*+ -.2$''2$)0+2*-&$)"2$/#/# ($- /'4>
)/# 2#*' ;/#$)"./ )/*2*-&!$-'4.(**/#'4># - - 1-$ /4*!*.$*).
2# - 4*02$'') /*/#$)&*0/$)/ -*+ -$'$/4;0/!*-/# (*./+-/;E$/%0./
2*-&.F>#$.$.*) *!/# - .*).2#4**"' 2.*(!*-/' $) )*-.$)"*/'$)
H$!#1$)"*/'$)* ''$)/*/# )-*$2."*$)"/* .*( #0"
+-*' (;**"' ($"#/#1  )(*- 0/$*0.>
)/#$.. /$*);2 2$'' 3+'*- .*( /#$)"./#/4*02$'') /**).$ -.4*0
#1 4*0-*/'$)* ''*0//*1'.. .)( /#*.>
Java Methods and Property Syntax
!4*0.+ )/$( *)*/'$)G+-*% /$))/#/*6 -.*/'$)0/*@
*(+' / J >">;)-*$/0$*;)/ ''$K;4*02$''-+$'4$.*1 -/#/*/'$)
''*2.4*0/*$)1*&  -/$)1( /#*..$!/# 42 - */'$)+-*+ -/$ .*-*/# -
.$(+' -!*-(.*!*/'$).4)/3>
JAVA INTEROPERABILITY
264
getXXX()getXXX() and setXXX()setXXX() Pairs
# .*@'' E1 ).F( /#*./-0/0- $)1$)1*'1 .+$- ( /#*.;
+- 73 2$/# get ) set();2$/#*((*). )0.$)"*((*)/4+ =
publicpublic classclass SomethingSomething {
privateprivate StringString foo;
publicpublic StringString getFoo() {
returnreturn foo;
}
publicpublic void setFoo(StringString newFoo) {
foo = newFoo;
}
}
 - ;2 #1 1 ).@./4'  ..*-( /#*.!*- foo 7 ';2$/# getFoo()
- /0-)$)"/# foo 1'0 ) setFoo() #)"$)"$/>
!4*0 ../#$.1* !-*(*/'$);4*0)/- //# getFoo() ) setFoo()
( /#*..$!4*02 -  ..$)" foo +-*+ -/4/#/2. public=
classclass BarBar {
funfun doFoo() {
valval thingy = SomethingSomething()
valval oldFoo = thingy.foo
thingy.foo = "this is the replacement"
}
}
# - $.)*/#$)"./*++$)"4*0!-*(''$)"/# 1( /#*.$- /'4;.*/#$.2*-&.
/**=
classclass BarBar {
funfun doFoo() {
valval thingy = SomethingSomething()
valval oldFoo = thingy.getFoo()
thingy.setFoo("this is the replacement")
}
}
JAVA INTEROPERABILITY
265
*2 1 -;4*0-($"#/#$)//*4*0/*.2$/#/*E+-*+ -/4 ...4)/3F=
Figure 6: Android Studio, Suggesting Property Access Syntax
*;2#$' ''$)"/# ( /#*.$- /'42*-&.;4*0-2*0'+- ! -/#/4*00. /#
.#*-/ -.4)/3>
*/ /#/+-*+ -/4 ...4)/3*)'42*-&.2# )/# - $.(/#$)"" // -)
. // -2$/#/# .( . )( )*+ -/$)"*)/# .( /4+ >*- 3(+' ;4*0
($"#/#1 1'..2$/#.$)"' " // -0/*1 -'* . // -.;.0#.2$/#
)-*$D. TextView )$/./ 3/E+-*+ -/4F># - - . 1 -' setText() 1-$)/.;
 +/$)"$6 - )/+-( / -.>!/# - $.(/#$)"+$-H.0#./#
CharSequence 1 -.$*).*! getText() ) setText() *) TextView H+-*+ -/4 ..
.4)/32*-&.=
classclass GooGoo {
funfun doText(tv: TextViewTextView) {
valval oldText = tv.text
tv.text = "This is new!"
}
}
*2 1 -;*/# -. // -./#/2*-&2$/#*/# -/4+ .- ,0$- 4*0/*''/# . // -
( /#*$- /'4;)*/0. +-*+ -/4..$")( )/.4)/3=
classclass GooGoo {
funfun doText(tv: TextViewTextView) {
valval oldText = tv.text
JAVA INTEROPERABILITY
266
tv.setText(RR.string.app_name)
}
}
!4*0/-4 tv.text = R.string.app_name;4*0" //4+ ($.(/# --*-;./#
+-*+ -/4..$")( )/$. 3+ /$)" CharSequence;) R.string.app_name J$)/#
2*-'*!)-*$K$.) Int>
The isXXX()isXXX() Variant
*( /$( .;2$/# boolean *- Boolean 1'0 .;/# 1" // -( /#*0. . is...()
$)./ *! get...() .)($)"*)1 )/$*)=
publicpublic classclass SomethingSomething {
privateprivate boolean foo;
publicpublic boolean isFoo() {
returnreturn foo;
}
publicpublic void setFoo(boolean newFoo) {
foo = newFoo;
}
}
#$./**(+./*+-*+ -/4$)*/'$);/#*0"#/# +-*+ -/4)( 2$'' /# .( .
/# " // -( /#*)( H isFoo $)/#$.. =
classclass BarBar {
funfun doFoo() {
valval thingy = SomethingSomething()
valval oldFoo = thingy.isFoo
thingy.isFoo = !oldFoo
}
}
#)$''4;/#$.2*-&.!*-)4//4+ ;)*/%0./ Boolean>)+-/$ ;/#*0"#;4*0
2$''-- '4. 1* 0. is...() !*-" // -/#/* .)*/- /0-) boolean *-
Boolean>
JAVA INTEROPERABILITY
267
get()get() as Square Brackets
*/'$)0. ..,0- -& /)*//$*)!*- ..$)"/# *)/ )/.*!/#$)".'$&  List
*- Map>)/# . *! List;/# 1'0 $)/# -& /.$./# R@. $) 3;2#$' !*-
Map;/# 1'0 $)/# -& /.$./# Map & 4>
*/'$)'.* 3/ )./#/)*//$*)/*)41'../#/#.++-*+-$/ ( /#*
.$")/0- .>
*;$!/# 1'..#. get() ) set() ( /#*./#/2*-&*6*!) int *- Integer
$) 3=
publicpublic classclass SomethingSomething {
privateprivate ArrayListArrayList<StringString> stuff = newnew ArrayListArrayList<>(100);
publicpublic StringString get(int i) {
returnreturn stuff.get(i);
}
publicpublic void set(int i, StringString value) {
stuff.set(i, value);
}
}
?/# )*/'$)''*2..,0- @-& /)*//$*)=
classclass BarBar {
funfun doFoo() {
valval thingy = SomethingSomething()
thingy[0] = "hello"
thingy[1] = "world"
println("${thingy[0], ${thingy[1]}")
}
}
)!/;/# get() ) set() ( /#*.)0. )4/4+ !*-/# $-E& 4F;)*/%0./ int=
publicpublic classclass SomethingSomething {
privateprivate HashMapHashMap<StringString, StringString> stuff = newnew HashMapHashMap<>();
publicpublic StringString get(StringString key) {
returnreturn stuff.get(key);
}
JAVA INTEROPERABILITY
268
publicpublic void set(StringString key, StringString value) {
stuff.put(key, value);
}
}
classclass BarBar {
funfun doFoo() {
valval thingy = SomethingSomething()
thingy["first key"] = "hello"
thingy["second key"] = "world"
println("${thingy["first key"]}, ${thingy["second key"]}")
}
}
/$.- '/$1 '40)'$& '4/#/4*02$''#1 1'../#/*)!*-(./*/#$..4./ (;
0/$!$/* .;4*0- 2 '*( /*0. /#$.*/'$))*//$*)/*2*-&2$/#$/>
Dealing with nullnull
*/'$)#. +- '/$*).#$+2$/# null=
I 0''' /4+ .JString?K
I ! ''.J?.K
I '1$.*+ -/*-J?:K
I ).**)
*./')"0" .*)*//- / null 2$/#/# .( - 1 - ) >)+-/$0'-;1* .
)*/>
#$.0. ..*( +-*' (.2# )*/'$)) ./*2*-&2$/#1*% /.>*-
3(+' ;.0++*. /#/2 #1 =
publicpublic classclass JavaStuffJavaStuff {
publicpublic StringString getSomething() {
returnreturn nullnull;
}
}
?)2 /-40.$)"/#$.!-*(*/'$)=
JAVA INTEROPERABILITY
269
valval thingy = JavaStuffJavaStuff().something
println(thingy.length)
# */'$)*(+$' -2$''/ )/*..0( /#/ getSomething() $.- /0-)$)" String;
)*/ String?;).*$/2$''/- / thingy . String?)2 2$''-.#/-0)/$(
2$/# NullPointerException>
!/# 1* #.))*//$*)./#/$)$/ )0''$'$/4;/#*0"#;*/'$)2$''// (+/
/*0. /# (>
importimport androidx.annotation.Nullableandroidx.annotation.Nullable;
publicpublic classclass JavaStuffJavaStuff {
publicpublic @Nullable StringString getSomething() {
returnreturn nullnull;
}
}
 - ;2 0. ))-*$))*//$*); @Nullable;/*$)$/ /#//# - /0-)
1'0 *! getSomething() ($"#/ null>*/'$);$)/0-);2$''/# )/- /
getSomething() .- /0-)$)" String? $)./ *! String>
)4'$--$ .;.0#./# )-*$;0. /# . ))*//$*)./*# '+1)
*/'$) 1 '*+ -.>*2 1 -;)*/'''$--$ .*6 -/# (>.- .0'/; - !0'2# )
''$)"1* !-*(*/'$);)*).$ -$)/-*0$)"/# )0''' /4+ 4*0-. '!>
*- 3(+' ; 1 )$)/#  -'$ -1 3(+' 2$/#*0/ @Nullable;2 *0'2-$/ *0-
*/'$).=
valval thingy: StringString? = JavaStuffJavaStuff().something
println(thingy?.length)
 - ;2 - .4$)"/#/2 *)*//-0./ getSomething() /*'24.- /0-)
)*)@null 1'0 ;.*2 !*- thingy /* String? !*-.! /4D..& >
Java Class Objects
*( /$( .$)1;2 ) /*- ! -/*1 Class *% /.>))-*$ 1 '*+( )/;
*) +-*($) )/. )-$*!*-/#$.$.$)- /$)" 3+'$$/ Intent *% /.=
startActivity(newnew IntentIntent(thisthis, TheOtherActivityTheOtherActivity.class));
JAVA INTEROPERABILITY
270
)1; .class ..083*)/# '..)( - /0-)./# Class *% /..*$/ 2$/#
/#/'..;)2 )+../#/ Class *% //*( /#*./#/) *) >
)*/'$); ::class ..083*)/# '..)( '.*- /0-).)*% /- +- . )/$)"
/# '..>*2 1 -;/#$.$. KClass H*/'$)'..*% /> ) /*0.
::class.java /*" ///# 1 Class *% /$)./ >*/#$.2$'')*/*(+$' =
startActivity(IntentIntent(thisthis, MainActivityMainActivity::classclass))
?0//#$.2$''=
startActivity(IntentIntent(thisthis, MainActivityMainActivity::classclass.java))
# - 2$'' . .$)*/'$)* 2# - 4*0%0./0. ::class>#/( )./#
!0)/$*)/#/4*0- ''$)"/& . KClass +-( / -;+-*'4 0. /#/
!0)/$*)2.2-$// )$)*/'$))$. .$") !*-0. 2$/#*/'$)'.. .>
The SAM Scenario
1 -4)*2)/# );12$).0+ $)" .$ -/*0. !-*(*/'$)/#)$.*/'$)
$/. '!>
*- 3(+' ;/& /#$..$(+' 1$)/ -! =
publicpublic interfaceinterface SomeJavaInterfaceSomeJavaInterface {
void beUseful(StringString value);
}
(+' ( )//$*).) /**1 --$ beUseful() )?2 ''? 0. !0'>
*0)#1 /#  ,0$1' )/$)/ -! $)*/'$);*!*0-. =
interfaceinterface SomeKotlinInterfaceSomeKotlinInterface {
funfun beUseful(value: StringString)
}
/# -/#)/# )( ;/# $)/ -! .. ($ )/$'>
*2 1 -; using /# (!-*(*/'$)$..$")$7)/'4$6 - )/>
*0. /# */'$)$)/ -! ;2 ) /*- / )*% //#/*1 --$ . beUseful()=
JAVA INTEROPERABILITY
271
valval thingy = objectobject : SomeKotlinInterfaceSomeKotlinInterface {
overrideoverride funfun beUseful(value: StringString) {
println(value)
}
}
*0. /# 1$)/ -! ;2 *0'*/# .( .$/#$)"=
valval thingy = objectobject : SomeJavaInterfaceSomeJavaInterface {
overrideoverride funfun beUseful(value: StringString) {
println(value)
}
}
)+-/$ ;/#*0"#;!*-12 )" /242$/#%0./=
valval thingy = SomeJavaInterfaceSomeJavaInterface { println(it) }
SomeJavaInterface $(+' ( )/./# +// -)=$)"' ./-/ /#*>*/'$)
&)*2.#*2/**)1 -/'( 3+- ..$*)$)/*/# $(+' ( )//$*)*!/#/.$)"'
./-/( /#*;2#$#$)/0-)''*2.!*-/# .0$)/.4)/3/#/2 . # - >
0/$!4*0/-40.$)"/#/.4)/32$/# SomeKotlinInterface;4*0!$'2$/#*(+$'
--*->
$./*-$''4;*/'$)$)*/.0++*-//# 0. *!!*-*/'$)$)/ -! .>#
-"0( )/*$'.*2)/*=4*0.#*0')*/- / SomeKotlinInterface>)./ ;0. 
!0)/$*)/4+ ;+*..$'42$/# typealias=
typealiastypealias SomeKotlinInterfaceSomeKotlinInterface = (StringString) -> UnitUnit
*2;4*0)" /.$($'-.4)/3.4*0" /2$/#*)1 -.$*)=
valval thingy: SomeKotlinInterfaceSomeKotlinInterface = { println(it) }
*2 1 -;*/'$)S>V  !0)/$*)'$)/ -! . 2#$#''*2!*-.4)/3!*-
.+ $''4@*)./-0/ $)/ -! .>
voidvoid and UnitUnit
//#$.+*$)/;/#*0"#;4*0(4 2*) -$)"2#/ Unit $.>
''!0)/$*).$)*/'$)- /0-).*( /#$)">!4*0*)*/.+ $!4*/# -2$. ;/#
JAVA INTEROPERABILITY
272
!0)/$*)- /0-). Unit>#$.$..$)"' /*)=/# - $. 3/'4*) Unit $)/#
)1$-*)( )/>
*0*)*/. Unit ( )/$*) /#/(0# 0. (0#*!/# /$( $/) .! '4
-*++ !-*(*/'$).4)/3>*- 3(+' ;/#$.!0)/$*)=
funfun ofMeasure() {
println("hello, world!")
}
?$.- ''4=
funfun ofMeasure(): UnitUnit {
println("hello, world!")
returnreturn UnitUnit
}
!4*0-!0)/$*)*4* .)*/#1 ) 3+'$$/ return;$/- /0-). Unit>);$!4*0-
!0)/$*) '-/$*)* .)*/#1 - /0-)/4+ ;$/- /0-). Unit>*/'$)%0./' /.0.
.&$+/# Unit - ! - ) .>
#$' 2 ).&$+ Unit !-*()/0'!0)/$*) '-/$*))/# - /0-)/4+ ;
!0)/$*)/4+ ))*/.&$+$/> ))*/- +' =
typealiastypealias SomeKotlinInterfaceSomeKotlinInterface = (StringString) -> UnitUnit
2$/#=
typealiastypealias SomeKotlinInterfaceSomeKotlinInterface = (StringString) ->
*-=
typealiastypealias SomeKotlinInterfaceSomeKotlinInterface = (StringString)
J/# '// -*(+$' .0/$.)*'*)" -!0)/$*)/4+ K
1( /#*./#/E- /0-)F void;2# )- ! - ) $)*/'$);- ''4- /0-) Unit>
Type Mapping
!4*0#1 1'..)(  org.yourapp.Restaurant;)4*0- / )$)./)
*!$/$)*/'$);4*0" / org.yourapp.Restaurant *% />
JAVA INTEROPERABILITY
273
#$.(4. (*1$*0.>
*2 1 -;)*/''/4+ .2*-&/#/24># - $.'*)"'$./*!1/4+ ./#/" /
E(++ F/**/'$))/$1 /4+ .>
*- 3(+' =
I 1+-$($/$1 .;'$& int;- /0-) $)/*$)./) .*!*/'$)'.. .;'$& Int
I E*3 F1+-$($/$1 .;'$& Integer;- /0-) $)/*$)./) .*!/#
*-- .+*)$)")0''' */'$)'..;'$& Integer?
I  -/$)*- 1'.. .;'$& Object ) String;" //0-) $)/**/'$)
,0$1' )/.JAny !*- Object;)*/'$)D.*2) String '..$)./ *!
java.lang.StringK
# */'$)*0( )//$*)#. /# !0''-*./ -*!(++ /4+ .>
.0''4;2 *)*//#$)&*0//#$.(0#>*2 1 -;$/(4-$. .*( #'' )" .
2# )0.$)"- 9 /$*)*-.$($'-/ #)$,0 ./#/$).+ /'.. /$'./-0)/$( >
Keyword Differences
# - $.!$-(*0)/*!*1 -'+$)& 42*-. /2 )*/'$))1;.0#. class
) return>*2 1 -;/# - - .*( */'$)& 42*-./#/- )*/1& 42*-.;
.0#. object ) when>
.- .0'/;!-*(/$( /*/$( ;4*02$''. !0)/$*))( .2-++ $)&/$&. H
/#$.$./*''*24*0/*'';.4;1 when() !0)/$*); .+$/ /# !//#/ when $.
*/'$)& 42*->
Java Calling Kotlin
!4*0-  (-&$)"*)) 2*/'$)@!*0. +-*% /;4*0($"#//#$)&/#/4*0-
*)'4*) -)$.#1$)"*/'$)* ''1* $)'$--$ .>
*2 1 -;!- ,0 )/'4;1* ) ./*''*/# -1* /#/2 .0++'4=
I $''&.
I $.0'.. .J >">;''./*( /#*./#/2 *1 --$ K
) /**/#*. /#$)".$)*/'$)/**;2#$#( )./#/.*( 1* $."*$)"
JAVA INTEROPERABILITY
274
/*) /*''/**/'$)* /#/2 2-$/ >
*- 3(+' ;$))-*$ 1 '*+( )/; Activity ) AppCompatActivity - 1
'.. .;0/*/'$)@!*0. +-*% /2$''- / .0'.. .$)*/'$)>.- .0'/=
I *02$'') /**1 --$ 1( /#*.'$& onCreate();- /$)" ,0$1' )/
*/'$)!0)/$*).;)1* $)/# !-( 2*-&*-)-*$'$--$ .2$''
2$)0+''$)"4*0-*1 --$ )( /#*.
I *0(4- /  1 )/'$./ ) -.;!*-/#$)".'$& 0//*)'$&.;2#$#2$''- .0'/
$)1'.. .'$& Button ''$)"4*0-*/'$)'$./ ) -.
*-(''4;/#$.''2*-&.2$/#*0/(0#$..0 >*2 1 -;!-*(/$( /*/$( ;2 *
#1 /*2*--4$/*0/#*21''.*0-*/'$)* >
Public Properties
*/'$)+-*+ -/$ .'**&'$& 17 '.>)- '$/4;*/'$)+-*+ -/4$.*($)/$*)
*!=
I " // -!0)/$*)
I . // -!0)/$*)J!*-(0/' var +-*+ -/$ .K
I E&$)"F7 '/#//0''4./*- ./# /
)*/'$);2 !- ,0 )/'4$")*- /#$.$./$)/$*)>)1;2 ))*/$")*- $/;.
+0'$+-*+ -/4$. not /# .( .+0'$17 '> #1 )*$- / ../*/#
7 '!-*(1;0/2 )2*-&2$/#/# " // -J);2# - 1$'' ;/# . // -K>
*;$!2 #1 /#$.*/'$)'..=
classclass KotlinStuffKotlinStuff {
valval immutableStuff = "foo"
varvar mutableStuff = "bar"
}
?/# )2 )0. /# " // -.!*-*/#+-*+ -/$ .)/# . // -!*- mutableStuff
!-*(1=
publicpublic classclass JavaStuffJavaStuff {
publicpublic void useKotlinStuff(KotlinStuffKotlinStuff stuff) {
StringString immutable = stuff.getImmutableStuff();
StringString mutable = stuff.getMutableStuff();
JAVA INTEROPERABILITY
275
stuff.setMutableStuff("and now for something completely different");
}
}
*/'$)0/*(/$''4- / ./# " // -). // -0.$)"./)-1 ))($)"
-0' .;/# .( *) ./#/$/#)' . $)/# */'$)@''$)"@1$- /$*)>
4 !0'/;/# . 2$'' get...() ) set...();2# - ... $.- +' 4/# )(
*!/# +-*+ -/4;2$/#/# $)$/$'' // -+$/'$5 JimmutableStuff - .0'/.$)
getImmutableStuff()K>#$.$./-0  1 )!*- Boolean +-*+ -/$ .H*/'$)* .)*/
.2$/#/* is...() .4)/30/*(/$''4>
*2 1 -;$!/# +-*+ -/4)( $/. '! "$).2$/# is;/# )*/'$)2$''#1 /# " // -
!0)/$*)0. /# .( )( ./# +-*+ -/4;2$/#. // -!0)/$*)/#/- +' . is
2$/# set>*;*/'$) isMutable +-*+ -/4=
classclass KotlinStuffKotlinStuff {
varvar isMutable = truetrue
}
?- .0'/.$) isMutable() ) setMutable() 1( /#*.!*-0./*$)1*& *)/# 1
.$ =
publicpublic classclass JavaStuffJavaStuff {
publicpublic void useKotlinStuff(KotlinStuffKotlinStuff stuff) {
ifif (stuff.isMutable()) {
stuff.setMutable(falsefalse);
}
}
}
*/ /#/'..(*$7 -.'$& data *- sealed #1 )*$(+/*)/# . )($)"-0' .;
.*/#$. KotlinStuff 1-$)/2*-&./# .( ./# +- 1$*0.*) !-*(1D.
./)+*$)/=
data classdata class KotlinStuffKotlinStuff(varvar isMutable: BooleanBoolean = truetrue)
Other Top-Level Declarations
.2 #1 . )/#-*0"#*0//#$.**&;*/'$).0++*-/./*+@' 1 '!0)/$*).=
!0)/$*)./#/- .$ *0/.$ *!)4*/'$)'..=
JAVA INTEROPERABILITY
276
funfun doSomething() {
println("something!")
}
$) 1* .)*/#1 $/.*2)/*+@' 1 '!0)/$*);$/) .'$//' # '+/*''/#*.
!0)/$*).>
-*(1D../)+*$)/;/# /*+@' 1 '!0)/$*)$. static ( /#**)'..2#*.
)( $.=
I /# )( *!/# */'$).*0- 7' *)/$)$)"/# /*+@' 1 '!0)/$*)?
I ?2$/# Kt .083 /*$/
!/# doSomething() !0)/$*).#*2)$)/# *1 .)$++ /2 - $) KotlinStuff.kt
.*0- 7' ;/# )1)- ! -/*$/. static ( /#**) KotlinStuffKt=
publicpublic classclass JavaStuffJavaStuff {
publicpublic void doSomethingElse() {
KotlinStuffKtKotlinStuffKt.doSomething();
}
}
*+@' 1 '+-*+ -/$ .- '.* ..$' !-*(1>*2 1 -;'$& '..+-*+ -/$ .;1
2$''0. " ) -/ " // -). // -( /#*.4 !0'/>#*. ++ -. static
( /#*.*)/# 1'.." ) -/ !-*(/# '..)( ;.2$/#/*+@' 1 '
!0)/$*).>
*;$! KotlinStuff.kt #.=
valval GLOBAL_VARIABLE = "it's a small world"
?/# )1)*/$)/# 1'0 1$=
publicpublic classclass JavaStuffJavaStuff {
publicpublic void doSomethingGlobal() {
StringString value = KotlinStuffKtKotlinStuffKt.getGLOBAL_VARIABLE();
}
}
)  3 +/$*)$.$! const $.0. =
constconst valval GLOBAL_VARIABLE = "it's a small world"
# );/# 1'0 $. ..$' $)1. static 7 '=
JAVA INTEROPERABILITY
277
publicpublic classclass JavaStuffJavaStuff {
publicpublic void doSomethingGlobal() {
StringString value = KotlinStuffKtKotlinStuffKt.GLOBAL_VARIABLE;
}
}
#$.$.*) 1)/" *!0.$)" const val $)./ *!%0./ val=4*0#1 (*-
)/0-'24*!- ! - )$)"/# *)./)/!-*(1>
Adjustments via Annotations
*( /$( .;4*0- "*$)"/*) /*#1 */'$)#)" $/.++-*#.'$"#/'4/*
(*- 1@!-$ )'4>#-*0"#. -$ .*!))*//$*).;4*0)'/ -#*2*/'$)*
) 0. !-*(1;2$/#*0/$)/ -! -$)"2$/##*2/#/.( */'$)* )
0. !-*(*/'$)$/. '!>
@file:JvmName()
.2 %0./.2;/*+@' 1 '*/'$)!0)/$*).) '' . static ( /#*.!-*(1;
0.$)".4)/# /$1'... *)/# */'$)7' )( >*( /$( .;/#*0"#;/#/
7' )( $.)*/+-/$0'-'4*)1 )$ )/>*0(4#1 #*. )/*+0//#/*/'$)*
$) ASourceFileWithAVeryLongName.kt;!*-$)"4*0/*''$/./*+@' 1 '!0)/$*).*)
ASourceFileWithAVeryLongNameKt>-;+ -#+./# 7' )( $..#*-/;0/$/* .)*/
(& . ). .1'..)( >-;+ -#+.4*0%0./*)*/'$& /# Kt .083*)/#
)>
**)/-*'/#$.;+0/ @file:JvmName() ))*//$*)./# 7-./'$) *!/# .*0- 7'
*)/$)$)"/# /*+@' 1 '!0)/$*)./#/4*02)//*0. !-*(1>*0.0++'4/#
))*//$*)2$/#/# )( /#/4*02)/*/'$)/*0. !*-/# .4)/# /$1'..=
@file:JvmNameJvmName("CoolStuff")
valval GLOBAL_VARIABLE = "it's a small world"
?/# )1)*/$)/# 1'0 40.$)"4*0-.0++'$ '..)( =
publicpublic classclass JavaStuffJavaStuff {
publicpublic void doSomethingGlobal() {
StringString value = CoolStuffCoolStuff.getGLOBAL_VARIABLE();
}
}
JAVA INTEROPERABILITY
278
@file:JvmMultifileClass
!4*00. /# .( @file:JvmName() ))*//$*)J2$/#/# .( )( K$)T]*/'$)
.*0- 7' .;4*02$''" /0$' --*-># ..0(+/$*)$./#/4*0( *+4@)@
+./  --*-;!*-" //$)"/*#)" /# )( >
*2 1 -;*/'$)/0''4.0++*-/.#1$)"(*- /#)*) */'$)7' *)/-$0/ /*/#
.( 1! '..!*-/#$)".'$& /*+@' 1 '!0)/$*).; object - ! - ) .;).*
*)>*0%0./#1 /**+/$)/*$/4'.*#1$)" @file:JvmMultifileClass .)
))*//$*);/4+$''4$(( $/ '4!/ -/# @file:JvmName() ))*//$*)=
@file:JvmNameJvmName("CoolStuff")
@file:JvmMultifileClassJvmMultifileClass
// this is in one Kotlin file
valval GLOBAL_VARIABLE = "it's a small world"
@file:JvmNameJvmName("CoolStuff")
@file:JvmMultifileClassJvmMultifileClass
// this is in another Kotlin file
valval OTHER_VARIABLE = "...but we can make it bigger!"
*0-1* )- ! - ) */# CoolStuff.GLOBAL_VARIABLE )
CoolStuff.OTHER_VARIABLE; 1 )/#*0"#/#*. /2*1-$' .-  7) $). +-/
*/'$).*0- 7' .>
@JvmStatic
# /# -4*00. @file:JvmName() *-)*/;1)''/*+@' 1 '*/'$)!0)/$*).>
# )$/*( ./*!0)/$*).*) companion *% /;/#$)"." /$/(*-
*(+'$/ >
*- 3(+' ;.0++*. /#/4*0#1 */'$)'../#/#. companion object 2$/#
.*( .*-/*!!/*-4!0)/$*)!*-- /$)"$)./) .=
JAVA INTEROPERABILITY
279
classclass KotlinThingyKotlinThingy {
companioncompanion objectobject {
funfun gimme() = KotlinThingyKotlinThingy()
}
}
1)''/#$.4- ! --$)"/* Companion .4)/# /$) ./ '..=
publicpublic classclass JavaStuffJavaStuff {
publicpublic void doSomethingGlobal() {
KotlinThingyKotlinThingy thingy = KotlinThingyKotlinThingy.Companion.gimme();
}
}
# - $.)*/#$)"2-*)"2$/#/#$.>*2 1 -;$!4*02)/;4*0) @JvmStatic /*
/# companion object !0)/$*)=
classclass KotlinThingyKotlinThingy {
companioncompanion objectobject {
@JvmStatic
funfun gimme() = KotlinThingyKotlinThingy()
}
}
# KotlinThingy.Companion.gimme() .4)/32$''./$''2*-&$)1;0/)*24*0
'.*)'' gimme() static ( /#**)/# */'$)'..;(0#'$& #*24*00.
/# companion object !0)/$*).$)*/'$)$/. '!=
publicpublic classclass JavaStuffJavaStuff {
publicpublic void doSomethingGlobal() {
KotlinThingyKotlinThingy thingy = KotlinThingyKotlinThingy.gimme();
}
}
)*/# -+' 2# - @JvmStatic ) 0. !0'$.2$/#)( *% /.=
objectobject KotlinSingletonKotlinSingleton {
funfun heyNow() {
println("What what?")
}
}
1)'' heyNow();0/$/) ./*- ! -/*) INSTANCE *!/# *% /=
JAVA INTEROPERABILITY
280
publicpublic classclass JavaStuffJavaStuff {
publicpublic void doSomethingGlobal() {
KotlinSingletonKotlinSingleton.INSTANCE.heyNow();
}
}
!;$)./ ;/# */'$)* 0. . @JvmStatic=
objectobject KotlinSingletonKotlinSingleton {
@JvmStatic
funfun heyNow() {
println("What what?")
}
}
?/# )/# 1* ).&$+/# INSTANCE )'' heyNow() $- /'4*)/# */'$)
*% /=
publicpublic classclass JavaStuffJavaStuff {
publicpublic void doSomethingGlobal() {
KotlinSingletonKotlinSingleton.heyNow();
}
}
@Throws
*/'$)* .)*/#1 # &  3 +/$*)./# 241* .H4*0- ) 1 - required
4/# */'$)*(+$' -/*2-+.*( /#$)"$) tryGcatch *)./-0/>''@ 7) 
3 +/$*).- /- / .-0)/$(  3 +/$*).4*/'$)>)2 #1 )* throws
& 42*-$)*/'$)/*1 -/$. /#/ -/$)!0)/$*)2$''/#-*2) 3 +/$*)>
*- 3 +/$*)./#/ 3/ )1D. RuntimeException;/#$.$.7) >
./-//*-0)$)/*+-*' (.2# )*/'$)* ($"#//#-*2.*( */# -.*-/*!
3 +/$*)J >">; IOExceptionK and 2 2)//#/ 3 +/$*)/* 0"#/41*
''$)"/# */'$)* ># 1*(+$' - 3+ /.''# &  3 +/$*)./*
- +*-/ 4 throws & 42*-.*( 2# - ;)$/2$''- !0. /**(+$' * /#/
/-$ ./* catch ) 3 +/$*)/#/$.)*/ RuntimeException and 2.)*/- +*-/ 4
throws>
*;$!2 #1 .*( */'$)* /#/($"#//#-*2# &  3 +/$*)=
JAVA INTEROPERABILITY
281
classclass KotlinThingyKotlinThingy {
funfun loadStuff() {
// TODO work that might throw an FileNotException, which we simulate by...
throwthrow FileNotFoundExceptionFileNotFoundException()
}
}
?)2 /-4/* catch $/$)1=
publicpublic classclass JavaStuffJavaStuff {
publicpublic void useTheLoadedStuff() {
trytry {
KotlinThingyKotlinThingy thingy = newnew KotlinThingyKotlinThingy();
thingy.loadStuff();
}
catchcatch (FileNotFoundExceptionFileNotFoundException ex) {
}
}
}
?2 " /1*(+$'  --*-;*(+'$)$)"/#/ loadStuff() * .)*//#-*2
FileNotFoundException>
# 2*-&-*0)!*-/#$.$./*))*// /# */'$)!0)/$*)2$/# @Throws;$ )/$!4$)"
/#  3 +/$*)J.K/#//# !0)/$*)(4/#-*2=
classclass KotlinThingyKotlinThingy {
@Throws(FileNotFoundExceptionFileNotFoundException::classclass)
funfun loadStuff() {
// TODO work that might throw an FileNotException, which we simulate by...
throwthrow FileNotFoundExceptionFileNotFoundException()
}
}
#$.* .)*/#)" /# .4)/30. 41/*''/# !0)/$*);0/$/''*2./#
catch /*2*-&>
!/# !0)/$*)/#-*2.(*- /#)*) # &  3 +/$*);.0++'4)--4*!
3 +/$*)'.. ./*/# exceptionClasses +-*+ -/4=
JAVA INTEROPERABILITY
282
classclass KotlinThingyKotlinThingy {
@Throws(exceptionClasses = [FileNotFoundExceptionFileNotFoundException::classclass,
CertificateExceptionCertificateException::classclass])
funfun loadStuff() {
// TODO work that might throw either of those exceptions
}
}
@JvmField
.2.)*/  -'$ -$)/# **&;*/'$)+-*+ -/4$.( 0+*!/#- +$  .=
I 7 '
I " // -( /#*
I . // -( /#*
- ,0 )/'4;2 $")*- /# 7 '$)*/'$);.*0-- ! - ) ./*/# +-*+ -/4
0/*(/$''40. /# " // -). // ->$($'-'4;4 !0'/;1))*/ ../#
7 'H*)'4/# */'$)" // -). // --  3+*. /*1>
*2 1 -;/# - (4 -- *.$*).2# - 4*0#1 1* /#/) ./* ..
*/'$)7 '$- /'4>)/#*. . .;/# @JvmField ))*//$*)*)/# */'$)
+-*+ -/42$''(& /# 7 '1$'' ;2$/#2#/ 1 -1$.$$'$/4$. 7) *)/#
+-*+ -/4J+0'$4 !0'/;0/*0' protected; />K>
@JvmOverloads
*/'$).0++*-/. !0'/+-( / -1'0 .*)*)./-0/*-.)!0)/$*).>1;
/#-*0"#1Z;* .)*/>4 !0'/;2#/*/'$) 3+*. ./*12$'' !0)/$*)
/#/- ,0$- .''+-( / -.;( )$)"/#//#  !0'/1'0 .2$'' $")*- >
*- 3(+' ;$!4*0#1 /#$.*/'$)* =
classclass KotlinThingyKotlinThingy {
funfun deeeeeeeeeefault(foo: StringString, bar: IntInt = 1, goo: FloatFloat = 2.3f) {
// TODO something
}
}
?4*0($"#//#$)&/#//#$.1*0'2*0'2*-&=
JAVA INTEROPERABILITY
283
publicpublic classclass JavaStuffJavaStuff {
publicpublic void iCanHazDefaults() {
KotlinThingyKotlinThingy thingy = newnew KotlinThingyKotlinThingy();
thingy.deeeeeeeeeefault("non-default");
}
}
*2 1 -;/# 1*(+$' -2$''*(+'$)/#/4*0*)*/#1 1'0 .!*-/# . *)
)/#$-+-( / -.>1 )/#*0"#*/'$)'' -.*)*/- ,0$- /# (;1'' -.
*>
# - - /2* 3 +/$*)./*/#$.-0' >
) $.2$/#*)./-0/*-.>!4*0-'..D*)./-0/*-#. !0'/1'0 .!*- all
*)./-0/*-+-( / -.;/# ). *)-45 -*@+-( / -*)./-0/*-$.'.*
1$'' !*-1>*;$!2 - 1$. /# *1 */'$)* /*=
classclass KotlinThingyKotlinThingy(parameters: StringString = "", are: IntInt = 4, fundamental: BooleanBoolean =
truetrue) {
funfun deeeeeeeeeefault(foo: StringString, bar: IntInt = 1, goo: FloatFloat = 2.3f) {
// TODO something
}
}
?/# )/# 1* 2$''./$'' ' /*- /  KotlinThingy 0.$)" new
KotlinThingy(); 1 )/#*0"#/# */'$)*)./-0/*-0. . !0'/+-( / -1'0 .>
# */# - 3 +/$*)/*/#$.-0' $.$!4*0++'4 @JvmOverloads /*/# *)./-0/*-*-
!0)/$*)>#$.0. ./# */'$)*(+$' -/*" ) -/ $/$*)'1 -.$*).*!/#
*)./-0/*-*-!0)/$*);+-*"- ..$1 '4++'4$)" !0'/+-( / -.)/# - !*- )*/
- ,0$-$)"1/*.0++'4/# (>
)++'4/#//* KotlinThingy */#!*-/# *)./-0/*-)/# !0)/$*)=
classclass KotlinThingyKotlinThingy @JvmOverloads constructorconstructor(parameters: StringString, are: IntInt = 4,
fundamental: BooleanBoolean = truetrue) {
@JvmOverloads funfun deeeeeeeeeefault(foo: StringString, bar: IntInt = 1, goo: FloatFloat = 2.3f) {
// TODO something
}
}
#$.1 -.$*)*!/# *)./-0/*-*)'4#./# '// -/2*+-( / -. !0'/ </# 7-./
*) $.- ,0$- >
JAVA INTEROPERABILITY
284
)1;2 )*2#1  ../*/#- KotlinThingy *)./-0/*-.=
I KotlinThingy(String, int, boolean)
I KotlinThingy(String, int)
I KotlinThingy(String)
$($'-'4;2 #1  ../*/#- 1 -.$*).*!/# !0)/$*)=
I deeeeeeeeeefault(String, int, float)
I deeeeeeeeeefault(String, int)
I deeeeeeeeeefault(String)
.- .0'/;1* '$& /#$.2$''*(+$' ' )'4=
publicpublic classclass JavaStuffJavaStuff {
publicpublic void iCanHazDefaults() {
KotlinThingyKotlinThingy thingy = newnew KotlinThingyKotlinThingy("whatever");
thingy.deeeeeeeeeefault("non-default");
}
}
Prefixes on Other Annotations
1#.$/.*2)))*//$*).;).*( /$( .4*02$'') /*++'4)))*//$*)
 7) $)1/*.*( */'$)* >
*-(''4;/#$.2*-&.7) >
*2 1 -;.*( /$( .4*0) /*++'4)))*//$*)/*.*( /#$)" speci*c /#/
*/'$)#$ . #$)*/# -'4 -.>
# $"" ./0'+-$/# - - */'$)+-*+ -/$ .># . (+/*7 ';" // -;)
. // -$)1>!4*0) /*+0//# ))*//$*)*)%0./*) *!/#*. /#$)".;/#
))*//$*)'*) 2$'')*/2*-&>)./ ;4*0#1 /*+- 73/*/# ))*//$*)
$/. '!=
I field: /*++'4/# ))*//$*)/*/# E&$)"7 'F*!/# +-*+ -/4
J@field:YourAnnotationK
I get: /*++'4/# ))*//$*)/*/# " // -J@get:YourAnnotationK
I set: /*++'4/# ))*//$*)/*/# . // -J@set:YourAnnotationK
JAVA INTEROPERABILITY
285
Scenario: JUnit4 Rules
*- 3(+' ;$!4*00. )$/V!*-/ ./$)";4*0($"#/2)//*0. .*( )$/V-0' .>
)1;4*0++'4/# @Rule ))*//$*)/*/# 7 '/#/#*'./# -0' =
publicpublic classclass YourTestYourTest {
@Rule
publicpublic finalfinal TemporaryFolderTemporaryFolder temp = newnew TemporaryFolderTemporaryFolder();
// TODO rest of testing
}
 - ;2 0. /# ./*&)$/V TemporaryFolder -0' ;/*" ) -/ / (+*--4!*' -
.0$/' !*-*0-/ ./$)">
# $- / ,0$1' )/$)*/'$)$./*+0//# ))*//$*)*)+-*+ -/4=
classclass YourTestYourTest {
@Rule
valval temp = TemporaryFolderTemporaryFolder()
// TODO rest of testing
}
*2 1 -;/#$.*)!0. ./# ))*//$*)+-* ..*->)./ ;4*0)0. @get:Rule /*
++'4/# ))*//$*)/*/# " // -=
classclass YourTestYourTest {
@getget:RuleRule
valval temp = TemporaryFolderTemporaryFolder()
// TODO rest of testing
}
Scenario: Dagger 2 Qualifiers
*0)0. 0./*())*//$*).2$/#"" -T/*# '+,0'$!4+-/$0'-0. .*!
*% /.>
*- 3(+' ;4*0($"#/ 7)  @SpecialCase 0./*())*//$*)=
@Qualifier
@Retention(AnnotationRetentionAnnotationRetention.RUNTIMERUNTIME)
annotationannotation classclass SpecialCaseSpecialCase
JAVA INTEROPERABILITY
286
?/# )0. /#/!*-.*( *% / 7) $)(*0' =
@Module
classclass YourModuleYourModule {
@Provides
@Singleton
@SpecialCase
funfun heyThisIsSpecial() = WhateverWhatever()
}
*0/# )($"#/2)//* @Inject /#/ @SpecialCase *% /.+-*+ -/4.*( 2# - >
*(& /#/2*-&.0 ..!0''4;4*0(4) /*0. @field:SpecialCase /*/ ''
*/'$)/*++'4/# @SpecialCase ))*//$*)/*/# &$)"7 '*!/# +-*+ -/4=
abstractabstract classclass SomethingOrAnotherSomethingOrAnother {
@Inject @field:SpecialCaseSpecialCase lateinitlateinit varvar special: WhateverWhatever
// TODO rest of this class, including an inject() call on a Dagger component
}
Decompiling Kotlin to Java
*( /$( .;/* 0"1G*/'$)$)/ -*+ -$'$/4$..0 .;$/# '+./*. /#
,0$1' )/1* !*-+-/$0'-*/'$).*0- 7' >
)/ ''$))-*$/0$**/#*6 -/#$.+$'$/4>-*(/# **'._*/'$)
( )0;#**. E#*2*/'$)4/ * F>#$.2$''*+ )0+/# E*/'$)4/ * F1$ 2
)2$''.#*24*0/# 4/ * ..*$/ 2$/#/# 0-- )/*/'$).*0- 7' >
)/#/1$ 2;/# E *(+$' F0//*)2$''// (+//* *(+$' /#/4/ * $)/*
1.*0- * ;*+ )$)"0+/# - .0'/$))*/# - $/*-/>
#$.2*-&.0/$.)*/2$/#*0/$/.92.=
I  *(+$' 4/ * ) (*- $80'//*- ;.'*'1-$' ./ )/*
0. " ) -/ )( .J >">; var2K-/# -/#)#0()@- ' )( .
I #  *(+$'/$*)+-* ..) 1 -4.'*2; + )$)"*)*/#/# $- /
.$5 *!/# */'$).*0- 7' )2#//#/.*0- 0. ./#/) ./*
*)1 -/ 
I ))-*$/0$*$)+-/$0'-; $/(4/& $/*!2*-&/*" //#
 *(+$' -/*-0)
JAVA INTEROPERABILITY
287
Changes in Newer Kotlin Versions
*/'$)& +.#)"$)">
 ) -''4) 2 -1 -.$*).*!*/'$)- &2-.@*(+/$' 2$/#*' -*) .>*;
2# )2 .2$/#/*) 2 -1 -.$*)*!*/'$)J.4;$) build.gradle *!))-*$
+++-*% /K;(*./*!*0- 3$./$)"* .#*0'./$''2*-&>
*2 1 -;) 2 -*/'$)1 -.$*).*6 -) 2! /0- ./#/2 )0. $)*0-
 1 '*+( )/>
)/#$.#+/ -;2 2$'''**&/#)" .$)/# /2*) 2 ./*/'$)1 -.$*).=S>U)S>V>
Version 1.3
)*1 ( -TRSZ; /-$).- ' . */'$)S>U>#$.2.- '/$1 '4.(''0+/
*1 -*/'$)S>T;0/$/$! 2) 2! /0- .*!)*/ >*2 1 -; 1 )/*4;.*(
*!/#*. ! /0- .- ./$''(-& .+- @- ' . H)*/ 1 -4/#$)"$)./' */'$)
1 -.$*)$../' >
$/#/#/$)($);# - - .*( *! /# #)" .$)/-*0 $)*/'$)S>U>
when()when() and Subject Variables
.2 .2 -'$ -$)/# **&;4*0)0. ) 3+- ..$*)$) when() /*+-*1$ /#
1'0 !*-*(+-$.*)$) #*!/# -)# .=
valval thingy = "foo"
whenwhen (thingy) {
"foo" -> println("something")
289
"bar" -> println("something else")
elseelse -> println("and now for something completely different")
}
J!-*( N2# )*-&.2$/#)44+ N$)/# '..**&K
/-/$)"2$/#*/'$)S>U;4*0)(& /#/ 3+- ..$*)'.* ./*- $)'*'
1-$' =
funfun thingyProvider() = "like, whatever"
funfun main() {
whenwhen (valval thingy = thingyProvider()) {
"foo" -> println("something")
"bar" -> println("something else")
elseelse -> println("we received: $thingy")
}
}
J!-*( N+/0-$)"'0 .$)2# )N$)/# '..**&K
# 1-$' D..*+ $./# when() $/. '!;.*4*0)- ! - ) $/!-*(/# -)# .
0/)*/)42# -  '. >
#$.$.+-/$0'-'40. !0'!*- else -)# .;.0#.!*-'*""$)"0) 3+ / 1'0 .>
JVM Interop for Interfaces
!4*0#1  companion object !*-)$)/ -! ;4*0#1 $(+-*1 $)/ -*+ -$'$/4
*+/$*).2$/#1=
I *0)(-&+-*+ -/$ .$)/# companion object 2$/# @JvmField
I *0)(-&!0)/$*).$)/# companion object 2$/# @JvmStatic
)*/#. .;!-*(1D../)+*$)/;/# .  *( static ( ( -.*!/#
$)/ -! >
*;.0++*. 2 #=
interfaceinterface SomethingSomething {
companioncompanion objectobject {
@JvmField
valval tag: StringString = "Something!"
@JvmStatic
CHANGES IN NEWER KOTLIN VERSIONS
290
funfun printMe() {
println(tag)
}
}
// TODO add rest of interface
}
-*(1* ;2 )- ! - ) Something.tag ) Something.printMe()>
Inline Classes
/ -$)/# **&;2 2$'' 3+'*- $)'$) !0)/$*).>$(+'4+0/;!0)/$*)(-&
2$/# inline 2*-&.'$& - "0'-!0)/$*);0//# *(+$' -E$)'$) .F$/.
$(+' ( )//$*)/ #+' $/$.0. >
*/'$)S>U - '/ +$'$/4;'' $)'$) '.. .>#$.! /0- $../$'' ( 
/* $)'+#.// ;0/$/*6 -..*( $)/ - ./$)"+*..$$'$/$ .> 2$'' 3+'*- $)'$)
'.. .(*- '/ -$)/# **&>
Contracts
-'$ -$)/# **&;2 *1 - .(-/./.;2# - */'$)2$''''*2.4)/3/#/*)
/# .0-! . (.$(+*..$'  0. $/$)! -./#/$/ is +*..$' 0) -/#
$-0(./) .>
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
funfun hop() = println("Hop!")
}
classclass AxolotlAxolotl : AnimalAnimal() {
funfun swim() = println("Swish!")
}
funfun main() {
valval critter: AnimalAnimal = FrogFrog()
whenwhen (critter) {
isis FrogFrog -> critter.hop()
isis AxolotlAxolotl -> critter.swim()
}
}
CHANGES IN NEWER KOTLIN VERSIONS
291
J!-*( N(-/./.N$)/# '..**&K
 - ;2 ) hop() ) swim() *) critter  0. /# *(+$' -&)*2.(*- *0/
/# /4+ *! critter "$1 )/# -0' . )* $)/# when -)# .>
*2 1 -;+-$*-/**/'$)S>U;/# - 2 - *)'4! 2+' .2# - .(-/./.*0'
# '+>*/'$)S>U..4./ (*!E*)/-/.F/#/''*2./#$..*-/*! #1$*-$)(*-
+' .?$)'0$)"$)* 2-$// )44*0J/#*0"#/#$.+-/$. 3+ -$( )/'K>
/$''4;$/( )./#/$!4*0#1 */'$)S>T*-*' -* ;2# )4*0(*1 /*
*/'$)S>U;4*0(47)/#/4*0-*-'$)/ -2$''- +*-/0)#*!+' .2# -
4*0)- (*1 ()0'./.;0))  ..-4 null # &.;)/# '$& ># . 2$''
- 9 //# *)/-/./#/2 -  /*/# */'$)./)-'$--4>
*)"@/ -(;E0./*(*)/-/.F;2-$// )4*-$)-4 1 '*+ -.;2$'' )*+/$*)>.
)*/ *1 ;/#$.$.*).$ -  3+ -$( )/'> #$.*0( )/ *0/'$) ./# ++-*#>
Unsigned Integer Types
*/'$)S>U  .0++*-/!*-0).$") )0( -$/4+ .;.0#. UInt ) ULong>
*/ ;/#*0"#;/#//# . - ./$''*).$ - /* $) /!*-(>)' ..4*0#1 
.+ $7- .*)/*0. /# (J >">;.$(+'$!4$)/ -*+ -$'$/42$/#.*( * $)
)-*$K;$/$.+-*'4 // -/*' 1 /# ('*) !*-)*2>
Version 1.4
0"0./TRTR-*0"#/0.*/'$)S>V;2#$##. '*)" --*./ -*! 1 '*+ -@!$)"
#)" .;/#*0"#/# 4- *!($3 $(+*-/) >#$.. /$*) 3($) ..*( /#/
(4  )*0)/ - (*- *!/ )4*-$)-4*/'$) 1 '*+ -.>
Exception Changes
) #)" /#/($"#/0. 4*0/*) %0./( )/./*4*0-* $.$)#*2*/'$)
-$. .)0''+*$)/ - 3 +/$*).$)*/'$)G;$)'0$)"$))-*$++.>
*-( -'4;)0''+*$)/ -- .0'/ $))4)0( -*!+*..$'  3 +/$*).;$)'0$)"=
I IllegalArgumentException
I IllegalStateException
I KotlinNullPointerException
CHANGES IN NEWER KOTLIN VERSIONS
292
I TypeCastException
?$)$/$*)/*1D. NullPointerException>
)*/'$)S>V;*/'$))*2- '$'4" ) -/ . NullPointerException $)''. .>
Probably 4*0*)*/#1 * /#/ + ).*)/# +-/$0'-/4+ *!*) *!/# .
3 +/$*).>#/$. 0. 0.0''42 *)*/ 3+'$$/'42*--4*0//#$. 3 +/$*)
*(+- /**/# -.>0/;$!4*0# tryGcatch /#/ + ) 0+*))0''+*$)/ -
 $)"-$. .*) *!/# !*-( - 3 +/$*)/4+ .;/#/* 2$'') /* %0./ >
Trailing Commas
#1 '*/*!*((@ '$($/ /#$)".$)*/'$);+-/$0'-'4''*!*0-+-( / -
'$./.=
valval spices = mutableListOf("Ginger", "Posh", "Scary", "Sporty", "Baby")
*2;2 )#1 /-$'$)"*((.;2$/#*0/0.$)".4)/3 --*->#$.$.#)4
2# )/#  )/-$ .- ''*)$)$1$0''$) .;+-/$0'-'4!*-*+4$)")+./$)"=
valval spices = mutableListOf(
"Ginger",
"Posh",
"Scary",
"Sporty",
"Baby",
"Pumpkin",
)
# /-$'$)"*(($.$")*- 4/# ')"0" +-. ->
Mixed Named and Positional Parameters
/0. /* /#/)( +-( / -.*0'*)'4*( //#  )*!''=
doSomething(2, 4, 6, 8, whoDoWeAppreciate = "everyone!")
# $ 2./#/)( +-( / -.(*./'42*0' 0. !*-*+/$*)'+-( / -.;
2# - /# !0)/$*)# '-  !0'/1'0 .>
*2 1 -;. *)-40. *!)( +-( / -.$..$(+'4!*-*0( )//$*)>*-
CHANGES IN NEWER KOTLIN VERSIONS
293
/#/;*/'$)S>V)*2.0++*-/.)( +-( / -.$)-$/--4.+*/.=
funfun doSomething(
colors: IntInt,
sides: IntInt,
quantity: IntInt,
theseParametersAreNotSerious: IntInt,
whoDoWeAppreciate: StringString = ""
)
doSomething(2, sides = 4, quantity = 6, 8, whoDoWeAppreciate = "everyone!")
.$''4;0+/#-*0"#/# './+*.$/$*)'+-( / -;/# )( .- +0- '4
*0( )//$*)H/# +*.$/$*)*!/# +-( / -$.2#/(// -.>
Functional Interfaces
-'$ -$)/# **&;2 .2 ;/# $)"' ./-/ /#*+// -)>//& .*
'$& /#$.=
valval thingy = objectobject : SomeJavaInterfaceSomeJavaInterface {
overrideoverride funfun beUseful(value: StringString) {
println(value)
}
}
?).#-$)&.$/*2)/** '$& /#$.=
valval thingy = SomeJavaInterfaceSomeJavaInterface { println(it) }
*2 1 -;/#$.2.'$($/ /*$)/ -! ./#/#.$)"' ./-/( /#*J >">;
beUseful()K>);/#$.2.'$($/ /* Java $)/ -! .H*/'$)$)/ -! .2$/#
.$)"' ./-/( /#*$)*/.0++*-//# 0." +// -)>
*/'$)S>V- '3 ./#/$/>*0)0. .4)/32$/#*/'$)$)/ -! ?$!/#/
$)/ -! $. fun=
funfun interface ComparitizerComparitizer<TT> {
funfun valid(item: TT): BooleanBoolean
}
funfun <TT> firstItemOrNull(items: CollectionCollection<TT>, comparitizer: ComparitizerComparitizer<TT>): TT? {
items.forEach {
ifif (comparitizer.valid(it)) returnreturn it
}
CHANGES IN NEWER KOTLIN VERSIONS
294
returnreturn nullnull
}
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval leetEvent = firstItemOrNull(events, ComparitizerComparitizer { it.id == 1337 })
println(leetEvent)
}
J!-*( N0)/$*)')/ -! .N$)/# '..**&K
-&$)"/# $)/ -! $/. '!2$/#/# fun & 42*-(& .$/E!0)/$*)'$)/ -! F;
*) /#/.0++*-/..4)/3>*2 1 -;!0)/$*)'$)/ -! $(+*. .*) & 4
'$($/=/# - )*)'4 *) abstract !0)/$*)J>&>>;.$)"' ./-/( /#*K>#
fun interface )#1 .()4*)- / !0)/$*)..- )  ;/#*0"#>
Delegated Properties Modifications
*/'$)S>V ) 2! /0- /*+-*+ -/4 ' "/ .=/# $'$/4 ' "/ +-*+ -/4
/*)*/# -+-*+ -/4> 2$'' 3+'*- /#/ '/ -$)/# **&>
Explicit API Mode
*-/#*. *!4*0*).$ -$)"+0'$.#$)"*/'$)'$--4;*/'$)S>V.2#/
 /-$).''.E 3+'$$/(* F>*0)*+/$)/*/#$.!*-'$--4(*0' *-
+-*% />/.$/$*)'*(+$' -# &./*# '+ ).0- /#/4*0-+0'$$.
2#/4*0/#$)&$/$.>0*/$)" /# +-*+*.'/#/' /*/#$.=
*(+$'/$*)$).0#(* $6 -.!-*(/#  !0'/(* $)/# !*''*2$)"
.+ /.=
I *(+$' -- ,0$- .4*0/*.+ $!4 3+'$$/1$.$$'$/4!*- '-/$*)
2# )' 1$)" !0'/1$.$$'$/42*0'- .0'/$) 3+*.$)"/#/
 '-/$*)/*/# +0'$>
I *(+$' -- ,0$- .4*0/*.+ $!4/#  3+'$$//4+ *!+-*+ -/4G
!0)/$*)2# )$/$. 3+*. /*/# +0'$G+0'$.# >
I *(+$' -- ,0$- .4*0/* 3+'$$/'4+-*+"/  3+ -$( )/'.//0.
!*-!0)/$*).2#$#*)/$) 3+ -$( )/'/4+ .$)/# .$")/0- >
I *(+$' -2-).4*02# ) '-/$*) 3+*. /*+0'$* .
)*/#1 *>
CHANGES IN NEWER KOTLIN VERSIONS
295
# *0( )//$*) #.$/$*)' /$'.;.0#.#*2/* )' /#$.(* >
CHANGES IN NEWER KOTLIN VERSIONS
296
Kotlin, WTF?
Custom Accessors
*( /$( .;4*02$''.  get() *-+ -#+. set() ..*$/ 2$/#+-*+ -/4>
#*. - 0./*( ..*-.;- +'$)"/# ./*&*) ./#/*/'$)2*0'" ) -/ !*-
4*0>
Defining Custom Accessors
#*/'$)+-*+ -/4$.( 0+*!7 ';" // -!0)/$*);). // -!0)/$*)>
4 !0'/;/# " // -). // -- * @" ) -/ !*-4*0;0/4*0)*1 --$
/#*. 2$/#0./*($(+' ( )//$*).$! .$- >
*- 3(+' ;# - $.+-*+ -/42$/#0./*(" // -!0)/$*)=
valval stuff = mapOf("something" to "This is the 'something' value")
valval something: StringString?
getget() = stuff["something"]
funfun main() {
println(something)
}
J!-*( N0./*( // -0)/$*).N$)/# '..**&K
# 0./*(" // -"* .*)/# '$) !/ -/# +-*+ -/4 '-/$*)>/$. '- .
get() !0)/$*))) ./*- /0-))*% /(/#$)"/# +-*+ -/4D.//4+ >
/# -2$. ;/#*0"#;/# /0'!0)/$*)$(+' ( )//$*)$.0+/*4*0>)/#$.. ;2
 '- /#//# something 1'0 - ''4*( .!-*(/# stuff Map;-/# -/#)!-*(
/# 7 '/#/*-$)-$'42*0' /# 1'0 *! something>
*/.0-+-$.$)"'4;4*0)*1 --$ /# . // -!0)/$*)/**>#/- ,0$- . var
+-*+ -/4;. val +-*+ -/4#.)*. // -># . // -++ -.!/ -/# +-*+ -/4;
299
'*)".$ /# " // -J$!/# - $.*) K># . // -$. set(value) !0)/$*);2# -
value $./# 1'0 /#/$. $)". />*$)/#$. 3(+' ;2 /0-)-*0))+0//#/
value $)/*/# stuff 0.$)"*0- something & 4=
valval stuff = mutableMapOf<StringString, StringString?>("something" to "This is the 'something' value")
varvar something: StringString?
getget() = stuff["something"]
setset(value) { stuff["something"] = value }
funfun main() {
something = "This is different"
println(something)
}
J!-*( N0./*( // -0)/$*).N$)/# '..**&K
*/ /#/0. -.*!/# +-*+ -/4- )*/6 / 4/# . #)" .>*00. /# .(
.4)/3!*-2*-&$)"2$/#/# +-*+ -/4. !*- >
Referencing the Field
!4*0 '- )*-('.$(+' +-*+ -/4;*/'$)2$''- / /# *-- .+*)$)"7 '
$)/*2#$#/# /" /../*- ># " ) -/ " // -- /0-)./# 1'0 *!/# 7 ';
2#$' /# . // -- +' ./# 1'0 $)/# 7 '>
!4*0 '- 0./*( ..*-.;4 !0'/*/'$)2$'')*/" ) -/ *-- .+*)$)"
7 '?0)' ..4*0#**. /*- ! - ) $/!-*(/# 0./*( ..*-!0)/$*).1$/#
field )( ># );*/'$)- / ./#/7 ') field . -1 ..- ! - ) /*$/>!
4*0- ..*-.- ! - ) field;/#*0"#;4*0-+-*+ -/4) .)$)$/$'$5 -;/*.0++'4
/# $)$/$'1'0 !*- field>
*;/#$.$..$''42#//#  !0'/*/'$)* " ) -/$*)2*0''**&'$& $!4*0
$$/()0''4=
varvar something: StringString = "This is the 'something' value"
getget() = field
setset(value) { field = value }
funfun main() {
something = "This is different"
println(something)
}
J!-*( N0./*( ..*-.)/# $ 'N$)/# '..**&K
CUSTOM ACCESSORS
300
Inline Getters
*- val +-*+ -/4;$!4*0-0./*(" // -' -'4$)$/ ./# //4+ *!/#
+-*+ -/4;4*0).&$++0//$)"/# //4+ *)/# +-*+ -/4$/. '!>)./ ;+0//#
0./*(" // -!0)/$*)$(( $/ '4!/ -/# +-*+ -/4)( =
valval stuff = mapOf("something" to "This is the 'something' value")
valval something getget() = stuff["something"]
funfun main() {
println(something)
}
J!-*( N)'$) 0./*( // -0)/$*).N$)/# '..**&K
Configuration Without Customization
*( /$( .;4*0*)*/) /*!0''40./*($5 /#  ..*-;0/.$(+'4*)7"0- $/
.'$"#/'4>
*- 3(+' ;4*0($"#/#1  var +-*+ -/4$)'..; 0. 4*0) /*..$")
) 21'0 /*$/!/ -$)$/$'$5/$*)>*2 1 -;(*$!4$)"/#/+-*+ -/4.#*0'
'$($/ /*$)./) .*!/# '..$/. '!;2# - .- $)"/#/+-*+ -/4($"#/
1$'' /*'''.. .>)*/# -2*-.;4*02)/. ($@+0'$+-*+ -/4=+0'$
" // -)+-$1/ . // ->
#/$.%0./(// -*!#1$)" private set  '-/$*);2$/#*0/!0)/$*)*4=
classclass WhateverWhatever {
varvar something: StringString = "This is the 'something' value"
privateprivate setset
// TODO
}
)./) .*! Whatever )(*$!4/# something +-*+ -/4;2#$'  1 -4/#$)" '. $.
'$($/ /*- $)"/#/+-*+ -/4>
Anti-Patterns
/$./ (+/$)"/*+0//*)1 -.$*)*-)*-('$5/$*)$)0./*(. // ->*2 1 -;
/#$.( )./#//# " // -2$'')*/- /0-)2#/2.'./. /$)/# . // -=
CUSTOM ACCESSORS
301
varvar something: StringString = "This is the 'something' value"
getget() = field
setset(value) { field = value.toUpperCase() }
funfun main() {
println(something)
something = "This is different"
println(something)
}
J!-*( N0./*( ..*--'' '$.(N$)/# '..**&K
0))$)"/#$."$1 .0.=
This is the 'something' value
THIS IS DIFFERENT
*/*)'4* ./# 1'0 - /0-) 4/# . *) something - ! - ) )*/(/#2#/
2 . /*)$/;0//# $)$/$'$5 -4+.. ./# . // -;+*..$'4- .0'/$)"$)E$)1'$F
/>
'.*; 1 -4- !0'2# )(&$)"0./*( ..*-  3+ ).$1 ;.0#. $)"
.'*2> 1 '*+ -.2$''..0( /#/ ..*-.- # +)(4)*/- '$5 /# *./*!
''$)"4*0-0./*(*) .>
Alternative: Delegates
!4*0 3+ //*- + //# .( .*-/*!0./*(" // -.). // -.;4*0($"#/
*).$ -$(+' ( )/$)"  ' "/ $)./ > ' "/ 2$''''*24*0/* )+.0'/
/# *((*)'*"$ // ->
CUSTOM ACCESSORS
302
Extension Properties
*/*)'4)4*0- /  3/ ).$*)!0)/$*).;0/4*0)- /  3/ ).$*) properties
.2 ''A# - .0'/$)".4)/3- . (' .*($)/$*)*! 3/ ).$*)!0)/$*).)
0./*(+-*+ -/4 ..*-.>
Recapping Extension Functions
.2 3/ ).$*)!0)/$*). -'$ -$)/# **&>*0)0. /# (/*!0)/$*).
/* 3$./$)"'.. .;$)'0$)"'.. ./#/4*0$)*/- / =
privateprivate valval EMAIL_REGEX = RegexRegex("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.]+[a-zA-Z0-9-.]+(\s)*")
funfun StringString.isValidEmail() = EMAIL_REGEXEMAIL_REGEX.matches(thisthis)
funfun main() {
println("[email protected]".isValidEmail())
println("this is not an email address".isValidEmail())
}
J!-*( N3/ ).$*)0)/$*).N$)/# '..**&K
-*( '-/$*)./)+*$)/;-/# -/#).$(+' !0)/$*))( ;/#  '-/$*)
0. . ClassName.functionName();2# - ClassName $./# '../#/4*0-
3/ )$)"2$/#/#  3/ ).$*)!0)/$*)) functionName $./# )( *!/#
!0)/$*)/#/4*0- $)">
.)*/ $)/#/ -'$ -#+/ -;/#*0"#; 3/ ).$*)!0)/$*).))*/+-*+ -/$ .
/*'..>
Recapping Property Custom Accessors
.2 .2$) /# +-  $)"#+/ -;4*0)*1 --$ /# " // -)G*-. // -!*-
303
- "0'-+-*+ -/4=
valval stuff = mapOf("something" to "This is the 'something' value")
valval something: StringString?
getget() = stuff["something"]
funfun main() {
println(something)
}
J!-*( N0./*( // -0)/$*).N$)/# '..**&K
valval stuff = mutableMapOf<StringString, StringString?>("something" to "This is the 'something' value")
varvar something: StringString?
getget() = stuff["something"]
setset(value) { stuff["something"] = value }
funfun main() {
something = "This is different"
println(something)
}
J!-*( N0./*( // -0)/$*).N$)/# '..**&K
# . ''*24*0/*/$'*-/# 0. *!/# +-*+ -/4;/*/# +*$)/2# - 4*0($"#/)*/
/0''40. /# &$)"7 '*!/# +-*+ -/4/''>
Extension Properties = A Mashup
) 3/ ).$*)+-*+ -/4'**&.'$& - "0'-+-*+ -/42$/#0./*( ..*-.; 3 +/
/#/$/$. '- *)) 3$./$)"'..;/# 24) 3/ ).$*)!0)/$*)$.=
valval <T> MapMap<StringString, TT>.something: TT?
getget() = thisthis["something"]
valval stuff = mapOf("something" to "This is the 'something' value")
funfun main() {
println(stuff.something)
}
J!-*( N3/ ).$*)1'-*+ -/4N$)/# '..**&K
#$.$./# .( .$. )-$*./# 0./*(" // - 3(+' !-*(*1 ; 3 +//#/
2 (*1 $//* ) 3/ ).$*)+-*+ -/4*) Map<String, T> !*-.*( " ) -$/4+ T>
!4*0$")*- /# " ) -$.;/# .4)/3$.+- //4(0#/# .( .$/$.!*- 3/ ).$*)
!0)/$*).= ClassName.propertyName /* '- /#//#$.+-*+ -/4$./*  /*
EXTENSION PROPERTIES
304
ClassName>
$/#) 3/ ).$*) val +-*+ -/4;2 #1 /*+-*1$ 0./*(" // -># 1'0 *!/#
+-*+ -/4#./**( !-*(.*( 2# - ;)$/$.0+/*0./* 7) 2#//#/
E.*( 2# - F$.>)/#$.. ;2 %0./*/$)/# 1'0 *!/# "something" )/-4$)/#
Map;$!/# - $.*) >
!4*00. var $)./ *! val;4*02$'') /*+-*1$ */#/# 0./*(" // -)
/# 0./*(. // -=
varvar <T> MutableMapMutableMap<StringString, TT>.something: TT
getget() = thisthis["something"] ?: throwthrow IllegalArgumentExceptionIllegalArgumentException("we do not have something")
setset(value) { thisthis["something"] = value }
funfun main() {
valval stuff = mutableMapOf<StringString, StringString?>("something" to "This is the 'something' value")
stuff.something = "This is different"
println(stuff.something)
}
J!-*( N3/ ).$*)1--*+ -/4N$)/# '..**&K
*0($"#/2*) -2#4/# 0./*(" // -2*-&.> [] .4)/3*) Map - /0-).
)0''' /4+ JT?K;)*0-0./*(" // -) ./*- /0-) T>*;0.$)"/# '1$.
*+ -/*-;2 /#-*2) 3 +/$*)$! [] - /0-). null># - .*)2#4/# *(+$' -
/# )- '$5 ./#/*0-0./*(" // -- /0-). T #./**2$/#2#/ throw 1'0/ .
/*;.2 2$''. '/ -$)/# **&>
3/ ).$*)+-*+ -/$ .;/# - !*- ;- '$//' (*- /#) 3/ ).$*)!0)/$*).;%0./*) .
/#/+-*1$ 0. -.2$/#+-*+ -/4.4)/3$)./ *!!0)/$*).4)/3>
EXTENSION PROPERTIES
305
Escaping Keywords
)*/# -2*-.;2#4* . when()  *( `when`()B
The Scenario: Mockito
*&$/* $.+*+0'-(*&$)"!-( 2*-&!*-1)*/'$)>*0)0. $/$)0)$/
/ ./.J))-*$$)./-0( )//$*)/ ./.K/*- / (*&$(+' ( )//$*).*!
*% /.;2# - 4*0)#1 4*0-/ ./.*)/-*'#*2/#*. *% /.- .+*)/*1-$*0.
!0)/$*)''.>
*- !0)/$*)!*-/#/$. Mockito.when();0. /*/ #*&$/*(*&#*2/*
- .+*)/*+-/$0'-1( /#*'';.0#.$)/#$.1.)$++ /=
MockitoMockito.when(thisThing.isCalledWithThis()).thenReturn("something");
?*-;$!4*00.  static $(+*-/*! when()=
when(thisThing.isCalledWithThat()).thenReturn("something else");
The Problem: Keywords
)1; when $.)*/& 42*->
)*/'$); when is & 42*->);'$& ()4+-*"-(($)"')"0" .;& 42*-.-
- . -1 .4(*'.>*0))*/0. /#*. .4(*'.!*-4*0-*2)!0)/$*))( .;
+-*+ -/$ .;)/# '$& >
1 -4)*2)/# );4*02$'' )*0)/ -1'$--4;'$& *&$/*;/#/0. .*/'$)
& 42*-.!*-( /#*)( .>$) /# '$--4$.'- 4*(+$' ;/# '$--4$/. '!
307
$.7) >)$!/# *''$.$*)*)/# */'$)& 42*-$.+0- '4+-/*!/# $)/ -)'
$(+' ( )//$*)*!/# '$--4; 1 -4/#$)"$.7) >0/;$!/# *''$.$*)$.$)/# +0'$
*!/# '$--4; 1 '*+ -.0.$)"*/'$)2*0' 0)' /*''/#/>
The Solution: Backticks
when() $.- ! - ) /*/# */'$)*)./-0/>
4*)/-./; `when`() J.*; when 2$/#&/$&.-*0)$/K$.)*/># &/$&.
$)$/ /#//#$.$..*( /#$)" '. /#/%0./#++ )./* )(  when>
*;/*0. *&$/*D. when() ( /#*;4*0)/# &/$&./*/# import *!/#/
.//$( /#*=
importimport org.mockito.Mockito.`when`org.mockito.Mockito.`when`
# );4*0)0. &/$&.*)/# ''=
`when`(thisThing.isCalledWithThat()).thenReturn("something else");
!4*07)/#/.4)/3/* 0"'4;)$!/# - $.)*/# -'$& '4)( /*0. !*-/#
!0)/$*);4*0*0''$($//# &/$&./*%0.//# import .// ( )/;+'0.0. as /*
- )( /# $(+*-/>#$.2*0'''*24*0/*1*$/# &/$&.*) 1 -40." *!
/# *''$.$*)>
ESCAPING KEYWORDS
308
Escaped Method Names
funfun `is thisthis syntax really necessary?`() {
println("Well, it has its uses...")
}
`is thisthis syntax really necessary?`()
The Scenario: JUnit Tests
*./*!/# /$( ;!0)/$*))( .- 0. +0- '42$/#$)*0-* >.$*)''4;
/#*0"#;!0)/$*))( ." / 3+*. *0/.$ *!/# * $/. '!>
) .0#. $.2$/#)$/;+-/$0'-'4/# )$/V0. !*-4 -.$))-*$++
 1 '*+( )/>)$/$.+*+0'-0)$// ./$)"#-) ..)!-( 2*-&!*-1)
')"0" .>)$/H)/**'./#/0. $/;'$& .)*)/$)0*0.$)/ "-/$*)
JK. -1 -.H#1 E/ ./-0)) -.F/#/ 3 0/  .$")/ / ./!0)/$*).>4 !0'/;
/# *0/+0/*!/# / ./.$)'0 ./# )( .*!/# / ./'.. .)/# / ./!0)/$*).>
The Problem: You Are Tired of CamelCase
*)1 )/$*).4./#//# / ./!0)/$*))( ..#*0'$)$/ 2#//# / ./!0)/$*)
/0''4/ ./.>.- .0'/;/# 4/ )/* - '/$1 '41 -*. ; 1 )(*- .*/#)1
)*-(''4" /.>/ ./!0)/$*)2$/#X]2*-.#$) /*" /# -$.!-!-*(0)0.0'>
1#../-$/-0' .*)( /#*)( .>#/;*0+' 2$/#./)-1
*)1 )/$*).;( )./#// ./!0)/$*).2$)0+
beingChainedTogetherUsingCamelCase()>
#$.$.,0$&; .4;0/)*/+-/$0'-'4- ' >#/$. .+ $''4/-0 $!)*)@
309
+-*"-(( -.- "*$)"/* - $)"/ ./*0/+0/= $/ ./6;()" -.; />
The Solution: Backticks
*/'$)#.)($)"-0' .!*-!0)/$*)./#/- 1 -4.$($'-/*1D.)($)"-0' .!*-
( /#*.;2$/#*) & 4$6 - ) =$)*/'$);4*0)0. &/$&.-*0)!0)/$*)
)( >$/#$)/# &/$&.;%0./*0/)4/#$)"$.1'$!*-/# !0)/$*))( $/. '!>
*''.0#!0)/$*);4*00. /# .( !0)/$*))( ;$)'0$)"/# &/$&.
*;"*$)"&/*/# .(+' //# *0/. /;/#$.!0)/$*) '-/$*))$)1*/$*)
$.+ -! /'4' "'=
funfun `is thisthis syntax really necessary?`() {
println("Well, it has its uses...")
}
`is thisthis syntax really necessary?`()
*-*-$)-4* ;/#$.2*0'" // $*0.;+-/$0'-'4$!4*0)  /*''/#
!0)/$*)'*/>)/# . *!.*( /#$)"'$& )$/;/#*0"#;4*00.0''4*)*/2-$/
* /#/''./# / ./!0)/$*).H/# / ./-0)) -''./# (1$- 9 /$*)>*/#
!0)/$*) '-/$*)$./# *)'4+' 2# - /# &/$&." /0. >);"$1 )/#
&/$&.;4*0))( /# !0)/$*)2#/ 1 -4*02)/H$)'0$)".+ .)
+0)/0/$*)H/*(& $/ .$ -/*- $)/ ./*0/+0/>
*/ ;/#*0"#;/#/.0++*-/!*-.0#!0)/$*))( .(41-44 )1$-*)( )/>1
))*/#)' .0#!0)/$*))( .;.*)4+' 2# - 1($"#/) /*2*-&
2$/#/# /0')( -0)./# -$.&*! )*0)/ -$)"+-*' (.>*- 3(+' ;$)
)-*$++ 1 '*+( )/; .+ !0)/$*))( .2$/#&/$&.2*-&7) !*-
0)$// ./.0/)*/!*-$)./-0( )/ / ./.>
ESCAPED METHOD NAMES
310
Property Delegates
)*/# -2*-.;' /D.7)*0/(*- *0/#*2/* '54A
A Refresher on lazylazy
&$) /# #+/ -*)+-*+ -/$ .;2 .2 by lazy .4)/3=
classclass FooFoo(valval rawLabel: StringString) {
varvar count = 0
valval label: StringString byby lazy { println("initializing via lazy!"); rawLabel.toUpperCase() }
funfun something() {
count += 1
println("$label: something() was called $count times")
}
}
funfun main() {
valval foo = FooFoo("foo")
foo.something()
foo.something()
foo.something()
println("the final count was ${foo.count}")
}
J!-*( N'54-*+ -/$ .N$)/# '..**&K
 - ; label $.*(+0/ '5$'4>!/# * 2 - =
valval label = rawLabel.toUpperCase()
?/# ) label 2*0' +*+0'/ $(( $/ '4>)./ ; by lazy 2$'' 1'0/ /#
.0++'$ '( 3+- ..$*)2# ) label $.7-./ .. >
311
)/#$.+-/$0'- 3(+' ;/# - $.)*+-/$'$6 - ) ;.$) rawLabel $. val
+-*+ -/4)2$'')*/#)" 0-$)"/# '$! /$( *! label;)2 - '24.
 ..$)" label>*2 1 -; by lazy $.0. !0'$!=
I # $)$/$'$5/$*)($"#/)*/ )  ;)
I # $)$/$'$5/$*)$.$/ 3+ ).$1
What’s Really Going On
by lazy $.- ''4/2*/#$)".= by ) lazy> by . /.0++-*+ -/4 ' "/ ;2#$' lazy
$.+-/$0'- ' "/ 2$/#+-/$0'- #1$*->
byby
*/'$)+-*+ -/4$.( 0+*!7 ';" // -;). // ->- ,0 )/'4;/# " // -
). // -- * @" ) -/ !*-0.>
# - - .$''4/2*24./*- +' /#*. * @" ) -/  ..*-.=
S> 1 --$ /# ( 2$/#0./*($(+' ( )//$*). $- /'4
T>  $- //# (/*+-*+ -/4 ' "/
#  3+- ..$*)/*/# -$"#/*! by ) ./* 1'0/ /*+-*+ -/4 ' "/ >#/
 ' "/ 2$''#1 getValue() ) setValue() !0)/$*).;)/#*. !0)/$*).2$''
- +' /# ./*&" // -). // -;- .+ /$1 '4>*;2# )4*0/-4/*- /#
+-*+ -/41'0 ;/#  ' "/ D. getValue() !0)/$*)$.'' ;)2# )4*0/-4/*
2-$/ /# +-*+ -/41'0 ;/#  ' "/ D. setValue() !0)/$*)$.'' >#/ 1 -
/#*. !0)/$*).*- +- . )/2#//# +-*+ -/4* .>
lazylazy
lazy() $.*/'$)@.0++'$ /*+@' 1 '!0)/$*)/#/- /0-). Lazy +-*+ -/4 ' "/ >
# lazy() !0)/$*)/& .)E$)$/$'$5 -F!0)/$*)/4+ ;0.0''4.0++'$ $)/# !*-(
*!'( 3+- ..$*)>#/" /.+.. '*)"/*/# Lazy +-*+ -/4 ' "/ ># )
getValue() $.'' *)/# Lazy;$!$/#.)*/ 3 0/ /# $)$/$'$5 -4 /;$/* ..*
)0. ./#/./# $)$/$'1'0 /*- /0-)!-*( getValue()>/#*'.*)/*/#$.1'0
$)$/.*2)+-*+ -/4;)$/0. ./#/+-*+ -/4!*-''*/# - getValue() )
setValue() ''.>
PROPERTY DELEGATES
312
- /$)"/# Lazy $/. '!$.# +>)'4$!4*0 ../# +-*+ -/4)/-$"" -/#
getValue() ''*)/# Lazy 2$''2 2$)0+ 3 0/$)"4*0-'( 3+- ..$*))
$)0--$)"$/. 3+ ). .J/$( ;( (*-40." ; />K>
Other Stock Property Delegates
# - - ! 2'.. .$)/# */'$)./)-'$--4; .$ . Lazy;/#/- . /0+/*
. -1 .+-*+ -/4 ' "/ .>
MapMap and MutableMapMutableMap
Map #./# getValue() !0)/$*))  /*. -1 . val +-*+ -/4 ' "/ ;.*2
) ' "/ +-*+ -/4/* Map=
valval stuff = mapOf("something" to 1337, "somethingElse" to "like, whatever")
valval something: IntInt byby stuff
valval somethingElse: StringString byby stuff
println(something)
println(somethingElse)
J!-*( N+.-*+ -/4 ' "/ N$)/# '..**&K
 - ;2 0. by stuff /* ' "/ *0-+-*+ -/$ ./* Map )(  stuff># )2
"*/*- /#*. +-*+ -/$ .;/# Map 2$'''**&0+/# *-- .+*)$)"1'0 .;. *)
/# +-*+ -/4)( .;)- /0-)/# (>
DelegatesDelegates
# Delegates '..*6 -.$/$*)'+-*+ -/4 ' "/ .>
*- 3(+' ; Delegates.observable() ''*2.4*0/*+-*1$ ''&/#/2$''
$)1*& 2# )/# +-*+ -/41'0 #)" .=
importimport kotlin.properties.Delegateskotlin.properties.Delegates
funfun main() {
varvar observed: StringString byby DelegatesDelegates.observable("initial value") { property, oldValue, newValue ->
println("'${property.name}' changed from '$oldValue' to '$newValue'")
}
println(observed)
observed = "new value"
PROPERTY DELEGATES
313
println(observed)
}
J!-*( N ' "/ .>*. -1' JKN$)/# '..**&K
*0-''&-  $1 ./# *')) 2+-*+ -/41'0 ;'*)"2$/#)*% /
- +- . )/$)"/# +-*+ -/4$/. '!>#/$. KProperty *% /;2#$#2 0. # - /*" /
/# +-*+ -/4)( >
# - $.'.* vetoable()=
importimport kotlin.properties.Delegateskotlin.properties.Delegates
funfun main() {
varvar noOddValuesPlease: IntInt byby DelegatesDelegates.vetoable(2) { property, oldValue, newValue ->
newValue % 2 == 0
}
println(noOddValuesPlease)
noOddValuesPlease = 4
println(noOddValuesPlease)
noOddValuesPlease = 3
println(noOddValuesPlease)
}
J!-*( N ' "/ .>1 /*' JKN$)/# '..**&K
#$./$( ;/# '( 3+- ..$*)) ./*- /0-) Boolean>!$/- /0-). true;/# )
/# +-*+ -/41'0 2$'' . //*/# ) 21'0 >!$/- /0-). false;/#*0"#;/# )/#
+-*+ -/41'0 2$'' ' !/'*) ># - !*- ;/# '( 3+- ..$*))E1 /*F
#)" /#/$/* .)*/'$& >)/#$.. ;2 1 /*)4#)" /#/- .0'/.$))*
)0( -;"$1$)"0.=
2
4
4
Delegating Properties to Properties
*/'$)S>V ) 2! /0- =''*2$)"4*0/* ' "/ +-*+ -/4/*)*/# -
+-*+ -/4>
6/# 06;/#/(4.*0).$''4>
*2 1 -;$/$.+-/$0'-'40. !0'!*- '$)"2$/##)" .; .+ $''4$)
.$/0/$*).2# - 4*0))*/ .$'4#)" /# *).0( -.*!/#/;.0#.
PROPERTY DELEGATES
314
'$--4/#/$.- 0. 4'*/.*!+-*% /.>
0++*. ;!*- 3(+' ;/#/4*0 $ /#/4*0-($.@)( +0'$+-*+ -/4)
2)//*.2$/#/*$6 - )/)( > -#+.4*0#1 =
classclass SomethingCoolSomethingCool {
varvar oldPropertyName: StringString = "default"
}
?))*24*02)/=
classclass SomethingCoolSomethingCool {
varvar newPropertyName: StringString = "default"
}
# +-*' ($./#/'' 3$./$)"0. -.*!4*0-- 0.$)"/# *')( >
*0)0. +-*+ -/4 ' "/$*)/*E- 2$- F/# *'/*- ''40. 4*0-) 2
+-*+ -/4=
classclass SomethingCoolSomethingCool {
varvar newPropertyName: StringString = "default"
varvar oldPropertyName byby thisthis::newPropertyName
}
*2;)4/#$)"./$''0.$)" oldPropertyName ./$''2*-&.>
*0)*($) /#$.2$/#/# @Deprecated ))*//$*)/*./ - 1 '*+ -./*2-.
/# ) 2)( =
classclass SomethingCoolSomethingCool {
varvar newPropertyName: StringString = "default"
@Deprecated("Please use 'newPropertyName' instead", ReplaceWithReplaceWith("newPropertyName"))
varvar oldPropertyName byby thisthis::newPropertyName
}
 - ;2 .*+ /# - ! - ) 0.$)" this:: /*- ! -/*+-*+ -/4*)/# -  $1 -
$/. '!> *0'-*0/ $//*.*( */# -*% /;$!)  =
classclass SomethingCoolSomethingCool(valval otherCoolThing: SomethingElseCoolSomethingElseCool) {
varvar oldPropertyName byby otherCoolThing::whatever
}
PROPERTY DELEGATES
315
*2;- ! - ) ./* oldPropertyName *)/# SomethingCool 2$)0+/0''4 $)"
#)' 4/# whatever +-*+ -/4*) SomethingElseCool *% //#/2..0++'$ 
/*/# SomethingCool *)./-0/*->
PROPERTY DELEGATES
316
Class Delegation
)# -$/) $.+*2 -!0'*)./-0/$)*% /@*-$ )/ +-*"-(($)"?.*( /$( .
$/ too +*2 -!0'>*0# -/#  3+- ..$*)E!1*-*(+*.$/$*)*1 -$)# -$/) F'*/;
 0. $)# -$/) $.!$-'4-$"$=
I '..)*)'4 3/ )*) */# -'..
I '.." /. everything /#/$/$)# -$/.J 3'0$)" private ' ( )/.K
# *($)/$*)*!/#*. /2*'$($//$*).( )./#/-!/$)"'..#$ --#4)
 #'' )" ;.*/#/*)'4/# right /#$)".- 1$'' $)/# right '.. .))*/
'. 2# - >
*/'$)*6 -..+$)*)$)# -$/) )*(+*.$/$*)/#/)# '+'' 1$/ .*( *!
/#$.;$)/# !*-(*!'.. ' "/$*)>0./.2 .2 $)/# +- 1$*0.#+/ - /#/4*0
)0. /# by & 42*-/* ' "/ +-*+ -/4;4*0)0. by /* ' "/ /#
$(+' ( )//$*)*!)$)/ -! >
Playing Favorites
0++*. /#/2 #1 .*( *'' /$*)*!$/ (./#/2 2)//*.#*2/# 0. -;
+ -#+.$).*( .*-/*!'$./>/ (.) (-& .E!1*-$/ .F;&$)/*-*2. -
**&(-&.;.*2 ) /*/-&2#$#$/ (.- +- . )/'4/# !1*-$/ .>*2 1 -;!*-
1-$*0.- .*).;/# 24/#/2 0+/ /# !1*-$/ .$.$6 - )//#)#*22 2*-&
2$/#/# $/ (./# (. '1 .H!*- 3(+' ;/# - ($"#/ $6 - )/ . -1$
)+*$)/.!*- '$)"2$/#!1*-$/ .>*2 2)//*& +/# !1*-$/ 0.$) ..'*"$
$/. +-/ !-*(/# $/ (./# (. '1 .>
*0'.4/#/2 #1  FavoriteStore /#// ''.0.2# /# -)$/ ($.(-&
.!1*-$/ )/*/*""' /#/.// !*-)$/ (=
317
interfaceinterface FavoriteStoreFavoriteStore<TT> {
funfun isFavorite(thingy: TT): BooleanBoolean
funfun toggleFavorite(thingy: TT, isFavorite: BooleanBoolean)
}
0-/0' Item '..* .)*//-&$/.!1*-$/ .//0.H2 $)./ 2*0'" //#/
!-*(.*( $)./) *! FavoriteStore=
data classdata class ItemItem(valval id: StringString, valval name: StringString)
 /D.!0-/# -.0++*. /#//#$.$. $)"*) $)))-*$++;*) /#/#.'- 4
*+/ /#  /+& ViewModel .4./ (>*2 #1 .*( .*-/*! ViewModel /#/2$''
#)' $.+'4$)"*'' /$*)*! Item *% /.>*2 1 -;/#/$.+'4)*/*)'4
$)'0 ./# /!-*(/# Item $/. '!;0/'.*2# /# -*-)*//# Item $.!1*-$/ ;
.*2 )#1 )$*) .$")/$)"/#/.//0.>
*( #*2;*).0( -.*!*0-/# *- /$' ItemViewModel 2$'') /*&)*22# /# -
*-)*/+-/$0'- Item $.!1*-$/ *-)*/;)2 2)//#//0' / -($)/$*)
/* #)' 4.*( $)./) *! FavoriteStore>#/($"#/ ( (*-4@& 
$(+' ( )//$*)!*-/ ./.)- '*) H.1$)"/*/. *- . -1$ H!*-
/# - '++>
*0'"*2$/#.*( /#$)"'$& /#$.=
interfaceinterface FavoriteStoreFavoriteStore<TT> {
funfun isFavorite(thingy: TT): BooleanBoolean
funfun toggleFavorite(thingy: TT, isFavorite: BooleanBoolean)
}
classclass InMemoryFavoriteStoreInMemoryFavoriteStore<TT> : FavoriteStoreFavoriteStore<TT> {
privateprivate valval favorites: MutableMapMutableMap<TT, BooleanBoolean> = mutableMapOf()
overrideoverride funfun isFavorite(thingy: TT) = favorites[thingy] ?: falsefalse
overrideoverride funfun toggleFavorite(thingy: TT, isFavorite: BooleanBoolean) {
favorites[thingy] = isFavorite
}
}
data classdata class ItemItem(valval id: StringString, valval name: StringString)
classclass ItemViewModelItemViewModel(valval favorites: FavoriteStoreFavoriteStore<ItemItem>)
CLASS DELEGATION
318
funfun main() {
valval vm = ItemViewModelItemViewModel(InMemoryFavoriteStoreInMemoryFavoriteStore())
valval item = ItemItem("this is my id", "this is my name")
println("item isFavorite: ${vm.favorites.isFavorite(item)}")
vm.favorites.toggleFavorite(item, truetrue)
println("item isFavorite: ${vm.favorites.isFavorite(item)}")
}
 3+*.  favorites +-*+ -/4!-*(/# ItemViewModel /#/$.*0- FavoriteStore>
*).0( -.*!/#/ ItemViewModel /# )2*-&2$/#/# favorites *% /!-*(/# - >
Manual Delegation
#$.2*-&.>*2 1 -;-"0'4;$/- &. )+.0'/$*)># *).0( -*!
ItemViewModel + -#+..#*0')*/#1 $- / ../*/# FavoriteStore>)./ ;
ItemViewModel .#*0'#1 )!*-2*-&$)"2$/#!1*-$/ ./#/#$ ./# !//#/
/# -  1 ) is FavoriteStore>
*;2 *0'. /0+()0' ' "/$*);2# - ItemViewModel !*-2-./#
FavoriteStore *)/* favorites=
classclass ItemViewModelItemViewModel(privateprivate valval favorites: FavoriteStoreFavoriteStore<ItemItem>) : FavoriteStoreFavoriteStore<ItemItem>
{
overrideoverride funfun isFavorite(thingy: ItemItem) = favorites.isFavorite(thingy)
overrideoverride funfun toggleFavorite(thingy: ItemItem, isFavorite: BooleanBoolean) =
favorites.toggleFavorite(thingy, isFavorite)
}
funfun main() {
valval vm = ItemViewModelItemViewModel(InMemoryFavoriteStoreInMemoryFavoriteStore())
valval item = ItemItem("this is my id", "this is my name")
println("item isFavorite: ${vm.isFavorite(item)}")
vm.toggleFavorite(item, truetrue)
println("item isFavorite: ${vm.isFavorite(item)}")
}
#$.'.*2*-&.;)!*-.(''$)/ -! '$& FavoriteStore;$/$.)*/ too / $*0./*
$(+' ( )/># $"" -/# .0-! ;/#*0"#;/# (*- 2*-&$.$)1*'1 $). //$)"
0+/#$.()0' ' "/$*)>
CLASS DELEGATION
319
The Class Delegate Alternative
40.$)"'.. ' "/ ;2 " //#  ./*!*/#2*-'.=/# FavoriteStore - ($).
$)/ -)'/*/# ItemViewModel $(+' ( )//$*);4 /2 " /5 -*@*./
$(+' ( )//$*)*!/# FavoriteStore =
interfaceinterface FavoriteStoreFavoriteStore<TT> {
funfun isFavorite(thingy: TT): BooleanBoolean
funfun toggleFavorite(thingy: TT, isFavorite: BooleanBoolean)
}
classclass InMemoryFavoriteStoreInMemoryFavoriteStore<TT> : FavoriteStoreFavoriteStore<TT> {
privateprivate valval favorites: MutableMapMutableMap<TT, BooleanBoolean> = mutableMapOf()
overrideoverride funfun isFavorite(thingy: TT) = favorites[thingy] ?: falsefalse
overrideoverride funfun toggleFavorite(thingy: TT, isFavorite: BooleanBoolean) {
favorites[thingy] = isFavorite
}
}
data classdata class ItemItem(valval id: StringString, valval name: StringString)
classclass ItemViewModelItemViewModel(favorites: FavoriteStoreFavoriteStore<ItemItem>) : FavoriteStoreFavoriteStore<ItemItem> byby favorites
funfun main() {
valval vm = ItemViewModelItemViewModel(InMemoryFavoriteStoreInMemoryFavoriteStore())
valval item = ItemItem("this is my id", "this is my name")
println("item isFavorite: ${vm.isFavorite(item)}")
vm.toggleFavorite(item, truetrue)
println("item isFavorite: ${vm.isFavorite(item)}")
}
J!-*( N'.. ' "/ .N$)/# '..**&K
# )2 /-40.$)"/# FavoriteStore *) ItemViewModel;*/'$)!*-2-./#*.
''.'*)"/* favorites;2$/#*0/0.#1$)"/**/#/ ' "/$*)()0''4> /;
favorites $.%0./*)./-0/*-+-( / -H$/$.)*/ 1 )+-*+ -/4>
*2;$)/#$.. ;2 *0''.*#1 ItemViewModel $)# -$/!-*(
InMemoryFavoriteStore() )" //# .( - .0'/>*2 1 -;$)- '@2*-'++;2
2$''2)//*.0++'4$6 - )/ FavoriteStore $(+' ( )//$*).;+ -#+./#-*0"#
 + ) )4$)1 -.$*)!-( 2*-&.>*-$)" ItemViewModel /*$)# -$/!-*(
InMemoryFavoriteStore 2*0' '$($)/ /# 9 3$$'$/4/*.2+$)$6 - )/
$(+' ( )//$*)!*-$6 - )/$-0(./) .>
CLASS DELEGATION
320
Constants
val +-*+ -/4$.$((0/' ;0/$/$.)*/*)./)/$)*/'$)>
!/#/.*0).*)!0.$)"?/#/$.)*/.0-+-$.$)">
Declaring a Constant
*)./)/$. '- 4$)"/# const & 42*- !*- val=
constconst valval REQUEST_PERMISSIONS = 1337
)?/#/D.$/>
Why Bother, When We Have valval?
val $.$((0/' ;0//# 1'0 ($"#/)*/  / -($) 0)/$'-0)/$( =
importimport kotlin.random.Randomkotlin.random.Random
valval randomNumber = RandomRandom.nextInt(1, 100)
 - ;2 *)*/&)*22#//# 1'0 *! randomNumber $.0)/$'/#$.+-*+ -/4$.
$)$/'$5 > ))*/#)" /# 1'0 *! randomNumber;0/2 *)*/&)*2/#
1'0 /*(+$' /$( >
4*)/-./; const - / .*(+$' @/$( *)./)/>
*( $/.*!* ) *(+$' @/$( *)./)/.># $"" ./*) $.))*//$*).>)
))*//$*)+-*+ -/4) ./* *(+$' @/$( *)./)/;.$) /# ))*//$*)$/. '!
321
($"#/ ++'$ //# /$( /# * $.*(+$' >
'.*;/# */'$)*(+$' -(4 ' /*+ -!*-($/$*)'*+/$($5/$*).!*-
*(+$' @/$( *)./)/.;.$) /# 1'0 $.&)*2)/*/# *(+$' -;*(+- /*
.$(+' $((0/' val 1'0 .>
*;!*-/#$)"./#/) *)./)/;0.$)" const #..*( 1'0 ;/#*0"#0.0''4$/
$.)*/ .. )/$'>
Constant Type Limitations
*2 1 -;4*0- "*$)"/*7)/#/)*/ 1 -4/#$)",0'$7 ./* *)./)/=
I /#./* +-$($/$1 /4+ J >">; Boolean; Int; Long; FloatK*- String
I /#./*  $/# -/*+@' 1 ' '-/$*)*-  '- $)) object
J$)'0$)" companion objectK
I *0))*/ *1 --$ /# " // -
I *0))*/0. +-*+ -/4 ' "/ .
CONSTANTS
322
Abstract Properties
1 1 '*+ -.2#*(*1 /**/'$)2$'' 0. /* ./-/'.. . )./-/
!0)/$*).>
/$.'.*+*..$' /*#1 ./-/+-*+ -/$ .;*./#/(4. (>
But, But, But… Why?
)./-/!0)/$*) '- .!0)/$*).$")/0- /#/.0'.. .(0./$(+' ( )/
J*-/# (. '1 . ./-/K>
$($'-'4;)./-/+-*+ -/4 '- .*) *-/2*!0)/$*).$")/0- ./#/
.0'.. .(0./$(+' ( )/=
I val  '- ." // -
I var  '- ." // -). // -
# !//#//#$.#++ )./* +-*+ -/4(// -.(*- /*/# '' -;./# 4/- /
/#$..+ -! /'4)*-('+-*+ -/4>-*(/# ./)+*$)/*!/# .0'.. .;
*1 --$$)"/# +-*+ -/4$.- ''4/# - /*+-*1$ $(+' ( )//$*).!*-/# " // -
J);2# - - ' 1)/;/# . // -K>
Abstract valval
(+' ( )/$)")./-/ val +-*+ -/4$.)*/.$")$7)/'4$6 - )//#)
$(+' ( )/$)"- "0'- val +-*+ -/4;*/# -/#)) $)"/# override & 42*-.
4*02*0'2$/#$(+' ( )/$)")./-/!0)/$*)=
323
abstractabstract classclass BaseBase {
abstractabstract valval something: StringString
}
classclass SomethingSourceSomethingSource : BaseBase() {
overrideoverride valval something: StringString = "like, whatever"
}
funfun main() {
println(SomethingSourceSomethingSource().something)
}
J!-*( N./-/1'-*+ -/$ .N$)/# '..**&K
 - ; Base  7) .)./-/+-*+ -/4;) SomethingSource 3/ ). Base>.
- .0'/; SomethingSource ) ./*+-*1$ *)- / $(+' ( )//$*)*!/# ./-/
+-*+ -/4>)/#$.. ;.$) $/$. val;2 %0./0. ./-$)"'$/ -'>
#$.$. ,0$1' )//*$(+' ( )/$)" 0./*(" // -=
abstractabstract classclass BaseBase {
abstractabstract valval something: StringString
}
classclass SomethingSourceSomethingSource : BaseBase() {
overrideoverride valval something: StringString
getget() = "like, whatever"
}
funfun main() {
println(SomethingSourceSomethingSource().something)
}
*2 1 -;/# get() .4)/3+-*1$ .(*- 9 3$$'$/4>*- 3(+' ;.0++*. /#/2
2)/ /*0. -)*()0( -$)./ *!./-$)"'$/ -'>!2 ..$")-)*(
)0( -/* something;/#/$./# *) @)@*)'4 something 1'0 =
importimport kotlin.random.Randomkotlin.random.Random
abstractabstract classclass BaseBase {
abstractabstract valval something: IntInt
}
classclass SomethingSourceSomethingSource : BaseBase() {
overrideoverride valval something = RandomRandom.nextInt(1,100)
}
ABSTRACT PROPERTIES
324
funfun main() {
valval source = SomethingSourceSomethingSource()
println(source.something)
println(source.something)
println(source.something)
}
!;*)/# */# -#);2 $(+' ( )/ get();.$) get() $.'' *) 1 -4 ..*!
/# +-*+ -/4;2 " /!- .#-)*()0( - 1 -4/$( /#/2 - ! -/* something=
importimport kotlin.random.Randomkotlin.random.Random
abstractabstract classclass BaseBase {
abstractabstract valval something: IntInt
}
classclass SomethingSourceSomethingSource : BaseBase() {
overrideoverride valval something
getget() = RandomRandom.nextInt(1,100)
}
funfun main() {
valval source = SomethingSourceSomethingSource()
println(source.something)
println(source.something)
println(source.something)
}
J!-*( N./-/1'0./*( // -N$)/# '..**&K
Abstract varvar
$($'-'4;4*0)#1 )./-/ var;0//# )/# .0'..) ./*.0++'4
" // -). // -=
abstractabstract classclass BaseBase {
abstractabstract varvar something: IntInt
}
classclass SomethingSourceSomethingSource : BaseBase() {
overrideoverride varvar something: IntInt = 0
getget() = field
setset(value) { field = value}
ABSTRACT PROPERTIES
325
}
funfun main() {
valval source = SomethingSourceSomethingSource()
println(source.something)
source.something = 1337
println(source.something)
}
J!-*( N./-/1-N$)/# '..**&K
ABSTRACT PROPERTIES
326
Covariance in Generics
 ) -$.- +*2 -!0'>4+ $)# -$/) $.+*2 -!0'>*( /$( .;+*2 -!0'/#$)".*
)*/" /'*)"1 -42 ''?)/#/) /# . 2$/#" ) -$.)$)# -$/) >
)/#$.#+/ -) /# ) 3/;2 2$'' 3+'*- 2#4.*( /$( .*/'$)*(+'$).*0/
4*0-" ) -$.;)#*2/2*.$(+' & 42*-.H in ) out H)# '+.*'1 /#
+-*' (>
You Can’t Put That in That!
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval animals: ListList<AnimalAnimal> = listOf<FrogFrog>(FrogFrog(), FrogFrog())
println(animals)
}
 - ;2 #1 /2*.0@/4+ .*! Animal= Frog ) Axoltl> - /  List<Animal>
1-$' )(  animals )/-4$)$/$'$5$)"$/2$/# List<Frog>>-*".- )$('.;
) Frog $.) Animal;.*2 2*0' 3+ //#$./*2*-&?)$/* .>
*2 1 -;.$(+'4.2$/#$)"!-*( List /* MutableList 0. .+-*' (.=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
327
classclass AxolotlAxolotl : AnimalAnimal()
funfun main() {
valval animals: MutableListMutableList<AnimalAnimal> = mutableListOf<FrogFrog>(FrogFrog(), FrogFrog())
println(animals)
}
!4*0/-4/#$.;4*02$''" / Type mismatch: inferred type is Animal but Frog
was expected .*(+$'  --*->
!4*0'**&/ /# */'$)*0( )//$*)!*- MutableList;4*02$''. /#/$/$.
 '- .=
interfaceinterface MutableListMutableList<EE> : ListList<EE>, MutableCollectionMutableCollection<EE>
MutableList 3/ )./# List ) MutableCollection $)/ -! .;0.$)"" ) -$
/4+ E>
# */'$)*0( )//$*)!*- List;/#*0"#;$.'$//' $/./-)" =
interfaceinterface ListList<outout EE> : CollectionCollection<EE>
$. outB
outout Is a Direction
out $)$/ ./#/*0-*($))/0. *!/# " ) -$/4+ $./*- /0-)1'0 .*!/#/
/4+ >);..0#;2  '$ 1 /#/$/$..! !*-0./*.0++*-/.0@/4+ .*!/#//4+ >
 ( ( -/#/$)*/'$)J)()4./-*)"'4@/4+ *% /@*-$ )/ +-*"-(($)"
')"0" .K1-$' *-+-*+ -/4/4+ ) *)'4 *(+/$' 2$/#$/.1'0 D.
/0'/4+ >% /.#1 /4+ H1-$' *-+-*+ -/4D./4+ .4.E2 - 0.$)"/#$.
*% / as if it its type were XF;- "-' ..*!/# /0'/4+ *!/#/*% />
*;.0++*. /#/ MutableList ''*2 .0/4+ .)/#$..// ( )/.0  =
valval animals: MutableListMutableList<AnimalAnimal> = mutableListOf<FrogFrog>(FrogFrog(), FrogFrog())
animals .4.E#*''$./*! Animal *% /.F;)$/''*2.4*0/*..$"))4 Animal
/*)4$) 3>*;!-*(*(+$'/$*)./)+*$)/;/#$.2*0'2*-&=
COVARIANCE IN GENERICS
328
animals[0] = AxolotlAxolotl()
!/ -''; Axolotl $.) Animal>
*2 1 -;2#$' animals .4.E#*''$./*! Animal *% /.F;/# /0' object /#/
animals +*$)/./*.4.E#*''$./*! Frog *% /.F> ))*/+0/) Axolotl $)
List<Frog>;).* animals[0] = Axolotl() 2*0'0. *) *!/2*+-*' (.=
S> /2*0'!$'$(( $/ '4;*)/# "-*0)./#/4*0))*/+0/) Axolotl $)
List<Frog>
T> /2*0'!$'2# )2 '/ -/-4- /-$ 1$)"/#/ ' ( )/; 0. */'$)2*0'
/-4/- /$)") Axolotl .$!$/2 -  Frog;)/#/2*0'- .0'/$)
ClassCastException *-.$($'-+-*' (
*; MutableList $. invariant $)$/." ) -$/4+ > MutableList<Frog> ) ./*
. *)'$./*! Frog *% /.;)$!2 #1  MutableList<Animal> 1-$' *-
+-*+ -/4;/# /0'0) -'4$)"'$./) ./*.0++*-/)4/4+ *! Animal;)*/%0./*)
.0@/4+ >
0/ List * .)*/.0++*-/(0//$*)>!4*0- 1$ 2/# !0)/$*).*) List; $/# -=
I # 4- /0-)/# " ) -$/4+ ;*-
I # 4 +//# " ) -$/4+ 0/*)'4!*-*(+-$.*)+0-+*. .J >">;
contains() /* / -($) $!/# '$./*)/$). -/$) ' ( )/K
)*/# -2*-.;!*-/# *+ -/$*)./#/ List + -!*-(.;$/* .)*/(// -$!/#
0) -'4$)"'$./$.*!.0@/4+ *-)*/>''/# List *+ -/$*)../$''2*-&>
#/$.2#4 List )0. out /*.4/#/$/$. covariant $)/# " ) -$/4+ >
List<Animal> 1-$' *-+-*+ -/4)+*$)//* List<Frog> 2$/#*0/$..0 ; 0.
/)*+*$)/2$''2  !*- /*/-4/*+0/) Axolotl $)/*/#/ List>
Declaration-Site and Use-Site Variance
.$)" <out T> $)/4+  '-/$*);. List * .;$.'' E '-/$*)@.$/
1-$) F># '/ -)/$1 $.E0. @.$/ 1-$) FH0.$)" <out T> !*-!0)/$*)
+-( / ->
*- 3(+' ;' /D./-4/*'*) .*( !-*".=
COVARIANCE IN GENERICS
329
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun <TT> clone(input: MutableListMutableList<TT>, output: MutableListMutableList<TT>) {
input.forEachIndexed { index, item -> output[index] = item}
}
funfun main() {
valval input = mutableListOf(FrogFrog(), FrogFrog())
valval result = mutableListOf<AnimalAnimal>(AxolotlAxolotl(), AxolotlAxolotl())
clone(input, result)
println(result)
}
clone() /& .$)/2*'$./.;)$/..$")./# 1'0 .$)/# *0/+0/'$.//* /# .(
./# $)+0/'$./;*)+ -@$) 3.$.>#$.!0)/$*).#*0' + -!*-($)".*( /
1'$/$*); ).0-$)"/#//# *0/+0/'$./$./# .( ' )"/#*-'*)" -/#)/# $)+0/
'$./;0/2 - .&$++$)"/#//*& +/#  3(+' * .#*-/ ->
*2 1 -;/#$.* * .)*/*(+$' =
Type mismatch: inferred type is Animal but Frog was expected
# *(+'$)/$.*0//# input +-( / -./* clone()> - +..$)"$)/*
clone()=
I List<Frog>;./#/$.2#/ mutableListOf(Frog(), Frog()) - /0-).
I List<Animal>;./#/$.2#/2 .+ $7  result /*
*2 1 -; clone() $. 3+ /$)"*/#+-( / -./* *!/4+ T;0/2 - +..$)"$)
/2*$6 - )/J0/- '/ K/4+ .= Frog ) Animal>
*"$''4;2#/2 2)/ clone() /**$.7) > Frog $.) Animal;.*2 )/& 
0)#*! Frog *% /.)..$")/# (/* Animal .'*/.$)'$./>
$)" out /*/# 0. .$/ $)$/ ./#/2 ) +//# input MutableList /*
*!/4+ T *-)4.0@/4+ =
COVARIANCE IN GENERICS
330
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal()
classclass AxolotlAxolotl : AnimalAnimal()
funfun <TT> clone(input: MutableListMutableList<outout TT>, output: MutableListMutableList<TT>) {
input.forEachIndexed { index, item -> output[index] = item}
}
funfun main() {
valval input = mutableListOf(FrogFrog(), FrogFrog())
valval result = mutableListOf<AnimalAnimal>(AxolotlAxolotl(), AxolotlAxolotl())
clone(input, result)
println(result)
}
#/./$.7 ./# *(+$' -)(& .'*"$'. ). =2 )+0/)*% /*!)4
Animal .0@/4+ $)/*) Animal .'*/$)'$./>
COVARIANCE IN GENERICS
331
Contravariance in Generics
*)/$)0$)"*0- 3($)/$*)*!/4+ $)# -$/) )/# $-$(+/.*)" ) -$.?
.*( /$( .;2 '$& /*.*-/)$('.>
) *+/$*)$./*0. Comparable )#1 Animal &)*2#*2/**(+- $/. '!/*
*/# - Animal *% /.=
openopen classclass AnimalAnimal : ComparableComparable<AnimalAnimal> {
overrideoverride funfun compareTo(other: AnimalAnimal) = toString().compareTo(other.toString())
}
classclass FrogFrog : AnimalAnimal() {
overrideoverride funfun toString() = "Frog!"
}
classclass AxolotlAxolotl : AnimalAnimal() {
overrideoverride funfun toString() = "Axolotl?"
}
funfun main() {
println(listOf(FrogFrog(), AxolotlAxolotl()).sorted())
}
compareTo() 2*-&..$/* .$)1=$/) ./*- /0-)=
I ) "/$1 )0( -$!/# -  $1 -.*-/. !*- /# */# -1'0
I +*.$/$1 )0( -$!/# */# -1'0 .*-/. !*- /# -  $1 -
I  -*$!/# /2*1'0 .-  ,0'
#$.2*-&.2 '';.*-/$)"*0- Axolotl  !*- *0- Frog=
[Axolotl?, Frog!]
333
);.$) *0-'$./*)/$). Frog )) Axolotl;*/'$)$."*$)"/*/*7)/#
*((*).0+ -/4+ H Animal H)/- /*0-'$./. List<Animal>>
*0''.*0. Comparator;2#$##. compare() !0)/$*)/#/2*-&.'$&
compareTo()>*2 1 -; Comparator $../)'*) $)/ -! ;&)*2$)"#*2/*
*(+- /2**% /.*!.*( " ) -$/4+ =
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
overrideoverride funfun toString() = "Frog!"
}
classclass AxolotlAxolotl : AnimalAnimal() {
overrideoverride funfun toString() = "Axolotl?"
}
classclass FrogComparatorFrogComparator : ComparatorComparator<FrogFrog> {
overrideoverride funfun compare(one: FrogFrog, two: FrogFrog) =
one.toString().compareTo(two.toString())
}
funfun main() {
println(listOf(FrogFrog(), FrogFrog()).sortedWith(FrogComparatorFrogComparator()))
}
#$.2*-&.; 0. 2 - .*-/$)" List<Frog> ) FrogComparator &)*2.#*2/*
*(+- !-*".>0/;.0++*. 2 +..$)/# .( !-*".?. List<Animal>=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
overrideoverride funfun toString() = "Frog!"
}
classclass AxolotlAxolotl : AnimalAnimal() {
overrideoverride funfun toString() = "Axolotl?"
}
classclass FrogComparatorFrogComparator : ComparatorComparator<FrogFrog> {
overrideoverride funfun compare(one: FrogFrog, two: FrogFrog) =
one.toString().compareTo(two.toString())
}
CONTRAVARIANCE IN GENERICS
334
funfun main() {
println(listOf<AnimalAnimal>(FrogFrog(), FrogFrog()).sortedWith(FrogComparatorFrogComparator()))
}
*2;2 #1 *(+$'  --*-; 0.  1 )/#*0"#/# 0) -'4$)"*% /.- *!
/4+ Frog;2 - /- /$)"/# (. Animal;) FrogComparator * .)*/&)*2#*2
/**(+- Animal *% /.>
*;2#4)2 .*-/ List<Animal> 0.$)" Comparable 0/)*/ ComparatorB# 4-
 '- .'$"#/'4$6 - )/'4;%0./. List ) MutableList 2 - $) /# +-  $)"
#+/ ->
Comparator 2*-&..$(+'4*)" ) -$/4+ T=
funfun interface ComparatorComparator<TT>
?2#$' Comparable 0. .) 2& 42*-; in=
interfaceinterface ComparableComparable<inin TT>
in $)$/ ./#/*0-+-$(-40. *!/# //4+ $./* +/$/.$)+0/;$)24.
2# - 2 )2*-&2$/#.0@/4+ .!*-/#/$)+0/>)*/# -2*-.;
Comparable<Animal> )*(+-  Frog *-) Axolotl;./#*. - .0@/4+ .*!
Animal>
.$)"/# +'$)" ) -$/4+ ;. Comparator * .;( )./#/ Compartor $. invariant
2$/#- .+ //*/# /4+ >*)1 -. '4;/# 0. *! in 4 Comparable ( )./#/
Comparable $. contravariant 2$/#- .+ //*/# /4+ >
Declaration-Site and Use-Site Variance
.2$/# out; in ) 0. $) '-/$*)J. Comparable * .K*-/0. .$/
J >">;!0)/$*)+-( / -K>
*- 3(+' ;.0++*. /#/2 2)//*- +' !-*"2$/#)3*'*/'$)'$./=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
overrideoverride funfun toString() = "Frog!"
}
CONTRAVARIANCE IN GENERICS
335
classclass AxolotlAxolotl : AnimalAnimal() {
overrideoverride funfun toString() = "Axolotl?"
}
funfun replaceWithAnimal(list: MutableListMutableList<AnimalAnimal>, value: AnimalAnimal) {
forfor (i inin list.indices) {
list[i] = value
}
}
funfun main() {
valval animals: MutableListMutableList<AnyAny> = mutableListOf(FrogFrog(), FrogFrog())
valval replacement = AxolotlAxolotl()
replaceWithAnimal(animals, replacement)
println(animals)
}
animals $. MutableList<Any>>*; 1 )/#*0"#$/+*$)/./*)*% //#/*)/$).
*)'4!-*".;2 - /- /$)"$/./#*0"#$/$.'$.//#/*)/$).)4+*..$' /4+ >
#$.$.+-*' (2# )2 '' replaceWithAnimal();.$/$. 3+ /$)"
MutableList<Animal>;)*/ MutableList<Any>>
# 0. *! in #)" ./#/=
openopen classclass AnimalAnimal
classclass FrogFrog : AnimalAnimal() {
overrideoverride funfun toString() = "Frog!"
}
classclass AxolotlAxolotl : AnimalAnimal() {
overrideoverride funfun toString() = "Axolotl?"
}
funfun replaceWithAnimal(list: MutableListMutableList<inin AnimalAnimal>, value: AnimalAnimal) {
forfor (i inin list.indices) {
list[i] = value
}
}
funfun main() {
valval animals: MutableListMutableList<AnyAny> = mutableListOf(FrogFrog(), FrogFrog())
valval replacement = AxolotlAxolotl()
replaceWithAnimal(animals, replacement)
CONTRAVARIANCE IN GENERICS
336
println(animals)
}
/0. .$/ ; in .4.E2  +//#$./4+ *-)4.0+ -/4+ F>*; MutableList<Any>
./$.7 . MutableList<in Animal>>);'*"$''4;/#$.(& .. ). =2 -
// (+/$)"/*- +' ) ' ( )/*!/# '$./2$/#) Animal;)/#/2*-&.2# /# -
/# '$./$.'$./*! Animal *-'$./*! Any>
*/ ;/#*0"#;/#/2 - '.*- '4$)"*)*/'$)D./4+ $)! - ) >
mutableListOf(Frog(), Frog());4 !0'/;2*0'- /0-) MutableList<Frog>>
0/2 - )*/.//$)"/#/ 3+'$$/'4>*/'$). ./#/2 - 0.$)"$//*$)$/$'$5 
1-$' *!/4+ MutableList<Any>;.*$/ $ ./#/2 - ''42)/ /# '$.//*
/0''4 be MutableList<Any>>!2 !*- $//*  MutableList<Frog>=
valval animals: MutableListMutableList<AnyAny> = mutableListOf<FrogFrog>(FrogFrog(), FrogFrog())
?/# )2 #1  *1-$) +-*' (> MutableList $.$)1-$)/$)$/./4+ ;.*2
))*/..$") MutableList<Frog> /* MutableList<Any>>
);$!2 /0-)-*0)).4/#/ animals $..0++*. /*  MutableList<Frog>=
valval animals: MutableListMutableList<FrogFrog> = mutableListOf(FrogFrog(), FrogFrog())
?/# )2 #1 )*/# -*1-$) +-*' (> replaceWithAnimal() 2$'' +/
MutableList *! Animal *-.0+ -/4+ ;0/)*/.0@/4+ > ) ;$/2$'')*/ +/
MutableList<Frog>;2#$#(& .. ). ;.2 ))*/..$")) Animal /*.'*/$)
'$.//#/*)'4 +/. Frog>
CONTRAVARIANCE IN GENERICS
337
Anonymous Functions
#/#++ ).2# )/# *(+$' -$.)*/.(-/ )*0"#/*0) -./)4*0-'(
3+- ..$*)B
The Problem: Return Types
'( 3+- ..$*)''*2.0./*.+ $!4/# /4+ .!*-)4+-( / -.;4$)"/#
/4+ .$)/# +-( / -'$./=
valval squarifier = { x: IntInt -> x * x }
println(squarifier.invoke(3))
J!-*( N(3+- ..$*).)-( / -.N$)/# '..**&K
 - ;2 .+ $7''4 '- /#/ x $.*!/4+ Int>
*2 1 -;2 *)*/.+ $!42#//# - /0-)/4+ $.> )D/H/# - .$(+'4$.)*
.4)/3!*-$/>)./ ;/# */'$)*(+$' -2$''$)! -/# - /0-)/4+ ;. *)2#/$/
&)*2.*0/2#/2 - - /0-)$)">
*( /$( .;/#*0"#;*/'$)2$'')*/ ' /*$)! -2#/2 2)/>*- 3(+' ;$!2
-*++ /# /4+ !-*(/# *1  3(+' =
funfun main() {
valval squarifier = { x -> x * x }
println(squarifier.invoke(3))
}
?2 " /*(+$'  --*-= Cannot infer a type for this parameter. Please
339
specify it explicitly.
*2 1 -;/# - (4 . .2# - /# *(+$' -$.#++42$/#/# +-( / -/4+ .
0/))*/$)! -/# - /0-)/4+ ;*-$/$)! -./# 2-*)"- /0-)/4+ >
The Solution: Anonymous Function Syntax
'( 3+- ..$*)$.E!0)/$*)'$/ -'F># */# -.4)/3!*-!0)/$*)'$/ -'.$.
/# )*)4(*0.!0)/$*)=
valval squarifier = funfun(x: IntInt): IntInt { returnreturn x * x }
println(squarifier.invoke(3))
J!-*( N)*)4(*0.0)/$*).N$)/# '..**&K
 - ; squarifier #./# .( '"*-$/#(. !*- ;%0./2-$// )$)/# !*-(*!)
)*)4(*0.!0)/$*)> - ;2 have /*.// *0-- /0-)/4+  3+'$$/'4;%0./.2 *
!*-- "0'-!0)/$*)>
4)//$''4;))*)4(*0.!0)/$*)$.!0)/$*) '-/$*)2$/#*0//# !0)/$*)
)( > ..$")/# )*)4(*0.!0)/$*)/*+-*+ -/4;+..$/.+-( / -; />;
%0./.2 2*0'*!*-'( 3+- ..$*)>)2  3 0/ /# !0)/$*)/# .(
24.2 2*0'*!*-'( 3+- ..$*);.0#.''$)" invoke() *)$/>
Constraints and Effects
( 3+- ..$*).))*)4(*0.!0)/$*).) 0. $)/ -#)" '4!*-/#
(*./+-/># - - /2*)*/' $6 - ) .;/#*0"#>
Where You Can Supply Anonymous Functions
!!0)/$*)/& .!0)/$*)/4+ ./# './+-( / -;2 )+..'(
3+- ..$*)*0/.$ *!/# +-( / -'$./+- )/# . .=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval leetEvent = events.first { it.id == 1337 }
ANONYMOUS FUNCTIONS
340
println(leetEvent)
}
J!-*( N7-./JKN$)/# '..**&K
 - ; first() /& .!0)/$*)/4+ ;)2 +-*1$ '( 3+- ..$*)*0/.$ *!
/# first() ''+-( / -'$./>$) 2 #1 )**/# -+-( / -.*) first();2
) 1 )-*+/# +- )/# . .!-*(/# first() ''>
'*. /#*.  ) 7/.2$/#))*)4(*0.!0)/$*)> #1 /*+-*1$ /#*.
$).$ /# +- )/# . .;/# 24/#/2 2*0'!*-)4*/# -/4+ *!+-( / -=
data classdata class EventEvent(valval id: IntInt)
funfun main() {
valval events = listOf(EventEvent(1), EventEvent(5), EventEvent(1337), EventEvent(24601), EventEvent(42), EventEvent(-6))
valval leetEvent = events.first(funfun(it: EventEvent): BooleanBoolean { returnreturn it.id == 1337 })
println(leetEvent)
}
J!-*( N)*)4(*0.0)/$*)..-( / -.N$)/# '..**&K
Where returnreturn Returns
return ) ' '  *-2$/#*0/' '> return 2$/#*0/' ''24.- /0-).
!-*(2#/ 1 -!0)/$*)2 - $)>
))*)4(*0.!0)/$*)$.!0)/$*);0/'( 3+- ..$*)$.)*/> ) ;/#
 #1$*-*! return $.$6 - )/ /2 )/# /2**)./-0/.> return $).$ *!)
)*)4(*0.!0)/$*)- /0-).!-*(/# )*)4(*0.!0)/$*)$/. '!> return $).$ *!
'( 3+- ..$*)- /0-).!-*(2#/ 1 -!0)/$*) )'*. .$/>
ANONYMOUS FUNCTIONS
341
Local Functions
)#1 !0)/$*).$)'.. .).$($'-*)./-0/.;.0#.$)/ -! .>
)#1 /*+@' 1 '!0)/$*).;*0/.$ *!)4'..>
);.$//0-).*0/;2 )#1 '*'!0)/$*).;2# - *) !0)/$*)- .$ .$).$
*!)*/# -!0)/$*)>
I Heard You Like Functions…
#0)&*!/# */'$)./)-'$--4$.2-$// ).+0- !0)/$*).=/*+@' 1 '
!0)/$*)./#/- )*/+-/*!)4'..>*-/# . !0)/$*).;/# *)'4$)+0/J$ ''4K
*( ..+-( / -.>
1 )(*- $.2-$// )$)/# !*-(*! 3/ ).$*)!0)/$*).*) 3$./$)"'.. .>
3/ ).$*)!0)/$*). look '$& 2#//# $-)( .0"" ./.=/# 4 3/ )) 3$./$)"
'..>*2 1 -;!-*(+-/$'./)+*$)/; 3/ ).$*)!0)/$*).- ''4(*0)//*
E.4)//$.0"-F*)/*+@' 1 '!0)/$*).>) 3/ ).$*)!0)/$*)#.)*$'$/4/**
)4/#$)"/#/) ,0$1' )//*+@' 1 '!0)/$*)))*/H/#  3/ ).$*)!0)/$*)
.$(+'4" /./*0. this !*-*) *!/# +-( / -.>*;/#$.=
funfun StringString.withLength() = "$this ${this.length}"
?$. ,0$1' )//*=
funfun stringWithLength(s: StringString) = "$s ${s.length}"
0- !0)/$*).) 3/ ).$*)!0)/$*).#1 )*.// >''$)+0/$.+-*1$ 1$
+-( / -.;2# - /#  3/ ).$*)!0)/$*)%0./#.*)1 )$ )/.4)/3!*-.0++'4$)"
343
*) .+ $'+-( / -JthisK>
*2 1 -;.$) /# . .*-/.*!!0)/$*).#1 )*.// ;.0@$1$$)"/# ($)/*.('' -
!0)/$*).) +-*' (.*( /$( .>*0($"#/#1 - ''4*(+' 3!0)/$*)
2# - +$  *!'*"$*0"#//* +0'' *0/$)/*. +-/ !0)/$*);0/*/#/#
*-$"$)')/# . +-/ !0)/$*).#- /**()41'0 .>
*0(4'.*#1 . .2# - 4*02)//*+0'''*"$*0/$)/*. +-/ !0)/$*);
0//#/. +-/ !0)/$*)#.)*+0-+*.  3 +/2# )'' $)/# *)/ 3/*!$/.
*-$"$)'!0)/$*)>
…So I Put a Function in Your Function
*# '+- ../#*. .*-/.*!*) -).;*/'$).0++*-/.'*'!0)/$*).=
@Composable
privateprivate funfun DrawerContent(
navController: NavControllerNavController,
scaffoldState: ScaffoldStateScaffoldState
) {
funfun navTo(route: StringString) {
navController.navigate(route) {
popUpTo = navController.graph.startDestination
launchSingleTop = truetrue
}
scaffoldState.drawerState.close()
}
ColumnColumn {
DRAWER_ITEMSDRAWER_ITEMS.forEach {
DrawerRowDrawerRow(it) { navTo(it.route) }
}
}
}
#$.$.$/*!* 0.$)")'+#1 -.$*)*! /+&*(+*. ;) 3/@" ) -/$*)
/**'&$/!*-)-*$>$/#*0/" //$)"$)/*''*!/#  /$'.;2 #1 
DrawerContent() !0)/$*)/#/+*+0'/ .)1$"/$*)-2 -/#/.'$ .*0/!-*(
*)  " *!/# .- )> 2)//*#1 '$./*!-*2.$)/# -2 -- +- . )/$)"
+' .4*0))1$"/ /*$)/# ++;2# - DrawerRow() - ) -.-*2>-/*!/#
'*"$*! DrawerRow() $./*- .+*)/*'$& 1 )/.*)-*24''$)"/# .0++'$ 
'( 3+- ..$*)># - ;2 '' navTo() /*)1$"/ /*/#  .$- +*-/$*)*!/#
LOCAL FUNCTIONS
344
++> navTo() $.$(+' ( )/ .'*'!0)/$*)H$/.$(+' ( )//$*)++ -.
$).$ *! DrawerContent()>
navTo() *0' + -!0)/$*)*! DrawerContent()>*2 1 -;.$//0-).*0/;/#
*)'4+' /#/ navTo() $.)  $.# - $) DrawerContent()> DrawerContent() $.
/*+@' 1 '!0)/$*)<#1$)" navTo() also  /*+@' 1 '!0)/$*)'0// -.0+/# /*+@
' 1 ')( .+ > *0'%0./#1 /# *4*! navTo() "*$- /'4$)/# '(
3+- ..$*)+-*1$ /* DrawerRow()?0/2 ($"#/#1 */# -)1$"/$*)
*+/$*). .$ .-*2.;.0#..*( 0//*).//# /*+*!/# -2 -!*-+*+0'-
 ./$)/$*).>.$)"'*' navTo() !0)/$*)''*2./#/'*"$/* - 0. .)  
2$/#$) DrawerContent();0/ only 2$/#$) DrawerContent();2# - $/$.)  >
Funception
# - $.)*+-/$''$($/$)#*2 +4*0-'*'!0)/$*).)) ./>!0)/$*))
#1 '*'!0)/$*);2#$##.$/.*2)'*'!0)/$*);).**)=
funfun up() {
funfun you() {
funfun give() {
funfun gonna() {
funfun never() {
}
}
}
}
}
*2 1 -;.*+$)"-0' .++'4> up() ))*/'' never();. never() $. 7) '*'
/* gonna() H*)'4 gonna() )'' never()>
LOCAL FUNCTIONS
345
Local Types
*/*)'4)2 ) ./!0)/$*).$).$ *!!0)/$*).;0/2 )'.*- / '*'
'.. .;$)/ -! .;)*/# -/4+ ..2 ''A
Scenario: Perils of PairPair Programming
*( /$( .;2 ) .*( .*-/*!/./-0/0- /*- +- . )/)$)/ -$(- .0'/>
)-*$ 1 '*+ -.0.$)"31$)*/'$)-0)$)/*/#$.. )-$*'*/;+-/$0'-'4
2$/#*+ -/*-.'$& zip()> zip() .4.E*/#$.'$./*!*+ -/$*).$)+-'' ';/# )
*($) /# $-- .0'/.)"$1 /#//*( F> zip() )*/*)'4/& ./# *+ -/$*).J >">;
+$-*! Single *% /.K;0/'.*.*( '( 3+- ..$*)*-*/# -!0)/$*)/4+
/#/*($) ./# - .0'/.*! # Single $)/*.$)"' *% /- .0'/=
SingleSingle.zip(userAuthCall, serverStatusCall) { userAuthResult, serverStatusResult ->
TODOTODO("ummmm... combine these... somehow...")
}
# +) .4.*'0/$*)$./*0.  Pair;2#$#2-+.)-$/--4+$-*!*% /.>
# /*+@' 1 ' to() $)'$) !0)/$*) )- /  Pair=
SingleSingle.zip(userAuthCall, serverStatusCall) { userAuthResult, serverStatusResult ->
userAuthResult to serverStatusResult
}
*( /#$)"'/ -/#/.0.-$ ./*" //# - .0'/.2$''" //# Pair=
SingleSingle.zip(userAuthCall, serverStatusCall) { userAuthResult, serverStatusResult ->
userAuthResult to serverStatusResult
}
347
.subscribe { pairOfResults ->
// TODO something cool
}
*2 1 -; Pair %0./#. first ) second +-*+ -/$ ./*" //# 2-++ 1'0 .>
first ) second - 7) +-*+ -/4)( .;0//# 4- 1 -4" ) -')*)*/#1
)4- '( )$)"$)*0-* >
) *+/$*)$./*0.   ./-0/0-$)" '-/$*) /*- &/# Pair &+-/$)/*
/2*1-$' .=
SingleSingle.zip(userAuthCall, serverStatusCall) { userAuthResult, serverStatusResult ->
userAuthResult to serverStatusResult
}
.subscribe { (userAuthResult, serverStatusResult) ->
// TODO something cool
}
)*/# -*+/$*)2*0' 0./*( data class $)./ *! Pair;.*4*0)0.
+-*+ -/4)( ./#/#1 "- / -( )$)"=
data classdata class OpResultsOpResults(valval userAuth: UserAuthUserAuth, valval serverStatus: ServerStatusServerStatus)
SingleSingle.zip(userAuthCall, serverStatusCall) { userAuthResult, serverStatusResult ->
OpResultsOpResults(userAuth = userAuthResult, serverStatus = serverStatusResult)
}
.subscribe { results ->
// TODO something cool with results.userAuth and results.serverStatus
}
# ,0 ./$*)/# ) *( .=2# - * ./#/ data class - .$ B!$/#.)*1'0
*0/.$ /# *0)-$ .*!*) !0)/$*);* .$/(& . ). !*-/#$./* /*+@' 1 '
'..*-) ./ '..B
$/#*/'$);4*0#1 )*/# -).2 -= '- /# data class $).$ /# !0)/$*)
/#/) .$/=
funfun loadStuff() {
data classdata class OpResultsOpResults(valval userAuth: UserAuthUserAuth, valval serverStatus: ServerStatusServerStatus)
SingleSingle.zip(userAuthCall, serverStatusCall) { userAuthResult, serverStatusResult ->
OpResultsOpResults(userAuth = userAuthResult, serverStatus = serverStatusResult)
}
.subscribe { results ->
LOCAL TYPES
348
// TODO something cool with results.userAuth and results.serverStatus
}
}
#$.$.$/(*- 1 -*. /#) Pair>*2 1 -;$ ''4;$!4*00. /# Pair;4*0
2*0'#1 *(( )/.$)/# *  3+'$)$)"2#/ first ) second - >!4*0-
"*$)"/**/# - 3+'$)$)"$/1$*(( )/.?+ -#+.4*0*0' 3+'$)$/1$*
$)./ >
LOCAL TYPES
349
Inline Functions
*( /$( .;4*0($"#/7)!0)/$*) '- 0.$)"/# inline & 42*-=
inlineinline funfun max(x: IntInt, y: IntInt): IntInt = ifif (x > y) x elseelse y
funfun main() {
valval bigger = max(3, 7)
println("The larger of 3 and 7 is $bigger")
}
J!-*( N)'$) 0)/$*).N$)/# '..**&K
#/$. inlineB#/E'$) F- 2 E$)FB
Macro History
)4+-*"-(($)"')"0" .H)]](*)"/# (H.0++*-/+- +-* ..*-
(-*.=
#define max(X, Y) ((X) > (Y) ? (X) : (Y))
$1 )/# (-* 7)$/$*);4*0)0. $/'$& - "0'-!0)/$*)''=
foo = max(bar, goo);
./# )( .0"" ./.;/# E+- +-* ..*-F/& ./# -2.*0- * )+-* .. .$/
 !*- +..$)"$//*/# /0'*(+$' ->)/#$.. ;/# +- +-* ..*- 3+)./#
(-*$)+' ;- +'$)"/# *1 '$) 2$/#=
foo = ((bar) > (goo) ? (bar) : (goo));
351
.- .0'/;2 #1 .4)/3/#/'**&.'$& !0)/$*)'';0/2 1*$.*(
*1 -# $)1*'1 2$/#!0)/$*)''1 -.0.%0./*$)"/# 2*-&$- /'4>
!2 0. /# (-*. 1 -'/$( .;/#*0"#;/# - $.*./$)/ -(.*!++.$5 >
#1 /# E 3+) F(-** $). 1 -'+' .;-/# -/#)$/ $)"%0./
$(+' ( )/ $).$)"' !0)/$*)*4>
Inline Functions: Like Macros
) inline !0)/$*)$)*/'$)2*-&.'$& +- +-* ..*-(-*># *(+$' -/& .''
*0-- ) .*!''./*/#/ inline !0)/$*))- +' ./# (2$/#/# /0'
!0)/$*)*4;(0#'$& #*2/# +- +-* ..*-- +' .''*0-- ) .*!/#
(-*2$/#/# (-* 7)$/$*)>
What We Write
''2 *$. inline /*/# !0)/$*) '-/$*)=
inlineinline funfun max(x: IntInt, y: IntInt): IntInt = ifif (x > y) x elseelse y
funfun main() {
valval bigger = max(3, 7)
println("The larger of 3 and 7 is $bigger")
}
J!-*( N)'$) 0)/$*).N$)/# '..**&K
What the Compiler Compiles
# *(+$' -* .)*/" ) -/ )/0'!0)/$*); .+$/ *0- '-/$*)>/# -;
#+' /#/2 ''/# !0)/$*);*/'$)2$''*(+$' /# * !-*(/# !0)/$*)
*4>/2$'' .$!2 2-*/ =
funfun main() {
valval bigger = ifif (3 > 7) 3 elseelse 7
println("The larger of 3 and 7 is $bigger")
}
"$).*( + -!*-() 41*$$)")/0'!0)/$*)''>0/;$!2 '' max()
$)()4+' .;2 2$)0+2$/#()4*+$ .*!/# max() * $)/# *(+$' 
INLINE FUNCTIONS
352
++;)*0-++2$'' $"" ->
Inline Properties
!4*0$(+' ( )/+-*+ -/42$/# 0./*( ..*-. /#/*)*/- ! - ) /#
&$)"7 ';/#*.  ..*-.) (-& . inline=
valval stuff = mutableMapOf<StringString, StringString?>("something" to "This is the 'something'
value")
varvar something: StringString?
inlineinline getget() = stuff["something"]
inlineinline setset(value) { stuff["something"] = value }
funfun main() {
something = "This is different"
println(something)
}
);$!4*02)/*/# ..*-./* $)'$) H..#*2)*1 H4*0)(-&/#
2#*' +-*+ -/4. inline=
valval stuff = mutableMapOf<StringString, StringString?>("something" to "This is the 'something' value")
inlineinline varvar something: StringString?
getget() = stuff["something"]
setset(value) { stuff["something"] = value }
funfun main() {
something = "This is different"
println(something)
}
J!-*( N)'$) -*+ -/$ .N$)/# '..**&K
INLINE FUNCTIONS
353
Inline Classes
$/#*/'$)S>U; inline $.)D/%0./!*-!0)/$*).)4(*- A
What?
) /# +-  $)"#+/ -;2 .2 inline ++'$ /*!0)/$*).>#$..4.E$)./ *!
(&$)"/# /0'!0)/$*)'';+0//# * !*-/# !0)/$*)C$)'$) D2# - /# ''
$. $)"( F>
*/'$)S>U .0++*-/!*- inline ++'$ /*'.. .=
inlineinline classclass InvoiceLineItemKeyInvoiceLineItemKey(valval key: StringString)
) inline class $.*)'4''*2 /*#1 *) +-*+ -/4;)$/(0./++ -$)/#
*)./-0/*-># 4- ''*2 /*#1 !0)/$*).)+-*+ -/$ . 2$/#0./*(
 ..*-. J0/)*&$)"7 '.K>
# )/# * $.*(+$' H..0($)"/#//# - - )**(+$'  --*-.H''
*0-- ) .*!/#$. inline class - - +' 4- ! - ) ./*/# *) @)@*)'4
+-*+ -/4*!/#/'..;)''- ! - ) ./*!0)/$*)''.- - +' 2$/#$)'$)
- +- . )//$*).*!/#*. !0)/$*).>
Why?
) & 40. . !*-/#$.2$'' 2$/#*% /@- '/$*)'(++$)" )"$) .J.K
).$($'-.4./ (.;!*-# '+$)"/*1*$E./-$)"'4@/4+ F+-*+ -/$ .>
0++*. ;!*- 3(+' ;/#/2 #1 /. /#/#.$)1*$ .;2# - $)1*$ .#1
. -$ .*!'$) $/ (.- +- . )/$)"/# /#$)"./*++ -*)/# $)1*$ >)*/'$);2
355
($"#/#1 Invoice ) InvoiceLineItem '.. ./*(+/*/# /' .$)*0-
/. >)/#*. '.. .($"#/#1 +-*+ -/$ ./#/'$) 0+2$/#/# *'0().$)
/# /' ?0.$)"//4+ .. *)/# *'0()/4+ .=
data classdata class InvoiceInvoice(
valval key: StringString,
valval createdOn: InstantInstant,
valval customerKey: StringString,
// TODO other properties
)
data classdata class InvoiceLineItemInvoiceLineItem(
valval key: StringString,
valval quantity: IntInt,
valval productKey: StringString,
// TODO other properties
)
# +-*' (# - $./#/''*!*0-& 4.- String +-*+ -/$ .=
I /# & 4*) Invoice
I /# & 4*) InvoiceLineItem
I /# & 4/**0-0./*( -;+ -#+.$) Customer '..)/'
I /# & 4/**0-+-*0/;+ -#+.$) Product '..)/'
-*(*/'$)D../)+*$)/; String $. String $. String> . 1 '*+ -.&)*2
/#/2 ))*/0. ) InvoiceLineItem & 4/*'**&0++-*0/?0/*/'$)* .
)*/&)*2/#/;.*$/))*/ )!*- .0#- ./-$/$*)>#$..*-/*!E./-$)"'4@/4+ F
. /*!+-*+ -/$ .' ./*0".;2# - 2 $ )/''40. *) '..D& 42# - 2
( )/)*/# -'..D& 4;)*/'$)' /.$/#++ ) 0. /# 4- ''./-$)".>
$/# inline class;2 )2-+/#*. ./-$)".$)'.. ./#/- $./$)/=
data classdata class InvoiceInvoice(
valval key: InvoiceKeyInvoiceKey,
valval createdOn: InstantInstant,
valval customerKey: CustomerKeyCustomerKey,
// TODO other properties
)
data classdata class InvoiceLineItemInvoiceLineItem(
valval key: InvoiceLineItemKeyInvoiceLineItemKey,
valval quantity: IntInt,
INLINE CLASSES
356
valval productKey: ProductKeyProductKey,
// TODO other properties
)
#*!/#*. ...Key '.. .$.) inline class &$)/*/# *) .#*2) -'$ -$)/#$.
#+/ -=
inlineinline classclass InvoiceLineItemKeyInvoiceLineItemKey(valval key: StringString)
/*(+$' /$( ;*/'$)/- /.''!*0-*!/#*. ...Key '.. .. $)"$./$)/)
)*/ ,0$1' )/> ))*/$ )/''40.  ProductKey 2# - 2 ) )
InvoiceKey;!*- 3(+' >#$. '$($)/ .'..*!0"./#-*0"#./-*)"/4+
# &$)"$)/# *(+$'/$*)+-* ..>
*2 1 -; 0. /# 4- inline class;/ runtime;/# 4- %0././-$)".> *
)*/$)0-*1 -# *!- /$)"$)./) .*!''*!/# . ...Key '.. .>*;2 "$)
/# +*2 -*!/4+ # &$)"2$/#*0/+4$)"+-$ 2# )*0-++-0).>
Alpha!
*/ ;/#*0"#;/#/ inline class $.)'+#! /0- //# +- . )//$( >*0) /*
)' $/ .+-/*!4*0-0$'*+/$*). !*- 0.$)"$/>)/# - (4 0".;.
2$/#)4'+#@"- .*!/2- >
INLINE CLASSES
357
Reified Type Parameters
E $!4F$.2*-$))"'$.#;( )$)"=
/**).$ -*-- +- . )/.*( /#$)"./-/.(/ -$'*-*)- / /#$)"<
/*"$1  7)$/ *)/ )/)!*-(/**) +/*-$ 
J!-*( /#  --$(@ ./ -$/$*)-4K
-*(+-*"-(($)"./)+*$)/; /#  -'4)0-4TRTS $/$*)*!$&$+ $#.=
4( ).*!- $7/$*);.*( /#$)"/#/2.+- 1$*0.'4$(+'$$/;
0) 3+- .. ;)+*..$'4$) 3+- ..$' $. 3+'$$/'4!*-(0'/ )(
1$'' /**) +/0'J'*"$'*-*(+0//$*)'K()$+0'/$*)>)!*-(''4;
- $7/$*)$.*!/ )- ! -- /*.E(&$)".*( /#$)"7-./@'..$/$5 )F
2$/#$)/# .*+ *!+-/$0'-.4./ (>
 .+$/ /# 9*2 -4 .-$+/$*).;/# refied & 42*-$)*/'$)#.1 -4.+ $7
( )$)"=& +" ) -$/4+ -*0)!*-0. /-0)/$( >
Type Erasure (Other Than Via the Backspace Key)
.2/# 0. *!" ) -$/4+ . -'$ -$)/# **&>) & 4.+ /*!" ) -$/4+ .
$./#//# 4- +0- '4.*( /#$)"0. /*(+$' /$( ># */'$)*(+$' -0. .
" ) -$/4+ ./*# '+ ).0- /# 1'$$/4*!*0-* ;4 ''$)"/0.$!2 1$*'/ .*(
*)/-/>*2 1 -;/# compiled code #.)*&)*2' " *!/#*. " ) -$/4+ .>
*- 3(+' ;*'' /$*).'$& List #1  filterIsInstance() *+ -/*->#$.- /0-).
)*/# -*'' /$*)2#*. /4+ .(/#+-/$0'-/4+ > *0'/-4/* 7) *0-
*2).$($'-!0)/$*); filterByType()=
359
funfun <TT> List<*>.filterByType() = thisthis.filter { it isis TT }
funfun main() {
println(listOf(1, "this", 3, "is", "a", 1337, "mess").filterByType<StringString>())
}
 - ; filterByType() $. '- .) 3/ ).$*)!0)/$*)*! List<*>;2# - /#
2$'-( ).E2 - )*/*) -) *0//# /4+ *!*% /.$)/#$. ListF>#
!0)/$*) '- ." ) -$/4+ T;)/# $(+' ( )//$*)*!/# !0)/$*)0. ./#
- "0'- filter() *+ -/*-2$/#'(/#/- /0-). true *)'4!*-/#*. *% /.
/#/- *!/4+ T>
)/# *-4;/#$..#*0'2*-&>
)+-/$ H.0#.$!4*0/-4/#$.$)/# '..**&.-/#+H$/!$'.2$/#
*(+$'  --*-=
Cannot check for instance of erased type: T
# is # &$.-0)/$( # &H2 *)*/&)*2/*(+$' /$( 2#/$.$)/#
List *0-/ .4*!/# 2$'->0//#/$(+'$ ./#//-0)/$( 2 &)*22#//4+ T
$.;)4 !0'/;2 *)*/; 0. /4+ ." /E -. F4/# *(+$'/$*)+-* ..>
.0''4;*0-// (+/./*0. " ) -$/4+ /-0)/$( " / / / 4/# *(+$' -;
)2 " /) --*-( .." '$& /# *) .#*2)*1 >
Reified = Retained Type for Inline Functions
*-.('' -!0)/$*).;/# 2*-&-*0)$./*(& /# !0)/$*)$/. '! inline 2#$'
'.*$)"/# reified & 42*-/*/# " ) -$/4+ =
inlineinline funfun <reifiedreified TT> Iterable<*>.filterByType() = thisthis.filter { it isis TT }
funfun main() {
valval raw = listOf(1, "this", 3, "is", "a", 1337, "mess")
valval filtered = raw.filterByType<StringString>()
println(filtered)
}
J!-*( N $7 4+ .N$)/# '..**&K
#$./ ''./# *(+$' -/**/2*/#$)".=
REIFIED TYPE PARAMETERS
360
S> .2$/#'' inline !0)/$*).;/# /0'$(+' ( )//$*).#*0' +' 
$)/* #''.$/ ;.*2#$' $/ looks '$& 2 - ''$)"!0)/$*);$)- '$/4
2 - $- /'4 3 0/$)"*+4*!/# !0)/$*)*4
T> )/#/*+4*!/# !0)/$*)*4;*)1 -/ T $)/*/# /0'/4+ 0. 4/#/
+-/$0'-''
)/#$.. ;2 &)*2/#/2 2)/ T /* String; 0. /#/$.#*22 - ''$)"
filterByType();0.$)" filterByType[String]()> reified .4./#/2 /& /# T $)
/# $)'$) !0)/$*))- +' $/4/# /0'/4+ >*;$/$..$!2 #2-$// )/#
* .=
funfun main() {
valval raw = listOf(1, "this", 3, "is", "a", 1337, "mess")
valval filtered = raw.filter { it isis StringString }
println(filtered)
}
# filterByType() $(+' ( )//$*)$.E$)'$) F2# - 2 (& /# '';)/#
*)- / /4+ !*- T $.'.*$)'$) >
#$.$.*) *!/#*. & 42*-./#/4*02$''/ )/**)'40. 2# )/# *(+$' -4 ''.
/4*0;*- maybe 4*0 )*0)/ -.*( -0)/$(  --*->)4/$( 4*0" /) --*-
- '/ /*/4+  -.0- ;/#/.0"" ././#/4*0.#*0' 0.$)" inline ) reified>
inline $.- ''4 .$") !*-.(''!0)/$*).;2# - /# *1 -# *!#1$)"0)#
*!*+$ .*!/# !0)/$*)*4$.)*//**>!4*0" //4+  -.0-  --*-.*)
'-" -!0)/$*);4*02$''2)//*/-4/*$.*'/ /# $//#/#./# /0'/4+  -.0-
+-*' (;- !/*-/#/$)/*. +-/ !0)/$*);/# ) inline /#/!0)/$*))0.
reified /*' -0+/# /4+  -.0- $..0 >
REIFIED TYPE PARAMETERS
361
noinlinenoinline and crossinlinecrossinline
*./'$& '4; noinline ) crossinline - & 42*-./#/4*02$''/*4*0-*
2# )/# */'$)*(+$' -/ ''.4*0/*/# (/*4*0-* 424*!) --*-
( .." >
*-()4 1 '*+ -.;.$(+'4*$)"2#//# *(+$' -/ ''./# (/**$..08$ )/>
*2 1 -;$). 4*0- ''42)//*&)*2 why /# *(+$' -$.*(+'$)$)"?- *)A
noinlinenoinline: Keep the Lambda as an Object
# )2 (-&!0)/$*). inline;/# * ..*$/ 2$/#/#/!0)/$*)$.0. 
.- +' ( )/!*-)4''./*/#/!0)/$*);.*2 1*$/# *1 -# *!/#
!0)/$*)''>*./*!/# /$( ;2 !*0.*)E/# * ..*$/ 2$/#/#/!0)/$*)F
. $)"/# !0)/$*)*4>*2 1 -;$/ also ( )./# * ..*$/ 2$/#)4
'( 3+- ..$*).+.. .!0)/$*)/4+ +-( / -./*/# !0)/$*)>
& /#$.* ;!*- 3(+' =
inlineinline funfun beUseful(whenDone: () -> UnitUnit) {
// TODO something useful
whenDone()
}
funfun main() {
beUseful {
println("hello, world!")
}
}
 0. beUseful() $.(-& . inline;)*/*)'4$./# body *! beUseful() $)'$) 
363
//# ''.$/ ;0/.*$./# '( 3+- ..$*)/#/2 +... whenDone()>
*( /$( .;/#$.$.7) H''2 2)//**2$/#/# !0)/$*)/4+ $. invoke() $/;.
2 - *$)"# - >*2 1 -;$)*/# -. .;2 2$''/-4/*/- //# !0)/$*)/4+ .
)*% /;./*-$)"$/.*( 2# - *-+..$)"$//*.*( */# -!0)/$*)=
funfun somethingElse(thingToDo: () -> UnitUnit) {
thingToDo()
}
inlineinline funfun beUseful(whenDone: () -> UnitUnit) {
// TODO something useful
somethingElse(whenDone)
}
funfun main() {
beUseful {
println("hello, world!")
}
}
 - ;2 " /=
Illegal usage of inline-parameter 'whenDone' in 'public inline fun beUseful(whenDone:
() -> Unit): Unit
defined in root package in file klassbook.kt'. Add 'noinline' modifier to the
parameter declaration
*;2 )!*''*2/# $- /$*).*!/# *(+$' -) noinline /*/#/+-( / -=
funfun somethingElse(thingToDo: () -> UnitUnit) {
thingToDo()
}
inlineinline funfun beUseful(noinlinenoinline whenDone: () -> UnitUnit) {
// TODO something useful
somethingElse(whenDone)
}
funfun main() {
beUseful {
println("hello, world!")
}
}
NOINLINE AND CROSSINLINE
364
#$./ ''.*/'$)/*)*//- //#/+-( / -. inline;.*$/$.' /* /- / )
)*% /) +.. -*0)>
crossinlinecrossinline: Allowing returnreturn
*0($"#/#1 !0)&4* /#//-$ .*$)" return !-*($).$ *!'(
3+- ..$*)=
funfun beUseful(whenDone: () -> UnitUnit) {
// TODO something useful
whenDone()
}
funfun main() {
beUseful {
println("hello, world!")
returnreturn
}
println("...and we're done!")
}
#$.!$'./**(+$' =
'return' is not allowed here
*0))*/ return !-*(/# ($' *!.*( '( 3+- ..$*)>
*2 1 -;$!4*0(-&/# !0)/$*). inline=
inlineinline funfun beUseful(whenDone: () -> UnitUnit) {
// TODO something useful
whenDone()
}
funfun main() {
beUseful {
println("hello, world!")
returnreturn
}
println("...and we're done!")
}
NOINLINE AND CROSSINLINE
365
# * *(+$' .?0/ ...and we're done! * .)*/" /+-$)/ >6 /$1 '4;2#/
/# inline & 42*-* .$./0-)/#/* $)/*=
funfun main() {
println("hello, world!")
returnreturn
println("...and we're done!")
}
.- .0'/;2 return !-*( main()  !*- 2 - #/# . *) println() ''>
!4*02)//*'*&/#$..*-/*! return !-*( $)"0. ; crossinline /*/#
!0)/$*)/4+ +-( / - '-/$*)=
inlineinline funfun beUseful(crossinlinecrossinline whenDone: () -> UnitUnit) {
// TODO something useful
whenDone()
}
funfun main() {
beUseful {
println("hello, world!")
returnreturn
}
println("...and we're done!")
}
*2;2 - &/*/# *-$"$)' 'return' is not allowed here *(+$' - --*->
NOINLINE AND CROSSINLINE
366
Receivers in Function Types
-'$ -$)/# **&;2 .2/# apply() .*+ !0)/$*)=
classclass SomethingOrAnotherSomethingOrAnother {
varvar someProperty = 123
}
valval foo = SomethingOrAnotherSomethingOrAnother().apply {
someProperty = 456
}
)/# '( 3+- ..$*)/#/2 +../* apply();/# 1'0 *! this $.2#/ 1 -2
'' apply() 0+*)> with() 2*-&..$($'-'4; 3 +//#/ this $.2#/ 1 -2 +...
/# +-( / -/* with()=
classclass SomethingOrAnotherSomethingOrAnother {
varvar someProperty = 123
}
funfun main() {
valval foo = SomethingOrAnotherSomethingOrAnother()
with(foo) {
someProperty = 456
}
}
*0($"#//#$)&/#//#$.$..*( .*-/*!*(+$' -("$>). ). ;+ -#+.$/$.>
*2 1 -;*/# apply() ) with() - !0)/$*).$(+' ( )/ $)*/'$)$/. '!;)
4*0)0. /#$.++-*#/*/$'*- this $).*( *)/ 3/*!4*0-*2)>
367
What with()with() Looks Like
with() $.1 -4.$(+' inline !0)/$*)=
publicpublic inlineinline funfun <TT, RR> with(receiver: TT, block: TT.() -> RR): RR {
returnreturn receiver.block()
}
//& ./2*+-( / -.; receiver ) block> receiver $.%0./)*-$)-4
+-( / -;' $/*)  '- 0.$)"" ) -$/4+ T> block $.2# - /# !0)'$ .>
with() 0. ./2*" ) -$/4+ .= T ) R> T $./# /4+ !*-/# receiver +-( / -;)
R $.2#/ with() - /0-).>
block $. '- . T.() -> R> () -> R $. '-/$*)*! !0)/$*)/4+ > T.() ->
R $. '-/$*)*!) extension !0)/$*)/4+ ;2# - 2 ) /*..*$/ )4''
*!/#/!0)/$*)/4+ 2$/#.*( /#$)"*!/4+ T>
with() $)1*& . block 1$ receiver.block()># *($)/$*)*! T.() -> R .4)/3
) receiver.block() .4./#/ this;$)/# *)/ 3/*!/# $)1*& block;$.
receiver>
!2 "*&/*/# with() 3(+' !-*( -'$ -$)/#$.#+/ -=
classclass SomethingOrAnotherSomethingOrAnother {
varvar someProperty = 123
}
funfun main() {
valval foo = SomethingOrAnotherSomethingOrAnother()
with(foo) {
someProperty = 456
}
}
receiver $)/#$.. $.*0- SomethingOrAnother $)./) JfooK> block $.*0-'(
3+- .$*)J{ someProperty = 456 }K>$1 )#*2 with() $.$(+' ( )/ ; this $)
/#/'( 3+- ..$*)$./# SomethingOrAnother $)./) ;2#$#$.2#42 )
()$+0'/ someProperty 2$/#*0/.+ $!4$)"/#/*% /$- /'4>
RECEIVERS IN FUNCTION TYPES
368
What apply()apply() Looks Like
apply() $..$($'-;/#*0"#$/$.2-$// ).) 3/ ).$*)!0)/$*)*) T $/. '!=
publicpublic inlineinline funfun <TT> TT.apply(block: TT.() -> UnitUnit): TT {
block()
returnreturn thisthis
}
$) apply() $.) 3/ ).$*)!0)/$*)*! T;$/)%0./'' block() $- /'4> this $.
'- 4/# *-- /-  $1 -=/# .( *% //#/ apply() 2.'' 0+*)>
# */# -(%*-$6 - ) $./#/ with() - /0-)./# - .0'/.*! receiver.block();
2#$' apply() - /0-). this J/# -  $1 -K>
Use Case: DSL
*/'$)$.2 ''@.0$/ !*--!/$)"*($)@.+ $7')"0" .J.K>*0)0. /#
E 3/ ).$*)!0)/$*)*)-  $1 -F/-$&/**6 -.(''./*.$(+'$!4/# 0. *!
.*( - 0.' $/*!* >
*- 3(+' ;)-*$ 1 '*+ -.- ./-/$)"/*0. /+&*(+*. ;)$/.
*(+*. .0. /;!*- 7)$)"0. -$)/ -! .>*(+*. 0. .'$//' .$)
. 1 -'+' ./*# '+- 0 /# (*0)/*!* )  !*-.$(+' . )-$*.>
#$.* .)$++ /. /.0+1 -/$''4@.-*''$)"'$./=
LazyColumnLazyColumn {
items(content.forecasts) {
WeatherRowWeatherRow(it)
}
}
# items() ''$).$ *! LazyColumn() +-*1$ ./# '$./*!(* '*% /./#/
- +- . )//# /!*-/# '$./H$)/#$.. ;.'$./*!2 /# -!*- ./.!*-
+-/$0'-'*/$*)># '( 3+- ..$*)+.. /* items() " /.'' .)  
/*- ) - #$/ ($)/# '$./;. *)/# )0( -*!$/ (.;$!/# 0. -.-*''.; />
# /-$'$)"'(+-( / -*) LazyColumn() $. 7) .=
content: LazyListScopeLazyListScope.() -> UnitUnit
RECEIVERS IN FUNCTION TYPES
369
LazyColumn();.+-/*!$/.$(+' ( )//$*);- / . LazyListScope *% /)
0. ./#/!*-$)1*&$)"/# content +-( / -Jscope.apply(content)K>
LazyListScope  7) ./# !*-/# =/# !0)/$*)./#/2$'' 1$'' /*
/#/ content '(=
interfaceinterface LazyListScopeLazyListScope {
funfun item(key: AnyAny? = nullnull, content: @Composable LazyItemScopeLazyItemScope.() -> UnitUnit)
funfun items(
count: IntInt,
key: ((index: IntInt) -> AnyAny)? = nullnull,
itemContent: @Composable LazyItemScopeLazyItemScope.(index: IntInt) -> UnitUnit
)
}
LazyColumn();$)/0-);2$''0. /# items() .0++'$ 4/#/'(.+-/*!
- ) -$)"/# '$./>
#$.$.!$-'4*((*)+// -)$)*(+*. =!0)/$*)/#/4*0''/& .
/-$'$)"'(;2$/#/# -  $1 -. //* .*( /#$)"0. !0'!*-/#//-$'$)"'(
/* 7) 2#/$/$..0++*. /**>)/# . *!!0)/$*).'$& LazyColumn();
LazyRow(); ConstraintLayout();) NavHost();/# -  $1 -$.. //*.*( *% /
/#/$(+' ( )/.)$)/ -! /#/- +- . )/./# ;.*2 )*)$. '4/ ''
*(+*. 2#/$/) ./**>
RECEIVERS IN FUNCTION TYPES
370
Renamed Imports
#/#++ ).2# )4*0'*1 /2*'.. ..*(0#/#/4*0))*/ -/* +-/
!-*( $/# -*!/# (?0//# 4.#- )( B
The Scenario: ObservableObservable
#1 ! 2'.. .)$)/ -! .)(  Observable $))-*$++
 1 '*+( )/=
I java.util.Observable
I android.database.Observable
I android.databinding.Observable
I io.reactivex.Observable J!*-/#*. 0.$)"31TK
I -#+.*) .!-*(*/# -'$--$ .
J)/#$.* .)*/ 1 )*0)/*/'$)D.*2) observable();2#$#/' ./$.''
'*2 -. K
)(*./. .;2 *)'4) *) *!/# . $))4"$1 )*/'$)'..>
371
The Problem: Import Name Collisions
0/2#/$!2 ) /2*B
Figure 7: Distracted Kotlin Class
)1;/# .*'0/$*)$.'0)&4=*)'4*) )#1 ) import .// ( )/;)/# */# -
#./* !0''4@,0'$7 >*;4*0#1 /*+$&!1*-$/ Observable /#/" /./#
.#*-/)( ;2$/#- ! - ) ./*/# */# -*) '0// -$)"0+/# * >
The Solution: import ... as ...import ... as ...
*/'$)#..$(+' .*'0/$*)!*-/#$.=$(+*-/'$. .>&$)/*#*22 0. as !*- /4+
'$. .;2 )0. as /*..$"))'$./*) import=
importimport android.databinding.Observableandroid.databinding.Observable
importimport io.reactivex.Observableio.reactivex.Observable asas RxObservableRxObservable
*2;2 )0. RxObservable $)./ *! io.reactivex.Observable>
#$.) $/*)!0.$)";..0'* - 1$ 2 -.($"#/)*/)*/$ /# $(+*-/
RENAMED IMPORTS
372
'$.)($"#/)*/- *")$5 /# '$.)( >*2 1 -;+-/$0'-'42# )4*00. )
!*-2*-&$)"2$/#*/'$);7)$)"/# .*0- *!/# '$.0.0''4$. .4 )*0"#;
)/# '$.0.0''4$.' ..0( -.*( /#)$./# !0''4@,0'$7 '..)( >
RENAMED IMPORTS
373
Operator Overloading
)*/# -2*-.;2#/*4*0" /2# )4*0$1$  String 4) IntB);)4*0
#)" /#/ #1$*-B
What the #$@&%!?
)4+-*"-(($)"')"0" .$)1*'1 (/#>#/(/#0.0''4$)1*'1 ..4(*'.
. -1$)".*+ -/*-.=
valval thisIsNotQuitePi = 22 / 7
 - ; / $..4(*'- +- . )/$)"/# $1$.$*)*+ -/*->
The Concept of Operator Overloading
*( 2# - ;/# +-*"-(($)"')"0" ) ./*&)*2/#/ / ( ).$1$.$*);)$/
) ./*/0''4$(+' ( )//# $1$.$*)!0)/$*)'$/4>
*-. ;/# $1$.$*)'*"$(41-4.*( 2#/4//4+ >$1$$)"/2*$)/ " -.
($"#/ #)' .*( 2#/$6 - )/'4/#)2*0'$1$$)"/2*9*/$)"@+*$)/
)0( -.>
*( ')"0" ..$(+'4E& $)F/# $-.0++*-/!*-*+ -/*-.)/# $-*-- .+*)$)"
*+ -/$*).>1;!*- 3(+' ;*).$ -.*+ -/*-.)2#//# 4*/* +-/*!/#
*- ')"0" >
/# -')"0" .H.0#.*/'$)H.0++*-/ *+ -/*-*1 -'*$)">$(+'4+0/=
I # ')"0"  7) .(++$)" /2 ))*+ -/*-)!0)/$*))(
375
J >">; / (++$)"/* divide() !0)/$*)K
I # ')"0" D../)-'$--4*6 -.$(+' ( )//$*).*!/# !0)/$*)!*-
3+ / //4+ .J >">; divide() *) NumberK
I # ')"0" ''*2. 1 '*+ -./*$(+' ( )//# .( @)( !0)/$*)*)
*/# -'.. .)"$)*+ -/*-.0++*-/!*-/#*. '.. .J >">; divide(Int)
*) String /*.0++*-/.+'$//$)"./-$)"$)/*.+ $7 )0( -*!+$  .K
*( ')"0" .H.0#.2$!/H/& /# ) 3/./ +)''*2 1 '*+ -./* 7)
-)@) 2*+ -/*-.J >">; ***K;2#$#/# )" //- / /# .( ./# ./)-
*+ -/*-.>*/'$)* .)*/"*/*/#/' )"/#;/#*0"#>
Example: Dividing a String
4 !0'/;$)*/'$);/#$.!$'.2$/#*(+$'  --*-=
funfun main() {
valval pieces = "This is a reasonably long string" / 3
}
*/'$)* .)*/&)*2#*2/*$1$  String 4) Int>*2 1 -;2 )/ #
*/'$)/**/#$.;424*!$(+' ( )/$)" div() 3/ ).$*)!0)/$*) *) String=
operatoroperator funfun StringString.div(count: IntInt) = thisthis.chunked((thisthis.length / count) + 1)
funfun main() {
println("This is a reasonably long string" / 3)
}
J!-*( N+ -/*-1 -'*$)"N$)/# '..**&K
0./.2 ) /* override !0)/$*)/#/$. '- $).0+ -/4+ ;2 ) /*
/# operator & 42*-/*!0)/$*)2#*. )( $.0. 4*/'$)./#
$(+' ( )//$*)*!)*+ -/*-> - ; div() $./# !0)/$*)/#/*/'$)(+. / /*>*;
*0- div() 3/ ).$*)!0)/$*)''*2.0./*$1$  String 4) Int>
So. Many. Operators.
# - - ()4*+ -/*-.$)*/'$);$)'0$)".*( /#$)"./#/4*0($"#/)*/
3+ //* *).$ - *+ -/*-.>
OPERATOR OVERLOADING
376
inin
in $.)*+ -/*->/(+./*/# contains() !0)/$*)>*;)4/4+ /#/.0++*-/.
contains() H String; List; />H) 0. 2$/# in=
funfun main() {
println("foo" inin "foobar")
}
#$' in #++ )./* )*+ -/*-;2 )- / */# -E*+ -/*-.F/#/
.4)//$''4'**&'$& in>*- 3(+' ;2 0.  to $) mapOf()=
valval things = mapOf("key" to "value", "other-key" to "other-value")
println(things::classclass)
println(things)
println(things.size)
J!-*( N+.N$)/# '..**&K
to $.)*/)*+ -/*->)./ ;$/$.) $)73!0)/$*);)2 )- / *0-*2)$)73
!0)/$*).>
Indexed Access
,0- @-& /.4)/3" /.(++ /**+ -/*-.> [] (+./* get();2#$' []= (+.
/* set()>
)/# . *! get();/# *((@ '$($/ '$./*!-"0( )/.$)/# -& /." /.
+.. /*/# get() !0)/$*)>.- .0'/;2 )0. [] !*-*/#.$)"' $) 3H
'$& 2 *2$/# List *- Map H)!*-(0'/$+' $) 3 .>
)/# . *! set();/# *((@ '$($/ '$./*!-"0( )/.$)/# -& /." /.
+.. /*/# set() !0)/$*);!*''*2 4/# 1'0 /*/# -$"#/*!/# *+ -/*->*;
something['foo'] = 3 $. ,0$1' )//* something.set('foo', 3)>
Invocation
# )2 . .4)/3'$& foo();2 ..0( /#/ foo $./# )( *!!0)/$*)>*./
*!/# /$( ;/#/$./# . >*2 1 -;$/*0''.* )*% /2#*. /4+ #.)
invoke() !0)/$*)=
OPERATOR OVERLOADING
377
operatoroperator funfun StringString.invoke(count: IntInt) = thisthis.chunked((thisthis.length / count) + 1)
funfun main() {
println("This is a reasonably long string"(3))
}
J!-*( N$)1*& JK+ -/*-N$)/# '..**&K
 - 2 #1 /# .( !0)/$*)/#/2 #!*- div(); 3 +/)*2$/$.)( 
invoke()> ''$/4E''$)"!0)/$*)F*)/# *% /;+..$)") Int +-( / ->
OPERATOR OVERLOADING
378
Infix Functions
. /# #+/ -*)*+ -/*-*1 -'*$)" ( )/$*) ;4*0))*/$(+' ( )/4*0-*2)
*+ -/*-.?/' ./)*/$)/# !*-(*!+0)/0/$*)>);2 .2/#/.*(
*+ -/*-.;'$& in;- )*/. *)+0)/0/$*)>
# )$ /#$)"$./#/*/'$)' /.4*0- / 4*0-*2)E*+ -/*-.F;/*.0++*-/.4)/3
'$& "this is very impolite" disemvowel "*">
Postfix and Infix
).*!'..$'0'/*-.2$''- ( ( -E- 1 -. *'$.#)*//$*)FJK./#
/ )/-4!*-(/>*'0'/ T+'0.T;4*02*0'+- .. 2 Enter 2 +
*)/# & 4*-># Enter & 4+0.# .)0( -*)/*/# ./&;)/# +
*+ -/$*)/& ./# 0-- )/)0( -;+*+./# './)0( -*6*!/# ./&;).
/# (>#$.$.'' E+*./73F)*//$*)>
'0'/*-.!-*(*/# -()0!/0- -.*!/ )0. ) = & 4># - ;/*'0'/ T
+'0.T;4*02*0'+- .. 2 + 2 = >#$.E$)73F)*//$*)$.'.*#*2(*./
+-*"-(($)"')"0" .2*-&J*-/# $)"*)  3 +/$*)K>
The infixinfix Keyword
!4*0/# infix & 42*-/*!0)/$*) '-/$*);4*0)0. /#/!0)/$*)
0.$)"$)73)*//$*);.&$++$)"/# +- )/# . .)'**&$)"(*- '$& /# in
*+ -/*-=
privateprivate valval VOWELS = "[aeiouAEIOU]".toRegex()
infixinfix funfun StringString.disemvowel(replacement: StringString) = VOWELSVOWELS.replace(thisthis, replacement)
379
funfun main() {
println("this is very impolite" disemvowel "*")
}
J!-*( N)730)/$*).N$)/# '..**&K
* E$. (1*2 'F .*( / 3/$..$(+'4/*- (*1 /# 1*2 '.;- +'$)"/# (2$/#
+' #*' -.*-%0./ '$($)/$)"/# (*0/-$"#/> - ;2 #1  disemvowel()
3/ ).$*)!0)/$*)*) String /#/- (*1 ./# )"'$.#@')"0" 1*2 '.J$")*-$)"
E4F)EF 0. )"'$.#$.2 $-K>*0- 2 '*( /*'' disemvowel() 0.$)"
- "0'-!0)/$*))*//$*)=
println("this is very impolite".disemvowel("*"))
*2 1 -;/# infix & 42*-( )./#/2 )0. /#$.!0)/$*)(0#'$& 2
2*0'0. )*+ -/*-'$& in=
I **/*)) /$)"/# !0)/$*))( /*/# *% /*)2#$#2 - ''$)"
$/J/# E-  $1 -FK
I *+- )/# . .-*0)/# +-( / -
 "-' ..*!2# /# -4*00. disemvowel() .!0)/$*)*-.+. 0*@*+ -/*-;
4*0" /$. (1*2 ' ./-$)"=
th*s *s v*ry *mp*l*t*
Limitations
# infix !0)/$*)) ./*/&  3/'4*) +-( / ->#$.( ).=
I *5 -*@+-( / -!0)/$*).
I *!0)/$*).2$/#T]+-( / -.
I *1--".
I * !0'/1'0 .
'.*;$/))*/ /*+@' 1 '!0)/$*)H/# !0)/$*)) ./*#1 -  $1 -*!
.*( !*-(>)/# +-  $)" 3(+' ;2 #1 $/.) 3/ ).$*)!0)/$*);/#*0"#
- "0'-'..!0)/$*)$.'.*7) >
INFIX FUNCTIONS
380
Destructuring Declarations
*/'$) 3+- ..$*)($"#/2 ''- /0-)(*- /#)*) 1'0 >#$.* .)*/( )
E(*- /#)*) 1'0 ;0/''./*- $).$)"' *% /F;/#*0"#*1$*0.'4*/'$))
*/#//**>)./ ;/#$.( ).'$/ -''4" //$)"(*- /#)*) - .0'/!-*(/#
3+- ..$*)=
valval (something, orAnother) = gimmeStuff()
 - ; gimmeStuff() 2$''+*+0'/ */# something ) orAnother? 1 )/#*0"#
gimmeStuff() #.*)'4*) - /0-)1'0 >
#$./-$&$.'' E ./-0/0-$)" '-/$*)F>
The Components of a Class
*-/#$./*2*-&;/# *% /- /0-) 4/#  3+- ..$*)J >">;/# *% /- /0-) 4
gimmeStuff()K) ./*$(+' ( )/!0)/$*).)(  component1(); component2();
).**)># +- )/# /$''$./*!+-*+ -/$ .2$''0. /#*. !0)/$*).$))0( -$'
*- ->*;/# *1 * .)$++ /$. ,0$1' )//*=
valval temp = gimmeStuff()
valval something = temp.component1()
valval orAnother = temp.component2()
J*!*0-. ;/#$.. *).)$++ /'.*. /.0+ temp 1-$' /#//# 7-./.)$++ /
1*$.K
)40$'/@$)'.. .;'$& Pair;$(+' ( )//# . !0)/$*).;*) !*- 1 -4$./$)/
+$  *!/>*;$)/# . *! Pair;$/$(+' ( )/. component1() )
component2();0/)*/*/# -.;. Pair *)'4#*'./2*$/ (.>
381
'.*; data '.. .$(+' ( )//# . !0)/$*).;2$/#/# )0( - !0)/$*).
*-- .+*)$)"2$/#/# *)./-0/*-+-( / -+*.$/$*).=
data classdata class PersonPerson(
valval name: StringString,
valval quest: StringString,
valval airSpeedVelocityUnladenSwallow: FloatFloat
)
funfun main() {
valval (who, what, waitWut) = PersonPerson("Arthur", "To seek the Holy Grail", 0.01f)
println(who)
println(what)
println(waitWut)
}
J!-*( N ./-0/0-$)" '-/$*).N$)/# '..**&K
 - ;*0- Person '..2$).0+2$/#/#- *(+*) )/!0)/$*).=
Component Function Returned Property
component1() name
component2() quest
component3() airSpeedVelocityUnladenSwallow
J)*/ =/# 0)$/*!( .0- ( )/!*- airSpeedVelocityUnladenSwallow $.$) !0-'*)".
+ -($-*!*-/)$"#/K
OK, Why Would We Use This?
'';$)" ) -';4*0+-*'4.#*0')*/0. $/>/2$'' -/# -!-"$' .'.. .
#)" >*- 3(+' ;$!.*( *4#)" . Person /* =
data classdata class PersonPerson(
valval name: StringString,
valval quest: StringString,
valval favoriteColor: ColorColor,
valval airSpeedVelocityUnladenSwallow: FloatFloat
)
DESTRUCTURING DECLARATIONS
382
*2 waitWut 2$).0+ $)". //*/# favoriteColor 1'0 ;)*//#
airSpeedVelocityUnladenSwallow 1'0 >)/#$.. ;/# !//#/ waitWut  *( .
Color $)./ *! Float ( )./#/4*0($"#/2$)0+2$/#*(+$' - --*-.)
2$'' / //# !//#/ waitWut #)" >0/;$!$)./  Person 2 - #)" /*=
data classdata class PersonPerson(
valval name: StringString,
valval capitalOfAssyria: StringString,
valval quest: StringString,
valval airSpeedVelocityUnladenSwallow: FloatFloat
)
*2/# 1'0 *! wait 2$''#)" ;0//# /4+ 2$'')*/;.*2 ($"#/)*//#/#
$6 - ) >
J$!4*0*)*/" //# *- ! - ) .# - ;"*2/# /#$.(*1$ ;+- ! -'4. 1 -'
/$( .K
So, Why Does This Exist?
!/# 0/#*-#/*"0 ..; ./-0/0-$)" '-/$*).2 -  !*-0. 2# )
$/ -/$)"*1 - Map=
funfun main() {
valval montyPythonCast = mapOf(
"Graham Chapman" to "Arthur, King of the Britons",
"John Cleese" to "Sir Lancelot",
"Terry Gilliam" to "Patsy",
"Eric Idle" to "Sir Robin",
"Terry Jones" to "Sir Bedevere",
"Michael Palin" to "Sir Galahad"
)
forfor ((actor, role) inin montyPythonCast) {
println("$actor -> $role")
}
}
J!-*( N ./-0/0-$)" '-/$*).*!+.N$)/# '..**&K
*-(''4; for '**+*1 - Map "$1 .4*0.$)"' Map.Entry +-*+ -/4;)4*0#1
/*- ! -/*/# key ) value *)/#//*" //*/# $)$1$0'+$  .*!//#/4*0
- '**&$)"!*->$/# ./-0/0-$)" '-/$*).;4*0)#1 /# for '**+"$1 4*0
/# & 4)1'0 $)$)$1$0'+-*+ -/$ ./#/#1 0. !0')( .J-/# -/#) key
DESTRUCTURING DECLARATIONS
383
) valueK>
#$.'.*2*-&.2$/#'( 3+- ..$*).;.0#.0.$)" forEach() *) Map=
funfun main() {
valval montyPythonCast = mapOf(
"Graham Chapman" to "Arthur, King of the Britons",
"John Cleese" to "Sir Lancelot",
"Terry Gilliam" to "Patsy",
"Eric Idle" to "Sir Robin",
"Terry Jones" to "Sir Bedevere",
"Michael Palin" to "Sir Galahad"
)
montyPythonCast.forEach { (actor, role) -> println("$actor -> $role") }
}
J!-*( N ./-0/0-$)" '-/$*).)(.N$)/# '..**&K
DESTRUCTURING DECLARATIONS
384
Labeled Returns
*( /$( .;$)*/'$);4*02$''.  return !*''*2 4 @ ).*( )( ;.0#.
return@overthere>#$.$.E' ' - /0-)F)''*2.4*0/*#1 .*( *)/-*'
*1 -2# - return /0''4- /0-)./*>
Where returnreturn Goes By Default
- ,0 )/'4;/# - $.'$//'  / *1 -/#  #1$*-*! return=
funfun theFunction() {
returnreturn
}
 - ; return - /0-).!-*( theFunction()>
#$)"." /( ..$ -2# )2 ./-//*$)/-*0 ) ./ *)./-0/.;.0#.'(
3+- ..$*).=
funfun somethingifier(items: ListList<StringString>) {
items.forEach {
ifif (it.length == 3) returnreturn elseelse println(it)
}
println("Done!")
}
funfun main() {
somethingifier(listOf("this", "is", "a", "fun", "bit", "of", "Kotlin"))
}
J!-*( N !0'/- /0-) #1$*-N$)/# '..**&K
385
 - ;2 return $!2  )*0)/ -/#- @' // -2*->*0($"#//#$)&/#//#$.
2*0'%0./ 3$//# '( 3+- ..$*);.*2 2*0'+-$)/E*) AF>*2 1 -;4
 !0'/; return - /0-).!-*(2#/ 1 - function $/$.$)>( 3+- ..$*).- )*/
!0)/$*).>*;*0- return - /0-). )/$- '4!-*( somethingifier();4+..$)"/#
E*) AF println() .// ( )/>*;2 " /=
this
is
a
#$' '( 3+- ..$*)$.)*/!0)/$*);) )*)4(*0.!0)/$*) is !0)/$*)>
 2-$/$)"/# *1 .)$++ //*0. ))*)4(*0.!0)/$*)#)" ./#  #1$*-
$/=
funfun somethingifier(items: ListList<StringString>) {
items.forEach(funfun(it: StringString) {
ifif (it.length == 3) returnreturn elseelse println(it)
})
println("Done!")
}
funfun main() {
somethingifier(listOf("this", "is", "a", "fun", "bit", "of", "Kotlin"))
}
J!-*( N- /0-)))*)4(*0.0)/$*).N$)/# '..**&K
*2;*0-E0.$) ..'*"$F$.*)/$) $)))*)4(*0.!0)/$*);)*/'(
3+- ..$*)> return;4 !0'/;- /0-).!-*(2#/ 1 -!0)/$*)$/$.$);.**0- return
- /0-).!-*(/# )*)4(*0.!0)/$*)>#/%0./*(+' / ./# 0-- )/+..$)/#
forEach '**+)''*2./# '**+/**)/$)0 >.- .0'/;2 %0./.&$+/# /#- @
' // -2*-.;)2 " /E*) AF//#  )=
this
is
a
of
Kotlin
Done!
LABELED RETURNS
386
Returning Just from a Lambda
!4*02)//# . *). /*!- .0'/.;0/4*02)//# .$(+' -.4)/3*!'(
3+- ..$*);4*0)0. ' ' - /0-)=
funfun somethingifier(items: ListList<StringString>) {
items.forEach {
ifif (it.length == 3) returnreturn@forEach elseelse println(it)
}
println("Done!")
}
funfun main() {
somethingifier(listOf("this", "is", "a", "fun", "bit", "of", "Kotlin"))
}
J!-*( N '  /0-).N$)/# '..**&K
# !0)/$*)/#/$)1*& .'( 3+- ..$*). /.)$(+'$$/' ';*!/# .(
)( ./# !0)/$*)$/. '!> - ; forEach() $.$)1*&$)"/# '( 3+- ..$*);.*
2 #1  forEach ' '/#/2 )0. > return@forEach;/# - !*- ;- /0-).!-*(
%0.//# '( 3+- ..$*);)*/!-*(/#  )/$- somethingifier() !0)/$*)>
Applying a Label
# +-*' (2$/#$(+'$$/' '.*( .2# )4*0#1 ) ./ ''.*!/# .(
!0)/$*)/4+ J >">; forEach() 2#*. '( 3+- ..$*)*)/$). forEach()K>
*0) 3+'$$/'4' ''( 3+- ..$*)$!4*0'$& ;/# )0. return@ 2$/#4*0-
0./*(' '>
' '$.)$ )/$7 -+'0. @;+-  $)"/# '( 3+- ..$*)$/. '!=
funfun somethingifier(items: ListList<StringString>) {
items.forEach toSender@ {
ifif (it.length == 3) returnreturn@toSender elseelse println(it)
}
println("Done!")
}
LABELED RETURNS
387
funfun main() {
somethingifier(listOf("this", "is", "a", "fun", "bit", "of", "Kotlin"))
}
J!-*( N0./*( '.N$)/# '..**&K
*;# - ;2 #1 ' ' /# '( 3+- ..$*) toSender@;.*2 )0.
return@toSender /*- /0-)!-*(%0.//# '( 3+- ..$*)>
*/ /#/ break ) continue '.*.0++*-/' '.;*/#$(+'$$/)0./*(*) .;/*
# '+*)/-*'2# - /# 4"*/*>
Is Any of This a Good Idea?
*0*)*/. ' '.)' ' - /0-).0. (0#$)*/'$)* .(+' .># $-
0. (4 .$")*!*1 -'4@*(+' 3'*"$2$/#$).$)"' !0)/$*)>*).$ -
 *(+*.$)"/# 2*-&$)/*. +-/ !0)/$*).;$!/# 4' /4*01*$' ' - /0-).>
# - .0'/$)"* $.'$& '4/* (*- - ' ;+-/$0'-!*-- '/$1 ) 2*( -./*
*/'$)>
LABELED RETURNS
388
NothingNothing
-'$ -$)/# **&;2 .2/# TODO() !0)/$*)=
funfun heyThisIsNotDoneYet() {
TODOTODO("implement this function")
}
TODO() /#-*2.) 3 +/$*)2$/#/# .0++'$ ( .." > 0. /#$..(*-
!*- !0''/ -)/$1 /*.$(+'40.$)" TODO *(( )//*$ )/$!4$)*(+' / 2*-&>
*)/-.//#/2$/#=
funfun heyThisIsNotDoneYet(): IntInt {
TODOTODO("implement this function")
}
)/# .0-! ;/#$.2*0'. ('$& $/*0')*/+*..$'4*(+$' > #1 . /0+
heyThisIsNotDoneYet() /*- /0-)) Int;4 /2 #1 )* return .// ( )/*-
)4/#$)" '. /*- /0-)1'0 >!/ -'';$!2 .&$++ /# TODO();2  7)$/ '4
2*0'" /*(+$'/$*) --*-=
funfun heyThisIsNotDoneYet(): IntInt {
// TODO implement this function
}
*;2#/$.$/*0/ TODO() /#/''*2.$//*.*( #*2*(+' / /# *4*!
!0)/$*)/#/- /0-)..*( /#$)";2#$' )*/- /0-)$)")4/#$)"B
# ).2 -$.=)*/#$)">-;(*- 0-/ '4; Nothing>
389
NothingNothing: It’s On the Bottom
-'$ -$)/# **&;2 .2 Any> Any $)*/'$)$.)'*"*0./* Object $)1=$/$./#
-**//4+ !-*(2#$#''*/# -/4+ .$)# -$/>)/-0/#; Any $. 1 )(*- 2$ .+- 
$)*/'$)/#) Object $.$)1; 0. all */'$)/4+ . 3/ ) Any;2# - .
+-$($/$1 .J >">; intK)--4.J >">; int<>K*)*/ 3/ ) Object $)1>
Nothing #.)*1)'*"0 )$.*)/# !-*/# - )*!/# $)# -$/)
.+ /-0(>6 /$1 '4; Nothing $..0@/4+ *! all other types>/$..0@/4+ *! Any>
/$/.0@/4+ *! String>/$..0@/4+ *! Axolotl>
Nothing #.)*$)./) .>/$.$(+*..$' /*- / $)./) .*! Nothing;.$/#.
private *)./-0/*->
Uses of NothingNothing
)/# .0-! ; Nothing (4. (0. ' ..>);$)4@/*@4 1 '*+( )/;$/$.
0)'$& '4/#/4*02$''0. Nothing $- /'4?0/$/$.1 -4'$& '4/#/4*0- 0.$)"
Nothing 2$/#*0/- '$5$)"$/>
As a Return Type
!0)/$*))- /0-) Nothing ./4+ >#$.($"#/. ($(+*..$' ;.$) /# - -
)*$)./) .*! Nothing>-*(*(+$' -./)+*$)/;/#*0"#;!0)/$*)- /0-)$)"
Nothing ( )./# !0)/$*) is required to never return># +-$(-4. )-$**!/#$.
$.$!/# !0)/$*)$."0-)/ /*/#-*2) 3 +/$*)>
#/(4.*0)./-)" ?0)/$'4*0*).$ - TODO()>
TODO() $. '- /*- /0-) Nothing=
publicpublic inlineinline funfun TODO(reason: StringString): NothingNothing =
throwthrow NotImplementedErrorNotImplementedError("An operation is not implemented: $reason")
TODO() $.(-& . $)" ) inline !0)/$*);.*/# *(+$' -2$''E& F/#
3 +/$*)-$"#/$)/*2# - 1 -/# TODO() ++ -.>*2 1 -;/# Nothing - /0-)/4+
$)$/ ./*/# *(+$' -/#/.$) TODO() )) 1 -- /0-);/# - $.)*. ). $)
2*--4$)"*0/)4/#$)" '. $)/# !0)/$*)!/ -/#/+*$)/;$)'0$)"/# ($..$)"
return>
NOTHING
390
*- 3(+' ;.0++*. 2 - / *0-*2).$($'-!0)/$*);''  NOTDONE()=
publicpublic inlineinline funfun NOTDONE(reason: StringString) {
throwthrow NotImplementedErrorNotImplementedError("$reason")
}
funfun heyThisIsNotDoneYet(): IntInt {
NOTDONENOTDONE("wut")
}
funfun main() {
heyThisIsNotDoneYet()
}
#$.!$'.*(+$'/$*)2$/# A 'return' expression required in a function
with a block body ('{...}') !*-*0- heyThisIsNotDoneYet() !0)/$*)>1 )
/#*0"# NOTDONE() $. inline;).* heyThisIsNotDoneYet() 2$'''24./#-*2)
3 +/$*);/# *(+$' -$.)*/,0$/ .(-/ )*0"#/*7"0- /#/*0/*)$/.*2)>
$)" Nothing ./# - /0-)/4+ /* NOTDONE() ' -./#/0+=
publicpublic inlineinline funfun NOTDONE(reason: StringString): NothingNothing {
throwthrow NotImplementedErrorNotImplementedError("$reason")
}
funfun heyThisIsNotDoneYet(): IntInt {
NOTDONENOTDONE("wut")
}
funfun main() {
heyThisIsNotDoneYet()
}
J!-*( N*/#$)". /0-)4+ N$)/# '..**&K
On the Right Side of Elvis
TODO() $.)*//# *)'4!0)/$*)/#/- /0-). Nothing> error() * ..2 ''=
publicpublic inlineinline funfun error(message: AnyAny): NothingNothing = throwthrow
IllegalStateExceptionIllegalStateException(message.toString())
error() !*-(.)$ .#*-/#)!*-/#-*2$)") 3 +/$*). *)!$'  null
# &0.$)"/# '1$.*+ -/*-=
NOTHING
391
funfun main() {
valval something: StringString? = "foo"
valval somethingNotNull = something ?: error("hey, that was null!")
println(somethingNotNull)
}
# - .*)2#4/#$.*(+$' .$./#/ Nothing $..0@/4+ *!''/4+ .;$)'0$)"
String> ) ;!-*(/4+ @.! /4./)+*$)/=
I # ' !/.$ *!/# '1$.*+ -/*-$. String; 0. 2 &)*2/#/$/$.)*/
null
I # -$"#/.$ *!/# '1$.*+ -/*-$. Nothing
I # *(+$' -7)./# *((*).0+ -/4+ *!/#*. ;2#$#$. String;)
*).$ -./#  3+- ..$*)D.*1 -''/4+ /* String
For Covariant Generics
# out & 42*-.$")$7 . *1-$) $)" ) -$/4+ =2 ) +//# /4+ *-)4
.0@/4+ > List;!*- 3(+' ;0. . out=
interfaceinterface ListList<outout EE> : CollectionCollection<EE>
.- .0'/;2 )0.  List<Nothing> $)+' *!)4*/# -/4+  List=
List<String>; List<Axolotl>; />
# .( #*'./-0 !*-)4*1-$)/" ) -$/4+ >
For Generic Singletons
#/+- 1$*0.. /$*)($"#/. ( .*/ -$>!/ -'';$!/# - - )*$)./) .*!
Nothing;2  -/$)'4))*/#1  List *!.0#$)./) .>*2 1 -;%0./ 0. 
List<Nothing> $.$(+*..$' * .)*/( )/#/ List<Nothing> #.)*0. .>
*- 3(+' ;.0++*. 2 ) ) (+/4'$./*!.*( /#$)">) 24/**/#/$./*
0. listOf() 2$/#)**)/ )/.=
valval things: ListList<StringString> = listOf()
)'/ -)/$1 $./*0. emptyList()=
valval things: ListList<StringString> = emptyList()
NOTHING
392
#*. '**&) -'4$ )/$';0//# 4/0''4#1 .$")$7)/'4$6 - )/
$(+' ( )//$*).> listOf() 2$''$)./)/$/ ) (+/4 List;''*/$)"$/*!
( (*-4'*)"/# 24> emptyList() * .)*/? 0. emptyList() - /0-).
.$)"' /*)=
publicpublic funfun <TT> emptyList(): ListList<TT> = EmptyListEmptyList
?) EmptyList $. List<Nothing>=
internalinternal objectobject EmptyListEmptyList : ListList<NothingNothing>, SerializableSerializable, RandomAccessRandomAccess {
privateprivate constconst valval serialVersionUID: LongLong = -7390468764508069838L
overrideoverride funfun equals(other: AnyAny?): BooleanBoolean = other isis ListList<*> && other.isEmpty()
overrideoverride funfun hashCode(): IntInt = 1
overrideoverride funfun toString(): StringString = "[]"
overrideoverride valval size: IntInt getget() = 0
overrideoverride funfun isEmpty(): BooleanBoolean = truetrue
overrideoverride funfun contains(element: NothingNothing): BooleanBoolean = falsefalse
overrideoverride funfun containsAll(elements: CollectionCollection<NothingNothing>): BooleanBoolean =
elements.isEmpty()
overrideoverride funfun get(index: IntInt): NothingNothing =
throwthrow IndexOutOfBoundsExceptionIndexOutOfBoundsException("Empty list doesn't contain element at index
$index.")
overrideoverride funfun indexOf(element: NothingNothing): IntInt = -1
overrideoverride funfun lastIndexOf(element: NothingNothing): IntInt = -1
overrideoverride funfun iterator(): IteratorIterator<NothingNothing> = EmptyIteratorEmptyIterator
overrideoverride funfun listIterator(): ListIteratorListIterator<NothingNothing> = EmptyIteratorEmptyIterator
overrideoverride funfun listIterator(index: IntInt): ListIteratorListIterator<NothingNothing> {
ifif (index != 0) throwthrow IndexOutOfBoundsExceptionIndexOutOfBoundsException("Index: $index")
returnreturn EmptyIteratorEmptyIterator
}
overrideoverride funfun subList(fromIndex: IntInt, toIndex: IntInt): ListList<NothingNothing> {
ifif (fromIndex == 0 && toIndex == 0) returnreturn thisthis
throwthrow IndexOutOfBoundsExceptionIndexOutOfBoundsException("fromIndex: $fromIndex, toIndex: $toIndex")
}
privateprivate funfun readResolve(): AnyAny = EmptyListEmptyList
}
-*(+-/$'./)+*$)/;*/# listOf() ) emptyList() 7''/# -*' *!)
(+/4'$./;0/ 0. emptyList() - /0-)..$)"' /*); emptyList() * .)*/
''*/ ( (*-4>
NOTHING
393
-*(*(+$'/$*)./)+*$)/; emptyList() ) ++'$ /*)4/4+ ; 0.
Nothing $..0@/4+ *!)4/4+ >
NOTHING
394
Types of Keywords
)4+-*"-(( -.- 0. /*/# )*/$*)/#/4*0))*/0. & 42*-../#
)( .*!1-$' .;+-*+ -/$ .;!0)/$*).;)/# '$& >*;!*- 3(+' ;/#$.!$'./*
*(+$' =
funfun main() {
valval class = 1337
println(classclass)
}
0/.*( & 42*-.;'$& import;2*-&%0./7) =
funfun main() {
valval import = 1337
println(importimport)
}
# - - ! 2/4+ .*!& 42*-.$)*/'$);) ##.$/.-0' .!*-2# - )
2# )4*0)0. /# (.$ )/$7 -.>
Hard Keywords
-& 42*-.'$& class;))*/ 0. .$ )/$7 -.)42# - >*./*/'$)
& 42*-.- *).$ - /* #->
# -*./ -*!#-& 42*-. 2$''1-44*/'$)1 -.$*);0/$/$)'0 ./#$)"./#/
4*0($"#/ 3+ / 4*) class;.0#.=
I else
395
I false
I fun
I if
I interface
I null
I object
I package
I return
I this
I true
I try
I val
I var
I when
Soft Keywords
.*!/& 42*- #.( )$)"$)+-/$0'-*)/ 3/;)*/# -2$. $.1$'' !*-
0. .)$ )/$7 ->
*- 3(+' ; try ++ - $)/# *1 '$./*!#-& 42*-.> catch ) finally *
)*/;))*/ 0. /# 0/#*-$)*/.0++'4/# *(+' / '$./> catch ) finally
- *).$ - /* .*!/& 42*-.;.$. import;2#$#( )./#/4*0)0. /# (
.$ )/$7 -.$)(*./+' .>
E*./+' .F;/#*0"#;(4)*/  1 -42# - >
*;!*- 3(+' ; constructor $..*!/& 42*->*0)#1 1-$' )( 
constructor;$!4*02)/ =
funfun main() {
valval constructor = 1337
println(constructorconstructor)
}
*2 1 -;/' ./*)*/'$)G;4*0))*/#1 !0)/$*))(  constructor()=
classclass FooFoo {
funfun constructor() {
println("I'm not a constructor!")
}
TYPES OF KEYWORDS
396
}
funfun main() {
FooFoo().constructorconstructor()
}
)/#$.. ;/# +-*' (++ -./* /$ /*1-$+/$)/ -*+ -$'$/4;./#
*/'$)G/-).+$' -" ) -/ .!0)/$*))(  constructor() /#/ is /# /0'
*)./-0/*-;.*#1$)"4*0-*2) constructor() !0)/$*)0. .)( *''$.$*)>
Modifier Keywords
(*$7 -& 42*- '.*#.( )$)"$).+ $7*)/ 3/>)/#$.. ;/# *)/ 3/
$. '-/$*);)/# & 42*-) 0.  '. 2# - .)$ )/$7 ->
*- 3(+' ; public $.(*$7 -& 42*-;.*4*0*0'#1 1-$' )( 
public $!4*02)/ =
funfun main() {
valval public = 1337
println(publicpublic)
}
*0) 1 )#1 !0)/$*))(  public=
classclass FooFoo {
funfun public() {
println("I am public, and I am public()")
}
}
funfun main() {
FooFoo().publicpublic()
}
# - (4 . ' /- .2# - 4*0))*/0. (*$7 -& 42*-..$ )/$7 -.;
0/*)/# 2#*' ;4*0)>
/# -)*/' (*$7 -& 42*-.$)'0 =
I abstract
I companion
TYPES OF KEYWORDS
397
I const
I data
I enum
I lateinit
I open
I override
I private
I protected
I sealed
I vararg
In General, Avoid Keywords
)4*0 0/ )#1 +-*+ -/4)(  publicB)4*0  1 )(*- 0/ )
#1  private +-*+ -/4)(  publicB
.>
./#$."**$ B
-*'4)*/>
 ( ( -/#//# +*$)/ #$)$ )/$7 -.$./*/ '''' 1 '*+ -.H*/#4*0-
0-- )// ((/ .)E!0/0- 4*0FH2#//# 1-$' ;+-*+ -/4;!0)/$*); />
( ).> 0.$)"& 42*-.!*-/#$.+0-+*. )0. *)!0.$*); 1 )$!/# .4)/3
2$'' *(+' / '4' "$/$(/ ).! >
- ,0 )/'4;/# & 42*-(& .!*-+**-$ )/$7 -)424> set $..*!/& 42*-;
0/4*0)./$''0. $/.1-$' )( =
funfun main() {
valval set = setOf(1, 3, 3, 7)
println(setset)
}
*0($"#//#$)&E2 '';$/$. Set;.*2#4)*/''$/ setBF*2 1 -;2#/$/* .)*/
*)1 4$.2#/$/$. Set of>)4 Set *0' )(  set H/# - $.)**)/ 3/!*-
2#$#*!4*0-()4 Set *% /./#$. set #++ )./*- ! -/*>
0./ 0. /# *(+$' -''*2. -/$).4)/3* .)*/( )/#//# #*$ *!
TYPES OF KEYWORDS
398
.4)/3$."**>-$/ * /#/$. .4/*- >
TYPES OF KEYWORDS
399