Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin 의 @JvmMultifileClass 어노테이션 #154

Open
occidere opened this issue Dec 6, 2020 · 0 comments
Open

Kotlin 의 @JvmMultifileClass 어노테이션 #154

occidere opened this issue Dec 6, 2020 · 0 comments
Assignees

Comments

@occidere
Copy link
Owner

occidere commented Dec 6, 2020

@JvmMultifileClass

@JvmName 과 함께 사용되며, top-level 함수가 여러 파일이 걸쳐 선언되어 있을 때 마치 하나의 파일에 선언되어 있는 것 처럼 사용하고 싶을 때 사용한다.

Example

예를들어 아래의 2개 파일이 각각 존재한다.

/* 파일 1: StringPrintUtils.kt */
fun printAllStrings(list: List<String>) {
    println(list)
}

/* 파일 2: IntPrintUtils.kt */
fun printAllInts(list: List<Int>) {
    println(list)
}

이 때, 두 파일 내 함수들에 접근 시 각 파일명이 아닌, Utils 와 같은 동일한 이름으로 접근하고 싶다고 하자.

public static void main(String[] args) {
	/* AS-IS */
	IntPrintUtilsKt.printAllInts(Arrays.asList(1, 2, 3)); // ok
	StringPrintUtilsKt.printAllStrings(Arrays.asList("a", "b", "c")); // ok
	
	/* TO-BE */
	Utils.printAllInts(Arrays.asList(1, 2, 3)); // compile error
	Utils.printAllStrings(Arrays.asList("a", "b", "c")); // compile error
}

이와 같은 경우 각 파일에 @file:JvmName, @file:JvmMultifileClass 어노테이션을 붙여주면 가능하다.

/* 파일 1: StringPrintUtils */
@file:JvmName("Utils")
@file:JvmMultifileClass

fun printAllStrings(list: List<String>) {
    println(list)
}

/* 파일 2: IntPrintUtils */
@file:JvmName("Utils")
@file:JvmMultifileClass

fun printAllInts(list: List<Int>) {
    println(list)
}

이후 원하는 방식대로 사용할 수 있다.

public static void main(String[] args) {
	/* AS-IS */
	IntPrintUtilsKt.printAllInts(Arrays.asList(1, 2, 3)); // compile-error
	StringPrintUtilsKt.printAllStrings(Arrays.asList("a", "b", "c")); // compile-error

	/* TO-BE */
	Utils.printAllInts(Arrays.asList(1, 2, 3)); // ok
	Utils.printAllStrings(Arrays.asList("a", "b", "c")); // ok
}

참고

@occidere occidere self-assigned this Dec 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant