Categorise a dimension measurement as small, medium, or large using named thresholds.
← Control Flow · Ref: Q1206
Use named Dimension constants for thresholds and if-else chains for classification. Named constants make the boundaries self-documenting.
NAMED THRESHOLDS
smallLimit <- 1.0m largeLimit <- 5.0m
CLASSIFICATION
if measurement < smallLimit category: "small" else if measurement < largeLimit category: "medium" else category: "large"
Dimension supports comparison operators (<, >, <=, >=) for same-unit values. Named constants avoid magic literals and make the classification logic readable.
See Q1191 for dimension comparison. See Q1184 for named constants. See Q62 for if-else chains.
Example
defines module qa.flow.switchoncategory defines function shorterOf() as pure -> left as Dimension right as Dimension <- rtn as Dimension: left <? right longerOf() as pure -> left as Dimension right as Dimension <- rtn as Dimension: left >? right classify() as pure -> measurement as Dimension <- category as String: "unknown" smallLimit <- 1.0m largeLimit <- 5.0m if measurement < smallLimit category: "small" else if measurement < largeLimit category: "medium" else category: "large" defines program SwitchOnCategoryDemo() stdout <- Stdout() // === CLASSIFY MEASUREMENTS === measurements <- [0.5m, 1.0m, 2.5m, 5.0m, 10.0m] for measurement in measurements category <- classify(measurement) stdout.println(`${measurement} -> ${category}`) // === COALESCING COMPARISON === beamA <- 0.5m beamB <- 2.5m shortest <- shorterOf(beamA, beamB) longest <- longerOf(beamA, beamB) stdout.println(`Shortest: ${shortest}`) stdout.println(`Longest: ${longest}`) // === STRING REPRESENTATION === panel <- 2.5m panelStr <- $panel stdout.println(`Panel dimension: ${panelStr}`)
Common mistakes
E50060 — Dimension supports comparison operators directly. No getValue() method needed. Use named Dimension constants for thresholds instead of magic numeric literals. See ek9 -h E50060 for details.
Incorrect:
if measurement.getValue() < 1.0
Correct:
if measurement < smallLimit
Other ways to ask this
- Use if-else with Dimension comparisons and named constants to classify a measurement.
- I need to bucket a measurement into size categories using named thresholds.
- In Java I'd use if-else chains with comparisons to classify dimensions. What is the EK9 approach?
- Given a Dimension value, classify it into small, medium, or large with clear boundaries.
Coming from another language?
Java: if (dim < 1.0) small; else if (dim < 5.0) medium; else large — plain doubles, no units. Python: similar if-else, no unit type. Rust: match with guards. Go: if-else with float64. EK9: if-else with Dimension comparison, unit-safe, named constants for thresholds.
Keywords: threshold, small, medium, large, dimension, measurement, classify, categorise, named constant, if else