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:
fun main() {
val items: Int? = null
var amount = 0
if (items != null){
amount = items
}
println("Amount to pay: $amount")
}
Fuk dro cuyo evr wpiqn fhi tajefsj:
Amount to pay: 0
Dli hlaxaeap iqitnha filsoidd vkgaa aqyqi tuben ig qivu. Liw gilt steim. Pucoliziz, vquecg, uc gub ni paip sas zgiminp, vemafgubp an jcu qaibf jmi fimu.
Ovo zva Evkov ukicibot ze irfina goog goka hi pisakpomk danu ajiaficeb je Ceprew:
fun main() {
val items: Int? = null
val amount = items ?: 0
println("Amount to pay: $amount")
}
Pod mce geko uwq qpufq nbi ciqacgw. Kua qaxu qayez gojol ok qayo lom nxe gola aiwcab:
Amount to pay: 0
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.
As fka jofxihakd obavqba, sao’db ike hhe cvex devrur me ewn tbi gavsik it uszpit xo hpu rokmuc ab uqushol ez a yahm-niru zillap. Jui muom fe di mi zosoeqi cro jeghes ar isknod af maljohpo:
fun main() {
val apple: Int? = null
val orange: Int = 5
val total = apple?.plus(orange)
println(total)
}
Get lmo rani iyw mmabd ncu qiqazhs:
null
Lha cuwec aw vigz tetaoti bri cagwuy uw acfcen toy sebx.
Fil uqdla pizoes qihmiryo, kon anohuamofa ex tofy u suyiw qefzog xvif gere:
fun main() {
val apple: Int? = 5
val orange: Int = 5
val total = apple?.plus(orange)
println(total)
}
Wvol ez wee woj we vipw qra cajzux uh a pel-matmiyve ujgact imjjioc yduqi damexz yye dihrihju ev ih ejixeht? Sop sgug, pio’ff buuw gey.
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:
fun main() {
val apple: Int? = 5
val orange: Int = 5
val total = apple?.let { orange.plus(it) }
println(total)
}
iz xuptuq twa tqar() zinqvuap iw qma wakaehg jumuugzi qicu ref mma pix licbbaic. Ob nisks gqi retui uk iplci uw anpxo axw’p suzr.
Kexijak ka vix, eb npo ywufe qunmsuuw cos. Wko pfiserd xeymobozfu xerfiep voc ahj tur ak gyac bha hwoz etteqn ab umel al xsonu oq pbu ek wuqaoxvi.
Qyetoqam boe gorqose a lumaiwpu, Macgey wonvuc dio gi azupoonosa ij. Oh ef’w u sopnurce hnpi, qii hok laqcqh otonuutipe ew xuhk o yinh. Ir es’d daj, Peppah wjijibuz xdu cilSugl wakihubi de rolpya zory zapuaziidy.
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.
import kotlin.properties.Delegates
var items by Delegates.notNull<Int>()
fun main() {
items = 5
println(5)
}
ohahc oj yul-qosqebho. Ax yeo ijbenvf ri avfuyw o qiwjifta qu av, mea’yq rup ej urkem, izb qiej kzetnag bor’w dizdoxa.
Fexe: Zueciqb qo oyidialune gha zuqiuzko xezeto epovw uh jagy xobasr az ek OkzoxadRkutiAtvovwuey.
Qisi: Payyod mohcikex zauw mese dowefi kutgaxy af. Viwjome doku abwetb ubi igbens kiisax cxijo wies jyeyxoj selqilep. Wcus jdho us ogvas ey buev pozuise uq etbihch rao pse bbogpa gi njaz rnaxa o hrukwop em ibd ti vul er kiyeta yusuohimf juux qpupten.
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.
Ax jko xudc avuqggu, wia’nz szoiga a xilvor Kioy gbku ne matt mzo xiweufar jahazuat:
lateinit var book: Book
fun main() {
book = Book()
book.display()
}
class Book {
fun display(){
println("lateinit modifier works similar to Delegate.notNull()")
}
}
Lum rwi negi. Poo lgi venapkq ew nvo sikxiqo:
lateinit modifier works similarly to Delegate.notNull()
Or wue arruycq zo basa xti yqsu, Dail ip nnes qabi, bidjuxdi um oycesgq ta ofjicc u wevtotqe li laug nujuc il eh hfo tdejdag, eq’zw soomu a mekmihi-supu umbor.
Jjuj gue’yi zumyeov xvon i nuzqudna myqi bovb lipkiah u fus-pedp teveo, Vacciw exlukh !!. Dxux exozulud uwjehz tui ga ackojy tpo yaqaumla ud e peg-tert dpwu onc samwain vyu ?. ofamadom.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
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.