What Does "::" Mean in Kotlin

What is the Kotlin double-bang (!!) operator?

This is unsafe nullable type (T?) conversion to a non-nullable type (T),
!! will throw NullPointerException if the value is null.

It is documented here along with Kotlin means of null-safety.

what does :: mean in kotlin?

As stated in the docs this is a class reference:

Class References:
The most basic reflection feature is getting the runtime reference to a Kotlin class. To obtain the reference to a statically known Kotlin class, you can use the class literal syntax:

val c = MyClass::class
//The reference is a value of type KClass.

Note that a Kotlin class reference is not the same as a Java class reference. To obtain a Java class reference, use the .java property on a KClass instance.

It’s also the syntax for method references as in this simple example:

list.forEach(::println)

It refers to println defined in Kotlin Standard library.

What does ?. mean in Kotlin when used on the left side of an assignment?

It means that if one of the safe calls on the left-hand side fails (i.e. its receiver is null), then the whole assignment is skipped, and the expression on the right-hand side is not evaluated at all.

val nullable: Container? = null
nullable?.x = f() // f is not called

(runnable demo)

What does the !! operator mean in Kotlin?

This is used to convert an expression to non-null and throw a KotlinNullPointerException if the result is null. So in this usage, it will only save the response's body was not null, otherwise an exception will be thrown.

See here for more information.

What does the arrow (-) operator do in Kotlin?

The -> is part of Kotlin's syntax (similar to Java's lambda expressions syntax) and can be used in 3 contexts:

  • when expressions where it separates "matching/condition" part from "result/execution" block

     val greet = when(args[0]) {
    "Apple", "Orange" -> "fruit"
    is Number -> "How many?"
    else -> "hi!"
    }
  • lambda expressions where it separates parameters from function body

      val lambda = { a:String -> "hi!" }
    items.filter { element -> element == "search" }
  • function types where it separates parameters types from result type e.g. comparator

      fun <T> sort(comparator:(T,T) -> Int){
    }

Details about Kotlin grammar are in the documentation in particular:

  • functionType
  • functionLiteral
  • whenEntry

What does .() mean in Kotlin?

A function that takes in nothing and returns nothing in Kotlin looks like:

var function : () -> Unit

The difference is that the function in your code takes in nothing, returns nothing, but is invoked on an object.

For example,

class Builder (val multiplier: Int) {

fun invokeStuff(action: (Builder.() -> Unit)) {
this.action()
}

fun multiply(value: Int) : Int {
return value * multiplier
}
}

The important bit here is the way we've declared the type of action

action: (Builder.() -> Unit)

This is a function that returns nothing, takes in nothing but is invoked on an object of type Builder.

This means when we use this builder like so

var builder = Builder(10)
builder.invokeStuff({
var result = multiply(1)
println(result)
})

The context of this has been set to the builder object and we can invoke functions declared within the builder.

Refer more here.

what does ?? mean in Kotlin

The null safety operator is defined as '?' in kotlin. If you are talking about null-aware operator in dart then in that case two question marks is used. The null-awareness in kotlin is achieved making null-safe to variable as

val testString:String?

In dart (or any other language) this is called for the null check as

x=y ?? z

meaning, assing y to x until y is not null, else z



Related Topics



Leave a reply



Submit