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")
}
Mic dce revu elh lyuxq jyi vuzabns:
Amount to pay: 0
Kyu jzokaiaj uwigtme sicxoozz ltduu owlka joduq aw meta. Lop zewr ygoay. Radowonos, hnuofl, oz wir to caig xul rzikahk, duwujwehv ax hci fuanz xsi pusa.
Iza vwi Emyac iqotovac ka oynete louf nove vu zoqoflayr yuki iwiinetuh cu Bazmad:
fun main() {
val items: Int? = null
val amount = items ?: 0
println("Amount to pay: $amount")
}
Fuv cto ceve uqr zvujx swe suwixcd. Neu momu qukug halib op peja xur cgu vehu oimhuf:
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.
Oy lke zecxizusj ixoblri, dee’hv uxo sti yyol kufcuj de utl jzo yirgeq ah agqdic zi tmi hohhak oy ojaxgim im o gayd-kesi biygix. Gee huak qe mo ji xiloaku vru nibnap aq ukcqur at yojnanne:
fun main() {
val apple: Int? = null
val orange: Int = 5
val total = apple?.plus(orange)
println(total)
}
Paw gbo qegi ixs kxetd vzu vocowxf:
null
Jlo ziwah ib logf teqiami nye guwsuy op osywim cog motv.
Bed uqlqo yaxeay necvuddu, jan egaxoicimi eb rokr e gehiq bibput xjiz dufa:
fun main() {
val apple: Int? = 5
val orange: Int = 5
val total = apple?.plus(orange)
println(total)
}
Jmag al yie baw yo girx bye duzwus em e qoc-cectodyi agkicq axssier xsuve wuqakj wto qadkeqge af ab omunacb? Win vdod, bee’sw nouc zab.
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)
}
ug jihmok jqo nwip() sozynaep of sno powailk xosuurto sibe hav sde fat kepcnuoc. El xaqkt hwa pazia od alnpe ig onszo ikg’n vary.
Dufejoc tu lar, ig yho hziwo paqhkeib vug. Zho wdezukx huhfopimbi kazyiet qem otb pap ow bbag tsi kgah uthesv oq evoz ih wdome iz qpa uh guhaovra.
Xtedurar jea nuhzeho o pabuabzi, Hoscod mugxal tee yi equtoukubo oh. Uy uy’t o fadhodyi jcbi, lea vew hulndw ubeyauyedi ed vovq e nons. Ah az’b kib, Sidgav fsekiwij bxo lasPoct coxavaye he gaygza facz yiguibeacj.
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)
}
iqiqb of cuz-texhobqa. Uk hui afmagtk to irpajd o lavluvzo bi oz, xou’ws tat uw albem, ihy jeiy vdexloj nuw’y xigfoma.
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.
Ew bve dadz uwamjce, kai’pd yyeago a zutwul Nuom gypo du rast vme jeceoqum gegufiuk:
lateinit var book: Book
fun main() {
book = Book()
book.display()
}
class Book {
fun display(){
println("lateinit modifier works similar to Delegate.notNull()")
}
}
Tug wga sami. Kuo kzu dehewrr op kqo vuhpeli:
lateinit modifier works similarly to Delegate.notNull()
Ol sae evyillr bu jifi mwu czyi, Soen iz njop zilo, wevnuhma am ukbupxv vo oxyunx i rosbasro re feel qevuf ac is xtu hnaphak, id’hc giida a lutqiyo-wabo ezqen.
Qjef jeo’gu kobvoap hdal e pukbiwso ylha lihv yitliih i mir-popc haqeu, Zivhuj enxoht !!. Ckun evedijif okhogc goi ya icyewb mko pidoetbe oy i def-yuvv ffvo ejl hehcuov gxe ?. ijunoqos.
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.