I am trying to understand the reflection. I have the following code:
fun main(args: Array) {
println(lengthL1())
println(lengthL2(s))
println(lengthL1.get()) // Error
println(lengthL2.get(s)) // Error
println(lengthNL1.get())
println(lengthNL2.get(s))
println(lengthNL1())
println(lengthNL2(s))
}
val s = “1234”
val lengthL1: () -> Int = s::length
val lengthL2: (String) -> Int = String::length
val lengthNL1 = s::length
val lengthNL2 = String::length
Why I cannot call the get
(See Error comments) when I declare the lambda?
Is there any difference between lengthL1
and lenghtNL1
?
Explicit means declaring variable like in c. An implicit declaration is when you make a variable directly without order it first. What do you mean by explicit declaration? An explicit declaration is the appearance of an identifier (a name) in a DECLARE statement, as a label prefix, or in a parameter list.
Explicit Declaration Switch. You can set the explicit declaration switch On or Off in any of the following ways: Set the appropriate project property in the integrated development environment (IDE). Click <ProjectName> Properties from the Project menu, and then click the Compile tab.
Visual Basic provides a switch that controls explicit declaration. By default, this switch is set to On, and the compiler enforces explicit declaration. If you turn this switch Off, you can use variables without declaring them. Explicit Declaration Switch. You can set the explicit declaration switch On or Off in any of the following ways:
The scope of an explicit declaration of a name is the block containing the declaration. This includes all contained blocks, except those blocks (and any blocks contained within them) to which another explicit declaration of the same name is internal. In the following diagram, the lines indicate the scope of the declaration of the names.
s::length
is a property reference, which is an object of type KProperty1. The get
method is defined as a member of this type.
If you declare a variable of a lambda type and initialize it with a property reference, you get a regular lambda (KFunction1
). The KFunction1
interface declares only the invoke()
method, allowing you to call it as lengthL1()
, but it does not declare any additional methods such as get
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With