1. ๋ฐ๋ ์ธ์๊ฐ ์๊ณ , ๋ฐํ ํ์ ์ด ์๋ ๋๋ค
์ฝํ๋ฆฐ ์ฝ๋
fun runLambda(lambda: () -> Unit) {
lambda()
}
fun main() {
runLambda { println("HelloWord") }
}
// ์ถ๋ ฅ ๊ฒฐ๊ณผ : HelloWord
์๋ฐ ๋ณํ
public final class MainKt {
public static final void main() {
runLambda((Function0)null.INSTANCE);
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
public static final void runLambda(@NotNull Function0 lambda) {
Intrinsics.checkNotNullParameter(lambda, "lambda");
lambda.invoke();
}
}
๋ฐ๋ ์ธ์๊ฐ ์๊ณ , ๋ฐํ ํ์ ์ด ์๋ ๋๋ค๋ Function0 ์ธํฐํ์ด์ค๊ฐ ๊ตฌํ๋ ํด๋์ค๋ก ๋ณํ๋๋ค.
Function0 ์ธํฐํ์ด์ค
// Functions.kt ์
package kotlin.jvm.functions
/** A function that takes 0 arguments. */
public interface Function0<out R> : Function<R> {
/** Invokes the function. */
public operator fun invoke(): R
}
2 ์ธ๋ถ ์ค์ฝํ์ value ์ฐธ์กฐํ๋ ๋๋ค
์ฝํ๋ฆฐ ์ฝ๋
fun runLambda(lambda: () -> Unit) {
lambda()
}
fun main() {
val sayHello = "Say Hello"
runLambda {
println(sayHello)
}
}
// ์ถ๋ ฅ ๊ฒฐ๊ณผ : Say Hello
์๋ฐ ๋ณํ
public final class MainKt {
public static final void main() {
final String sayHello = "Say Hello";
runLambda((Function0)(new Function0() {
// $FF: synthetic method
// $FF: bridge method
public Object invoke() {
this.invoke();
return Unit.INSTANCE;
}
public final void invoke() {
String var1 = sayHello;
System.out.println(var1);
}
}));
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
public static final void runLambda(@NotNull Function0 lambda) {
Intrinsics.checkNotNullParameter(lambda, "lambda");
lambda.invoke();
}
}
value์ ์ฐธ์กฐ๋ฅผ Function0 ํด๋์ค ์์์ ์ฌ์ฉํ๋ค.
2 ์ธ๋ถ ์ค์ฝํ์ variable์ ์ฐธ์กฐํ๋ ๋๋ค
์ฝํ๋ฆฐ ์ฝ๋
fun main() {
var sayHi = "Say Hi"
val myLambda = {
println(sayHi)
}
sayHi = "Good night"
myLambda()
}
//์ถ๋ ฅ ๊ฒฐ๊ณผ : Good night
์๋ฐ ๋ณํ
public final class MainKt {
public static final void main() {
final Ref.ObjectRef sayHi = new Ref.ObjectRef();
sayHi.element = "Say Hi";
Function0 myLambda = (Function0)(new Function0() {
// $FF: synthetic method
// $FF: bridge method
public Object invoke() {
this.invoke();
return Unit.INSTANCE;
}
public final void invoke() {
String var1 = (String)sayHi.element;
System.out.println(var1);
}
});
sayHi.element = "Good night";
myLambda.invoke();
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
}
variable์ Ref ํด๋์ค๋ก ๋ํํ๋ค.
variable์ด String์ด๋ฏ๋ก, Ref.ObjectRef ํด๋์ค์ element ๊ฐ์ผ๋ก variable์ด ์ ์ฅ๋์ด ๋ํ๋๋ค.
๋๋ค ๋ฐ๊นฅ์ varialbe์ ์บก์ณํ ๋๋ค๊ฐ ๋จผ์ ์ ์ธ๋์ด ์์ด๋, ๋๋ค ์คํ ์ ์ฅ variable์ด ์์ ๋๋ฉด Ref.ObjectRef ํด๋์ค์ element๋ก ๊ฐ์ ๋ณ๊ฒฝํด์ ๋๋ค ์คํ ์ ๋ณ๊ฒฝ๋ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
Ref ํด๋์ค
//Ref.java ํ์ผ ์
package kotlin.jvm.internal;
import java.io.Serializable;
public class Ref {
private Ref() {}
public static final class ObjectRef<T> implements Serializable {
public T element;
@Override
public String toString() {
return String.valueOf(element);
}
}
public static final class ByteRef implements Serializable {
public byte element;
@Override
public String toString() {
return String.valueOf(element);
}
}
... // Short, Long, Int, Boolean, Double, Float, Char์ฉ ๋ํผ ํด๋์ค๋ค์ด ์ ์ธ๋์ด ์๋ค.
}
3 ์ธ์๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๋๋ค, ๋ฐํ ํ์ ์ด ์๋ ๋๋ค ๋ฑ
์ฝํ๋ฆฐ ์ฝ๋
fun main() {
val lambdaWithParameterOne = { it: String ->
println("lambdaWithParameterOne $it")
}
lambdaWithParameterOne("Hello Kotlin")
val lambdaWithParameterTwo = { one: String, two: String ->
println("lambdaWithParameterTwo $one, $two")
}
lambdaWithParameterTwo("Hi", "Good Luck")
val lambdaWithReturn = {
"Return Hello"
}
println(
lambdaWithReturn()
)
val lambdaWithParameterAndReturn = { it: String ->
"$it Kotlin"
}
println(
lambdaWithParameterAndReturn("Hi")
)
}
// ์ถ๋ ฅ ๊ฒฐ๊ณผ :
// lambdaWithParameterOne Hello Kotlin
// lambdaWithParameterTwo Hi, Good Luck
// Return Hello
// Hi Kotlin
์๋ฐ ๋ณํ
public final class MainKt {
public static final void main() {
Function1 lambdaWithParameterOne = (Function1)null.INSTANCE;
lambdaWithParameterOne.invoke("Hello Kotlin");
Function2 lambdaWithParameterTwo = (Function2)null.INSTANCE;
lambdaWithParameterTwo.invoke("Hi", "Good Luck");
Function0 lambdaWithReturn = (Function0)null.INSTANCE;
Object var3 = lambdaWithReturn.invoke();
System.out.println(var3);
Function1 lambdaWithParameterAndReturn = (Function1)null.INSTANCE;
Object var4 = lambdaWithParameterAndReturn.invoke("Hi");
System.out.println(var4);
}
}
์ธ์๊ฐ ํ ๊ฐ์ด๊ณ ๋ฐํ ๊ฐ์ด ์๋ ๋๋ค -> Function1 ํด๋์ค๋ก ๋ณํ
์ธ์๊ฐ ๋ ๊ฐ์ด๊ณ ๋ฐํ ๊ฐ์ด ์๋ ๋๋ค -> Function2 ํด๋์ค๋ก ๋ณํ
์ธ์๊ฐ ์๊ณ ๋ฐํ ๊ฐ์ด ์๋ ๋๋ค -> Functoin0 ํด๋์ค๋ก ๋ณํ
์ธ์๊ฐ ํ ๊ฐ์ ๋ฐํ ๊ฐ์ด ์๋ ๋๋ค -> Function1() ํด๋์ค๋ก ๋ณํ
Functions.kt ํ์ผ ์
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
// Auto-generated file. DO NOT EDIT!
package kotlin.jvm.functions
/** A function that takes 0 arguments. */
public interface Function0<out R> : Function<R> {
/** Invokes the function. */
public operator fun invoke(): R
}
/** A function that takes 1 argument. */
public interface Function1<in P1, out R> : Function<R> {
/** Invokes the function with the specified argument. */
public operator fun invoke(p1: P1): R
}
/** A function that takes 2 arguments. */
public interface Function2<in P1, in P2, out R> : Function<R> {
/** Invokes the function with the specified arguments. */
public operator fun invoke(p1: P1, p2: P2): R
}
/** A function that takes 3 arguments. */
public interface Function3<in P1, in P2, in P3, out R> : Function<R> {
/** Invokes the function with the specified arguments. */
public operator fun invoke(p1: P1, p2: P2, p3: P3): R
}
...
/** A function that takes 22 arguments. */
public interface Function22<in P1, in P2, in P3, in P4, in P5, in P6, in P7, in P8, in P9, in P10, in P11, in P12, in P13, in P14, in P15, in P16, in P17, in P18, in P19, in P20, in P21, in P22, out R> : Function<R> {
/** Invokes the function with the specified arguments. */
public operator fun invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18, p19: P19, p20: P20, p21: P21, p22: P22): R
}
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Secrets Gradle Plugin for Android (1) | 2024.11.28 |
---|---|
[Android][View] getWidth(), getHeight() ์ measureWidth(), measuredHeight() (1) | 2024.11.13 |
[Kotlin] init ๋ธ๋ก (3) | 2024.11.04 |
[JVM] invokedynamic ์ดํดํ๊ธฐ (0) | 2024.11.01 |
[Kotlin] interface ์ default method๋ฅผ ์๋ฐ๋ก ๋ณํํ๊ธฐ (2) | 2024.10.29 |