E190: Pure Unit Expression — 순수 Unit 표현식
E190: Pure Unit Expression — 순수 Unit 표현식
Unit이 기대되는 상황에서 순수한(pure) non-Unit 표현식이 버려질 때 발생하는 경고예요.
본문
Unit이 기대되는 곳에 non-Unit 타입의 표현식이 사용되면, 컴파일러가 버리기(discarding) 변환을 넣어요. 만약 그 표현식이 순수하다면(부수 효과가 없다면) 이 변환은 아무 유용한 효과도 내지 못하고, 사실상 ()와 동등해져요.
좀 더 자세한 설명 (Longer explanation)
이 표현식이 Unit 타입이 아니기 때문에 { expression; () }로 디슈가(desugar)돼요. 여기서 표현식은 버려질 수 있는 순수한 문(statement)이에요. 따라서 이 표현식은 사실상 ()와 동등해져요.
예시 (Example)
def example: Unit = 42
에러 (Error)
-- [E190] Potential Issue Warning: example.scala:1:20 --------------------------
1 |def example: Unit = 42
| ^^
| Discarded non-Unit value of type Int. Add `: Unit` to discard silently.
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| As this expression is not of type Unit, it is desugared into `{ 42; () }`.
| Here the `42` expression is a pure statement that can be discarded.
| Therefore the expression is effectively equivalent to `()`.
-----------------------------------------------------------------------------
해결 방법 (Solution)
Unit을 명시적으로 반환하세요:
def example: Unit = ()
값을 반환하고 싶다면 반환 타입을 바꾸세요:
def example: Int = 42
더 알아보기
Unit을 기대하는 곳에서 순수한 non-Unit 값은 그냥 버려지고 ()와 같아져요. 타입에 맞게 정리해주세요.