티스토리 뷰

728x90
반응형

Kotlin 의 특수 기호




?.


safe call 을 수행한다

메소드가 호출되거나 수신기가 null 이 아닌 경우 속성에 액세스



?.  을 이용한 null safety



if ( user != null )

println("user is not null")

else

user = false



위의 코드를 ?.  을 이용하면


user?.let { println("user is not null") }.let { println("user is null") }


위와 같이 표현 할 수 있다




?:


왼쪽의 값이 null 인 경우 오른쪽 값을 가져온다 ( 엘비스 연산자 )



Elvis Operator



val l: Int = if( b != null ) b.length else -1



val l = b?.length ?: -1


처럼 표현 할 수 있다.


나아가


fun foo(node: Node): String? {

val parent = node.getParent() ?: return null

val name = node.getName() ?:  thorw IllegalArgumentException("name expected")

}


처럼 throw 와 return 을 활용 할 수도 있다



::


멤버 참조 또는 클래스 참조를 만든다



@


주석을 소개한다

loop label 을 소개하거나 참조한다

lambda label 을 소개하거나 참조한다

바깥 쪽 범위에서 this 표현식을 참조한다

외부 수퍼 클래스를 참조한다




+ References


< https://kotlinlang.org/docs/reference/keyword-reference.html >

반응형

'프로그래밍 > Kotlin' 카테고리의 다른 글

[Kotlin] primary constructor call expected  (0) 2018.08.27
[Kotlin] lateinit var is initialized  (0) 2018.08.25
[Kotlin] lateinit vs lazy  (0) 2018.08.23
[Kotlin] 배열에서 초기화  (0) 2018.08.10
[Kotlin] Convert a char to int  (0) 2018.08.10
공지사항
최근에 올라온 글