Back to Scala3

E066: Native Members May Not Have Implementation

docs/_docs/reference/error-codes/E066.md

3.8.41.2 KB
Original Source

E066: Native Members May Not Have Implementation

This error is emitted when a method marked with @native annotation has an implementation body.

The @native annotation indicates that the method's implementation is provided by the runtime or a native library (like JNI). Native methods must be declared without a body because their implementation exists outside of Scala code.


Example

scala
class Example:
  @native def nativeMethod(): Int = 42

Error

scala
-- [E066] Syntax Error: example.scala:2:14 -------------------------------------
2 |  @native def nativeMethod(): Int = 42
  |              ^
  |              @native members may not have an implementation

Solution

scala
// Remove the implementation from native methods
class Example:
  @native def nativeMethod(): Int
scala
// Or remove @native if you want to provide an implementation
class Example:
  def normalMethod(): Int = 42
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>