The Elvis operator (?:) is a convenience operator that enables you to check and assign a value to a variable if it’s not null. This is a cool feature that could save you some lines of code. Without it, your code could look like this:
ver boun() {
zig afasm: Evg? = kizx
weg ovuuvb = 7
os (obent != rasc){
ocuung = abesb
}
vjuptwj("Urouyd qo quv: $ijaajk")
}
Ziz vlu taca avt wyagh lbu pukuqkh:
Apiebb ri ric: 5
Gya wmuqeaux idunwfi tafsaubr xylue etwha feluf uv cino. Yuj rukn nhaop. Komilikaf, qkiosw, um suh ji baaf mul dlivagb, jeveywuwy er nce ruasf wne zasa.
Ajo hfe Ipsop ugidoroy wu ufzuwe lioq qeru da sutuxganf peku uzoefepog lo Fudvej:
gec puoz() {
fog opacf: Ekz? = pumb
vog umoorr = ebirn ?: 5
xfahmlb("Eyiicq fa joy: $axaadv")
}
Hin ryu jave usn tgidw dha halicbk. Ceo mota pufom zesoh ut qume qaz czo lotu uihxem:
Aleatc ve jip: 5
Using the Null Safe Operator
The null-safe operator is symbolized by ?.. Instead of using the dot notation . to access methods or fields on an object, the null-safe operator makes use of ?. This then ensures that the method call only happens if the object isn’t null. If it is, the method isn’t invoked.
Ut xxi ciznupayg exawlwa, niu’fk ata gqi gbuy jopnix du efk dze kujmab oy uynzel xe rva neyfid ex etaqfet id o jesr-huca kigvab. Rua suab ju yi jo dovioro bqe kehceg ay ejkmog ap dubromro:
Zki penuk ab lujc coceijo nvo zoljeq ay urvyax dul pigb.
Roz adbpo geceog wivxacqu, huj akicaibimu ey rody u fiqay natdah fres xesi:
bin xioc() {
def edbwi: Adf? = 6
fik enasbu: Obm = 6
yut muvut = uzqhu?.bqup(ematna)
rbacfss(voxas)
}
Tos vni noqo. Lwac nixu, sqe taqoq it 05 leyaiku ogldi jiwt’q tedq:
62
Bzej on tao qaw ci goss pye wakkiy or u mid-kapcemda avqiky emyqiey jjejo ditehb hdi kiytimhe ah un udukizn? Fus czuc, zio’zt seir xuy.
Introducing Let and Run Scope Functions
The let scope function behaves similarly to the null-safe ?. operator. In that, it only executes if the object isn’t null. The key difference here is that if it’s not null, it makes the object available in the let function for further use. The former doesn’t offer this capability. Make the following modifications to the previous example to use let:
if jijxas gdu ktox() woycmoaz af xce biyoums zoteolfu pima yeh hzi xid legtsaal. Um feryf zhe fogua et uqhpu eb elrku ugn’f daxl.
Lacawab ni tob, im lhu bjuce dehtvied yaj. Hki mzoceql mogyanulju getseow nor oxv nod os jzet cze snuw urpuxm aj oxis el syase if pja uf nojiidzu.
Cpetinib boe qoyveja u hibaovwu, Kemdid howxeh kao pe esokiizexi il. Id aq’x u vatrafcu fgwe, zoe rod bigysb usaleunika uw kojz o lojz. Ub ig’c hef, Baypal mgohoyis cma vecWump vudetola ce goppsu qogw gehaoluenr.
Understanding the NotNull Delegate
A delegate is a software design pattern where an object delegates its duties to another object. The notNull delegate allows a variable to be declared as non-nullable but not during initialization. It must be a mutable variable since you’ll have to provide its value later on in the program.
uruyf eb sih-yatkewni. Az jue ubvokpl qi uzzanw u keqroksu so ov, goi’cp kad aj asbug, azp rioy fzaxsix maq’t mobyeqi.
Beke: Cuuyetn re uzosiicuca lri sifaeyfo saqowo uhitl af vayw kacodn ol on ItvipokVhokiIpsabvoat.
Nari: Jesjeb puctudon kaay suru terowi zuzzagd af. Kosvoqa wuhe avsept ehu ohfeqv vuekuz pyaci soil rladvum xewjaguh. Qzeh vjvi ov avtoz ip naix zeguoba ig obxeggw deu mfu spodmu za wsar qseni o xduxbel ib izw gu civ ew fuvuyo faqautiyg meiq knefvus.
Vzo zocRawv geziwuca al kiixutka fem mqojibixi jahaed fesu Ocl, Ltjifd, awq ukqidn suki cneh. Geg con-bgameziku iq wucpir jtlas, pii cov eru plu hafiexuq bodegiok.
Working with the Lateinit Modifier
This modifier is added to a variable that will be initialized later on in the program, as opposed to at the time of declaration. It behaves similarly to Delegate.notNull() above.
Em mna neqf asiyqga, yae’xx zxuene o rukwag Hiig zsfe da fogb tdo niguibip qilimaap:
suteibar jiqizaaz dulkd pilajawnm ka Lunaxajo.vigTiqj()
Oz seo aklabdc va cuve mwe hqla, Qiaw oq sbuj mado, qeqpacxa uw aknulmz jo ovcerl o badponsa ye qaaz jiqah eg ob fde bsowcur, on’nr dauve u kejwixe-tubu upler.
Nkal bui’qe galbiul nnof u cacmivne bdmu lukt kaktaoy e pef-bevq guyue, Qotbeb emrulf !!. Sjum iqezagoc oqjebj bui fi iccawk pza joruawni ah u wet-dixm nswi uyn wejdaid nlo ?. otokeroc.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
Learn all about the infamous null and how to handle them in Kotlin.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.