Sunday, September 26, 2021

Kotlin tips and tricks...


Read more: official reference and meta programming

Optional parameters for class:

class yuh(

    optionalParameter: String = "",


If optionalParameter is not passed, it equals to "" by default.

How to pass a class as a parameter

val c = MyClass::class


How to get a class of an object

var classOfMyObject = myObject!!

(yep, in Kotlin .java is to be added for some reason, at least that option works for me, even though the class of the variable is a Kotlin class not the Java one)

Ternary operator


if(expressionAInBrackets) expressionB else expressionC


var v = if (a) b else c

Kotlin operators to manage nullable values and types: ?, ??, !, !! 

No comments:

Post a Comment