what does “::” mean in kotlin?

前端 未结 4 1531
灰色年华
灰色年华 2020-12-04 18:13

I\'m new to Kotlin
I used this code for opening another activity:

startActivity(Intent(this,IntroAndLang::class.java))

current activity

相关标签:
4条回答
  • 2020-12-04 18:22

    Since kotlin 1.1, in addition to class, function, property and constructor references as stated above, '::' can also be used to obtain the bound references to all of the above.

    For instance, using '::class' could be used to get the exact class of a particular object despite the type of the receiver as below...

    val widget: Widget = ...
    assert(widget is GoodWidget) { "Bad widget: ${widget::class.qualifiedName}" }
    

    widget::class returns the exact class of the object 'widget' as either 'GoodWidget' or 'BadWidget' despite the type of the receiver expression (i.e 'Widget' as declared initially)

    More info at https://kotlinlang.org/docs/reference/reflection.html

    0 讨论(0)
  • 2020-12-04 18:29

    :: converts a kotlin function into a lambda.

    lets say you have a function that looks like this:

    fun printSquare(a:Int) = println(a *2)
    

    now lets say you have a class that takes a lambda as a 2nd argument:

    class MyClass(var someOtherVar:Any,var printSquare:(Int) -> Unit){
    
            fun doTheSquare(i:Int){
                printSquare(i)
            }
        }
    

    Now how do you pass the printSquare function into MyClass ??? if you try the following it wont work :

      MyClass("someObject",printSquare) //printSquare is not a LAMBDA, its a function so it gives compile error of wrong argument
    

    so how do we CONVERT printSquare into a lambda so we can pass it around ? use the :: notation.

    MyClass("someObject",::printSquare) //now compiler does not compaint since its expecting a lambda and we have indeed converted printSquare FUNCTION into a LAMBDA. 
    

    also please note that this is implied... meaning this::printSquare is the same as ::printSquare. so if printSquare function was say in another class like a presenter, then you could convert it to lamba like this:

    presenter::printSquare
    

    UPDATE:

    Also this works with constructors. if you want to create the constructor of an object and then convert it to a lambda it is done like this:

    (x,y)->MyObject::new
    

    this translates to MyObject(x,y) in kotlin.

    0 讨论(0)
  • 2020-12-04 18:39

    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.

    0 讨论(0)
  • 2020-12-04 18:39

    :: is used for Reflection in kotlin

    1. Class Reference val myClass = MyClass::class
    2. Function Reference this::isEmpty
    3. Property Reference ::someVal.isInitialized
    4. Constructor Reference ::MyClass

    For detailed reading Official Documentation

    0 讨论(0)
提交回复
热议问题