레이블이 kotlin인 게시물을 표시합니다. 모든 게시물 표시
레이블이 kotlin인 게시물을 표시합니다. 모든 게시물 표시

2019년 11월 25일 월요일

The Mystery of Mutable Kotlin Collections 를 읽고

https://proandroiddev.com/the-mystery-of-mutable-kotlin-collections-e82cbf5d781

Kotlin에서 MutableList는 List는 아래와 같다.

public interface Mutable:ist<E> : List<E>, MutableCollection<E> {}
public interface List<out E> : Collection<E> {}

List: read-only access
MutableList: read/write access

- MutableListOf(...)나 listOf(...)를 통해서 만들어진 리스트는 MutableList로 인식된다.
- List 인터페이스를 Kotlin으로 직접 구현하면 MutableList로 인식하지 않는다.

위처럼 되는 이유는?
Kotlin에서의 List는 mock interface이기 때문이다.
Kotlin에서의 List는 컴파일시 사라지고 Java에서의 List로 변환된다.

2019년 11월 22일 금요일

Public API challenges in Kotlin 요약

https://jakewharton.com/public-api-challenges-in-kotlin/

코틀린의 data class 만이들 사용하실 겁니다. 자바에서 일일이 코딩해야 되는 내용을 매우 간단하게 만들어 주니까요. 그런데
이 data class를 쓰는 경우 바이너리 호환에 주의해야 합니다. 클래스의 내부 필드에 변경이 있는 경우 바이너리 호환이 안될
수가 있기 때문입니다.

1. 새로운 필드를 중간에 추가하는 경우 코틀린에서 제공하는 ComponentN()
함수가 맞지 않게 됩니다. 따라서, 새로운 필드를 추가하는 경우에는 항상 마지막에 추가해야 합니다.
2. data class의 경우 copy 함수를 자동으로 생성해 줍니다. 그런데 필드가 새로 추가되는 경우 copy()의 signature가 바뀌어
버리는 문제가 발생할 수 있습니다. 결국 바이너리 호환이 필요한 경우라면 data class를 쓰기보다는 직접 관련 함수 구현을 다
해주는 것이 좋겠습니다.
3. class 생성시 자유도를 주기위해 builder 패턴을 많이 사용합니다. 이 경우 필드에 @set:JvmSynthetic 를 사용해서 get은
노출하지만 set은 노출하지 않을 수 있습니다.
코틀린의 경우에는 top-level function이나 DSL을 통해 인스턴스를 생성하는 방법을 많이 사용합니다. 이 경우 자바에서는
바이너리 호환 문제가 발생할 수 있으므로 @JvmSynthetic을 사용해서 자바쪽에 노출이 되지 않도록 해줍니다.

2018년 1월 16일 화요일

Kotlin 스터디

KotlinConf 2017

* Introduction to Coroutines *

https://resources.jetbrains.com/storage/products/kotlinconf2017/slides/2017+KotlinConf+-+Introduction+to+Coroutines.pdf

- Synchronous
- Asynchronous

-> Callback

- Futures/Promises/Rx

-> returns promise for a future result immediately

- Coroutines

-> suspend: returns result when received
-> like regular code

- Coroutines are like very light-weight threads

http://github.com/kotlin/kotlinx-coroutines

* Deep Dive into Coroutines on JVM *

https://resources.jetbrains.com/storage/products/kotlinconf2017/slides/2017+KotlinConf+-+Deep+dive+into+Coroutines+on+JVM.pdf

- Continuation Passing Style(CPS)

-> Callback

- State Machine

- Communicating Sequential Processes(CSP)

* Kotlin Types: Exposed *

Int vs Int?

IntArray vs Array<Int>

Any vs Object

Boxing under the hood vs No boxing

Unit vs Nothing vs void

Unit

-> a type that allows only one value and thus can hold no information
-> the function completes successfully

Nothing :

-> a type that has no values
-> It means "this function never returns"

- Unit과 Nothing은 Kotlin에서 위와 같은 차이가 있지만 JVM에서 똑같이 void로 컴파일된다.

Nothing?

@Nullable -> Type? and @NotNull(@NonNull) -> Type

How to still prevent NPEs? - 자바 코드를 코틀린에서 사용하는 경우 문제가 발생

-> Annotate your Java types

compileKotlin {
  kotlinOptions {
    freeCompilerArgs += "-Xjsr305=strict"
  }
}

-> Specify types explicitly

Collections

-> Read-only : it is not immutable
-> Mutable

* Generating Kotlin Code *

KotlinPoet

A Functional Approach to Android Architecture using Kotlin

- Modeling Error and Success Cases

-- Result wrapper(Error or Success)
-- RxJava
-- KATEGORY

- Asynchronous Code and Threading

-- Java : ThreadPoolExecutor + exceptions + callbacks
-- RxJava : Schedulers + observable + error subscription
-- KATEGORY

Lazy evaluation

- Dependency Injection

-- Reader Monad: ReaderT, Monad Transformers

Exploring Kotlin's hidden costs - Part 1

Coroutine Theory

- Normal function

Call/Return

activation frame(stack)

- Coroutine

Suspend/Resume/Destroy

coroutine frame(heap) and stack frame

Kotlin Coroutines on Android: Things Wish I Knew at the Beginning

- Executor를 CoroutineDispatchers로 바꿀 수 있다.

- RxJava의 disposable 같은 것을 위해 "root" coroutine parent를 사용할 수 있다.

- CommonPool 의 크기를 조절할 수 있다.

- Async에서의 exception은 바로 발생되지 않는다.

- coroutine들을 협력적으로 cancel 하려면 CoroutineContext를 통한 parent-child 관계가 필요하다.

Async code using Kotlin Coroutines

RxJava to Kotlin coroutines

코루틴과 관련해서 Continuation에 대해서도 알면 좋을것 같다.

What's in a Continuation

Implementing a Stepping Debugger in JavaScript

Exploring Continuations: Resumable Exceptions

Generic interfaces 요점

 https://go.dev/blog/generic-interfaces  Generic interface를 정의할 때 최소한의 제약만을 정의하고 실제 구현체들이 자신만의 필요한 제약을 추가할 수 있도록 하는 것이 좋다. pointer receiver를...