E156: Modifier Not Allowed For Definition
This error occurs when a modifier is used on a definition where it is not applicable. Different kinds of definitions (classes, objects, methods, etc.) support different sets of modifiers.
Common invalid combinations include:
opaqueon a method definition (only valid for type aliases)abstracton an object definitionsealedon an object definition
Example
object Test {
opaque def example: Int = 42
}
Error
-- [E156] Syntax Error: example.scala:2:13 -------------------------------------
2 | opaque def example: Int = 42
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Modifier opaque is not allowed for this definition
Solution
object Test {
// opaque is only valid for type aliases
opaque type PositiveInt = Int
// Regular method definition
def example: Int = 42
}
// abstract is not allowed on object - use trait or abstract class instead
abstract class Base {
def example: Int
}
In this article