Why must EK9 constructs be used in their valid context?
← Syntax and Structure Rules · Ref: Q952
Language constructs must be used in their valid context.
EK9 enforces that constructs appear only where they are valid:
VALID CONTEXTS
'defines class' — inside module, contains classes 'defines function' — inside module, contains functions 'defines record' — inside module, contains records 'defines trait' — inside module, contains traits 'defines service' — inside module, contains services 'defines program' — inside module, contains programs 'defines constant' — inside module, contains constants 'defines application' — inside module, contains applications
STRUCTURE
Every .ek9 file starts with 'defines module <name>' and contains one or more 'defines <section>' blocks. Each section can only contain its declared construct type.
See Q725 for class body ordering. See Q726 for module reference syntax. See Q893 for section headers.
Example
defines module qa.syntax.wrongcontext defines class <?- Classes go in 'defines class' section. -?> Point x <- Float() y <- Float() default private Point() as pure Point() as pure -> x as Float y as Float this.x :=: x this.y :=: y default operator <=> default operator == default operator $ default operator :=: override operator ? as pure <- rtn as Boolean: x? and y? defines program ConstructContextDemo() stdout <- Stdout() p1 <- Point(3.0, 4.0) p2 <- Point(3.0, 4.0) stdout.println(`p1: ${p1}`) stdout.println(`p1 == p2: ${p1 == p2}`) stdout.println("Each section holds its own construct type")
Common mistakes
E09010 — Using a type name like 'Point' directly as a value expression is inappropriate. Types are not values. See ek9 -h E09010 for details.
Incorrect:
stdout.println(Point)
Correct:
stdout.println(`p1: ${p1}`)
Other ways to ask this
- What triggers E09010 CONSTRUCT_IN_WRONG_CONTEXT?
- Why can't I define a class inside a function?
- Where can I place different construct types?
Coming from another language?
Java: class inside method is allowed (local class). Python: class inside function allowed. Rust: impl blocks must be at module level. Go: type definitions at package level. EK9: strict section-based structure, each section contains only its declared construct type.
Keywords: E09010, context, placement, class, construct, module, structure, section, record, function