์ค๋ ์์๋ kotlin ๊ด์ฉ๊ตฌ์ด๋ค.
Creating DTOs (POJOs/POCOs)
1 | data class Customer(val name: String, val email: String) |
kotlin์ ๋ฐ์ดํฐ ํด๋์ค๋ ์๋์ผ๋ก getter์ (val์ ์ฝ๊ธฐ์ ์ฉ์ด๋ผ val์ ๊ฒฝ์ฐ๋ง setter ์ง์) ๊ธฐํ ์๋ฐ์์ ์ฌ์ ์ํด์ ์จ์ผํ๋ ๊ฒ๋ค(equals, hashCode, toString, copy๋ฑ)์ ์๋์ผ๋ก ์ง์ํด์ค๋ค.(๋๋ถ์ ์๋ฐ์ ๋ฌ๋ฆฌ ๊ท์ฐฎ์ ๊ฒ๋ค์ ๋์ดํ๋ฏ ์ฐ์ง ์์๋ ๋๋ค.)
Default values for function parameters
1 | fun foo(a: Int = 0, b: String = "") { ... } |
ํ๋ผ๋ฏธํฐ์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ๋ ค๋ฉด ์์๊ฐ์ด ์ ์ธ๊ณผ ๋์์ ๊ฐ์ ์ค์ ํ๋ฉด ๋๋ค.
Filtering a list
๋ฆฌ์คํธ์์ ํํฐ๋งํ๋ ๋ฐฉ๋ฒ์ ๋๊ฐ์ง๊ฐ ์๋ค.
1 | val positives = list.filter { x -> x > 0 } |
๋๋
1 | val positives = list.filter { it > 0 } |
์ด๋ ๊ฒ ํ๋ฉด 0์ด์(์์)์ ๊ฐ๋ง ํํฐ๋ง ๋๊ฒ ๋๋ค.
String Interpolation
String ์์์๋ ๋ณ์๋ฐ ํจ์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ฐ ์์ $๋ฅผ ๋ถํ์ฃผ๋ฉด ๋๋ค.1 | println("Name $name")
|
Instance Checks
์๋ฐ์ switch~case ๋ฌธ๊ณผ ์ ์ฌํ์ง๋ง ์ฌ๋ฌ ํ์ ์ check ๊ฐ๋ฅํ when์ ์ฌ์ฉ๋ฒ์ ์๋์ ๊ฐ๋ค.1 2 3 4 5 | when (x) { is Foo -> ... is Bar -> ... else -> ... } |
k , v ์ ์๋ฌด ์ด๋ฆ์ด๋ ๋ถ์ฌ๋ ๋๋ค.
Using ranges
๋ฒ์ ๋ฌธ๋ฒ์ ์ฌ์ฉ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค.1 2 3 4 5 | for (i in 1..100) { ... } // 100์ ํฌํจํ๋ค. for (i in 1 until 100) { ... } // 100์ ํฌํจํ์ง ์๋๋ค. for (x in 2..10 step 2) { ... } for (x in 10 downTo 1) { ... } if (x in 1..10) { ... } |
Read-only list
๋ฆฌ์คํธ๋ฅผ val๋ก ์ ์ธํ๋ฉด ์ฝ๊ธฐ ์ ์ฉ์ด ๋๋ค.
1 | val list = listOf("a", "b", "c") |
Read-only map
๋ฆฌ์คํธ์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋งต๋ val๋ก ์ ์ธํด ์ฝ๊ธฐ์ ์ฉ์ผ๋ก ๋ง๋ค ์ ์๋ค.1 | val map = mapOf("a" to 1, "b" to 2, "c" to 3) |
Accessing a map
๋งต์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค.1 2 | println(map["key"]) map["key"] = value |
Lazy property
์ง์ฐ ํ๋กํผํฐ๋ ํธ์ถ ์์ ์ ์ด๊ธฐํ๊ฐ ๋๋๊ฒ์ ๋งํ๋ค.
1 2 3 | val p: String by lazy { // compute the string } |
์ง์ฐ๋ก๋ฉ์ ์ฌ๋ฌ ์ด์ ์ด ์์ผ๋ ๊ทธ๊ฒ์ ๋์ค์ ์ค๋ช ํ๋๋ก ํ๊ฒ ๋ค.
Extension Functions
์ฝํ๋ฆฐ์์๋ ์ด๋ฏธ ์๋ ํด๋์ค์ ์ถ๊ฐ๋ก (ํ์ฅ) ํจ์ ์ค์ ์ด ๊ฐ๋ฅํ๋ค.
1 2 3 | fun String.spaceToCamelCase() { ... } "Convert this to camelcase".spaceToCamelCase() |
Creating a singleton
์ฝํ๋ฆฐ์์๋ ์ฑ๊ธํค์ ๋ง๋ค๊ธฐ ์ํด์๋ objectํด๋์ค๋ง ๋ถํ์ฃผ๋ฉด ๋๋ค.(์๋ฐ๋.....)
1 2 3 | object Resource { val name = "Name" } |
If not null shorthand
1 2 3 | val files = File("Test").listFiles() println(files?.size) |
files๊ฐ null์ผ๊ฒฝ์ฐ NPE๊ฐ ๋ฐ์ํ์ง ์๊ณ null์ ๋ฐํํ๋ค.
๊ฒฐ๊ณผ๊ฐ: null
If not null and else shorthand
1 2 3 | val files = File("Test").listFiles() println(files?.size ?: "empty") |
์ด ๊ฒฝ์ฐ files๊ฐ null์ด๋ผ๋ฉด ๊ธฐ๋ณธ๊ฐ "empty"๊ฐ ์ถ๋ ฅ๋๋ค.
๊ฒฐ๊ณผ๊ฐ: empty
Executing a statement if null
1 2 | val values = ... val email = values["email"] ?: throw IllegalStateException("Email is missing!") |
์ด ๊ฒฝ์ฐ๋ ๋ง์ผ values["email"]์ด null ์ด๋ฉด IllgalStateExcption์ ๋ฐ์์ํจ๋ค.
๊ฒฐ๊ณผ๊ฐ: Exception in thread "main" java.lang.IllegalStateException: name is missing
Execute if not null
1 2 3 4 5 | val value = ... value?.let { ... // execute this block if not null } |
value๊ฐ null์ด ์๋๋ผ๋ฉด let๋ธ๋ญ์ด ์คํ๋๋ค.
Map nullable value if not null
1 2 3 | val value = ... val mapped = value?.let { transformValue(it) } ?: defaultValueIfValueIsNull |
value๊ฐ null์ด ์๋๋ฉด let๋ธ๋ก์ด ์คํ๋๊ณ , null ์ผ ๊ฒฝ์ฐ defaultValueIfValueIsNull์ด mapped์ ๊ฐ์ด๋๋ค.
Return on when statement
1 2 3 4 5 6 7 8 | fun transform(color: String): Int { return when (color) { "Red" -> 0 "Green" -> 1 "Blue" -> 2 else -> throw IllegalArgumentException("Invalid color param value") } } |
when์ return์ ๋ฐ๋ก ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
'try/catch' expression
1 2 3 4 5 6 7 8 9 | fun test() { val result = try { count() } catch (e: ArithmeticException) { throw IllegalStateException(e) } // Working with result } |
try-catch๋ ์๋ฐ์ ๋น์ทํ๊ฑฐ ๊ฐ๋ค.
'if' expression
1 2 3 4 5 6 7 8 9 | fun foo(param: Int) { val result = if (param == 1) { "one" } else if (param == 2) { "two" } else { "three" } } |
Builder-style usage of methods that return Unit
1 2 3 | fun arrayOfMinusOnes(size: Int): IntArray { return IntArray(size).apply { fill(-1) } } |
Unit์ ๋ฆฌํดํด Builder์คํ์ผ์ ์ฌ์ฉํ ์๋ ์๋ค.
Single-expression functions
1 | fun theAnswer() = 42 |
์์ ์๊ณผ ์๋์ ์์ ๋์ผํ๋ค.
1 2 3 | fun theAnswer(): Int { return 42 } |
์ด๋ฐ์์ ํํ๋ฒ์ when๊ณผ ํจ๊ป ์ ์ฉํ๊ฒ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
1 2 3 4 5 6 | fun transform(color: String): Int = when (color) { "Red" -> 0 "Green" -> 1 "Blue" -> 2 else -> throw IllegalArgumentException("Invalid color param value") } |
Calling multiple methods on an object instance ('with')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Turtle { fun penDown() fun penUp() fun turn(degrees: Double) fun forward(pixels: Double) } val myTurtle = Turtle() with(myTurtle) { //100pix ์ฌ๊ฐํ ๊ทธ๋ฆฌ๊ธฐ penDown() for(i in 1..4) { forward(100.0) turn(90.0) } penUp() } |
with๋ฅผ ์ด์ฉํด ์ฐ์์ ์ธ ์์ ์ ํ๋ฒ์ ํ ์ ์๋ค.
Java 7's try with resources
1 2 3 4 | val stream = Files.newInputStream(Paths.get("/some/file.txt")) stream.buffered().reader().use { reader -> println(reader.readText()) } |
java7์ ์์ ์๋ ํด์ ๋ฅผ ์์ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
Convenient form for a generic function that requires the generic type information
1 2 3 4 5 6 | // public final class Gson { // ... // public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException { // ... inline fun <reified T: Any> Gson.fromJson(json: JsonElement): T = this.fromJson(json, T::class.java) |
์ด๋ถ๋ถ์ ์ ๋ชจ๋ฅด๊ฒ ์ผ๋ ๋์ถฉ ์ ๋๋ฆญ์ ๋ง๋๋ ํ์์ธ๊ฑฐ ๊ฐ๋ค.
Consuming a nullable Boolean
1 2 3 4 5 6 | val b: Boolean? = ... if (b == true) { ... } else { // `b` is false or null } |
nullableํ Boolean๊ฐ ์ฌ์ฉํ๊ธฐ.
์ ๋ฆฌํ๊ณ ๋ณด๋ ์๋ฐ๋ณด๋ค ํธ๋ฆฌํ ๊ธฐ๋ฅ๋ค์ด ๋ง์๊ฒ ๊ฐ๋ค.
์์ ๊ฐ ์๋ก ์ฝํ๋ฆฐ์ ๋งค๋ ฅ์ ์ธ ์ธ์ด์์ด ๋๊ปด์ง๋ค.ใ ใ
์ ๋ฆฌํ๊ณ ๋ณด๋ ์๋ฐ๋ณด๋ค ํธ๋ฆฌํ ๊ธฐ๋ฅ๋ค์ด ๋ง์๊ฒ ๊ฐ๋ค.
์์ ๊ฐ ์๋ก ์ฝํ๋ฆฐ์ ๋งค๋ ฅ์ ์ธ ์ธ์ด์์ด ๋๊ปด์ง๋ค.ใ ใ
0 ๊ฐ์ ๋๊ธ:
Post a Comment