E103: Illegal Start of Statement
E103: Illegal Start of Statement (문장 시작이 허용되지 않음)
현재 문맥에서 문장(statement)의 시작으로 올바르지 않은 토큰이나 표현식을 컴파일러가 만났을 때 나오는 에러예요.
본문
문장(statement)은 import 또는 export, 정의(definition), 표현식(expression)을 말해요. 어떤 문장은 특정 문맥에서만 허용되죠. 파서가 정의의 시작으로 보이는 무언가를 만났는데 실제로는 최상위 레벨에서 유효하지 않은 문법일 때 이 에러가 자주 발생해요.
이 에러의 가장 흔한 사례 중 하나는 표현식(예: 메서드 호출)을 클래스나 최상위 멤버 정의 밖에서 사용하는 경우예요.
예시
package example
def fa(f: String ?=> Unit): Unit = ???
fa(42)
에러 메시지
-- [E103] Syntax Error: example.scala:4:0 --------------------------------------
4 |fa(42)
|^^
|Illegal start of toplevel definition
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| A statement is an import or export, a definition or an expression.
| Some statements are only allowed in certain contexts
-----------------------------------------------------------------------------
해결 방법
표현식을 메서드 정의로 감싸면 돼요.
// Wrap the call in a method definition
package example
def fa(f: String ?=> Unit): Unit = ???
def test: Unit = fa(())