-
Notifications
You must be signed in to change notification settings - Fork 1
/
InlineClass.kt
55 lines (43 loc) · 1.39 KB
/
InlineClass.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// since Kotlin 1.3, it is impossible to call functions which accept inline classes from Java.
class InlineClass {
fun test() {
println("Inline Classes")
val martin = Name("Martin")
martin.greet()
println(martin.length)
println("")
println("Representation")
asInline(martin)
asGeneric(martin)
asInterface(martin)
asNullable(martin)
println(id(martin))
println("")
println("Inline classes vs type aliases")
val nameAlias: NameTypeAlias = ""
val nameInlineClass: NameInlineClass = NameInlineClass("")
val s: String = ""
acceptString(nameAlias)
// acceptString(nameInlineClass) // not allow
acceptNameTypeAlias(s)
// acceptNameInlineClass(s) // not allow
}
private fun asInline(n: Name) {}
private fun <T> asGeneric(t: T) {}
private fun asInterface(i: I) {}
private fun asNullable(n: Name?) {}
private fun <T> id(t: T): T = t
private fun acceptString(s: String) {}
private fun acceptNameTypeAlias(n: NameTypeAlias) {}
private fun acceptNameInlineClass(n: NameInlineClass) {}
}
interface I
inline class Name(private val s: String) : I {
val length: Int
get() = s.length
fun greet() {
println("Hello, $s")
}
}
inline class NameInlineClass(val s: String)
typealias NameTypeAlias = String