본문으로 바로가기

1. Kotlin에서 자주 사용되는 용어

category Kotlin/Idioms 2018. 5. 31. 17:37

1. Creating DTOs(POJOs/POCOs)

2. Default values for function parameters

3. Filtering a list

4. String Interpolation

5. Instance Checks

6. Traversing a map/list of pairs

7. Using ranges

8. Read-only list

9. Read-only map

10. Accessing a map

11. Lazy property

12. Extensions Functions

13. Create a Singleton

14. If not null shorthand

15. If not null and else shorthand

16. Executing a statement if null

17. Get first item of a possibly empty collection

18. Execute if not null

19. Map nullable value if not null

20. Return on when statement

21. try/catch expression

22. if expression

23. Builder-style usage of methods that return Unit

24. Single-expression functions

25. Calling multiple methods on an object instance('with')

 

class Turtle { fun penDown() fun penUp() fun turn(degrees: Double) fun forward(pixels: Double) } val myTurtle = Turtle() with(myTurtle) { //draw a 100 pix square penDown() for(i in 1..4) { forward(100.0) turn(90.0) } penUp() }



26. Java 7's try with resources

val stream = Files.newInputStream(Paths.get("/some/file.txt"))

stream.buffered().reader().use { reader -> println(reader.readText()) }


27. Convenient form for a generic function that requires the generic type information


// 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) 



28. Consuming a nullable Boolean


val
b: Boolean? = ... if (b == true) { ... } else { // `b` is false or null 

}