Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Reflection) Implicit and explicit lambda declaration

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?

like image 319
LiTTle Avatar asked Mar 07 '18 08:03

LiTTle


People also ask

What is the difference between explicit declaration and implicit declaration?

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.

How do I turn off explicit declaration in Visual Studio?

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.

What is the explicit declaration switch in Visual Basic?

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:

What is the scope of an explicit declaration of a name?

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.


1 Answers

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.

like image 84
yole Avatar answered Oct 18 '22 21:10

yole