E07188: an ordering - a '<=>' or a comparator function - must order every two set values of its type: it cannot return an unset Integer, nor build its result on a '<=>' (a field's, a super's, a function's) that answers unset for two set values
What went wrong
An ordering - a '<=>' written by hand, or a comparator function - can leave its result UNSET for two values that are both SET. Those two values then have no order: a sort over a list of them would depend on the order the list arrived in, and '<?' or '>?' would hand back whichever operand happened to come second.
EK9 checks a '<=>' when the operator is declared, because the compiler knows every type that can reach it, and a comparator where something orders by it (below). An ordering must order every two SET values of its type. It is reported when the Integer it returns can come from
- an unset Integer: 'Integer()', typically the 'else Integer()' of a '<=>' that only compares values of one unit;
- a '<=>' that is itself partial: the '<=>' of Money (defined only within one currency) or Dimension (only within one unit suffix), the hand-written '<=>' of another type that is partial, or a method or function whose result comes from one of these;
- the '<=>' of a type whose values can be of DIFFERENT run-time types: an open or abstract class, record or component, or a trait, whose sameness is per-type, not hierarchy-wide (one written by hand on an abstract base or trait). Two values of different run-time types are never the same, so their '<=>' is unset. This holds whether or not a sub-type exists yet: another module can add one.
A sub-type that overrides a '<=>' with one that can answer unset is reported on the sub-type's own '<=>', not on the super's.
A comparator function is an ordering too, so the same sources make it partial. It is reported where something orders by it - 'sort by byAmount', a function given where a Comparator is expected ('queue.withComparator(byAmount)', 'ordering as Comparator of Money: byAmount') - and where a dynamic function is declared 'is Comparator of T'. A function is never reported for its shape alone: one that takes two values of a type and returns an Integer that can be unset ('safeDivide') is ordinary until something orders by it. A comparator whose answer is unset only at run time - one that compares a key that can be unset - stops 'sort by' or a PriorityQueue with an EK9 Exception naming the two values and the comparator; it never treats them as equal.
A generic whose own '<=>' compares its type parameter's values ('item <=> other.item' over 'item as T?') cannot be judged where it is declared: it is as total as the '<=>' of the type T is given. So it is reported where the generic is given its type ('Box of Plain', 'Box of Money'), when that type's '<=>' is partial in one of the ways above.
The check looks at where the returned Integer can get its value, not at the path the code takes: a partial source is reported even when a test guards it. A path that never assigns a returning variable declared with no value is E08050. A '<=>' that is generated ('default operator <=>') is never reported here: over a field that is partial (Money, Dimension, or an open or abstract type as above) it is withdrawn, and a use of it is E07620.
Worked examples
The [EXAMPLE] section below shows a Length that returns an unset Integer for two lengths in different units, a Priced that compares Money values directly, and a comparator function that does the same for 'sort by', then the fixes - compare the unit first and the amount second, and compare a Money's currency (its '#>') before its amount (its '#<'). The last fix is for a generic whose '<=>' compares its T values: a Shelf of Money with no '<=>', sorted by a comparator, and a Box given a Price - a type wrapping a Money whose '<=>' orders every pair - in place of the Money.
How to fix it
- Make the result total: compare the part that groups the values (the unit, the currency) first, then the part that orders within a group. 'rtn: unit <=> other.unit' and then 'if rtn == 0' compare the amounts.
- For Money compare '#>' (the currency, a String) and then '#<' (the amount, a Float). For a Dimension the same two prefix operators give the suffix and the value.
- Never write 'Integer()' as a result: declare it with no value ('<- rtn as Integer?') and let E08050 prove that every path assigns it - in a '<=>' and in a comparator alike.
- For a field of an open or abstract type: put the ordering on an abstract base or a trait, written by hand over what every sub-type provides ('area() <=> other.area()'), so it is hierarchy-wide sameness; or make the field's type closed (remove 'as open'); or compare a part of the field that every run-time type has ('item.size()').
- If the type should not be ordered at all, declare 'default operator ==' and compare with a comparator function.
- When it is reported where a generic is given its type ('Box of Money'), the generic's '<=>' compares its T values, and no change to the generic makes Money's '<=>' total. If the generic does not need a natural order, remove its own ordering and order its values with a comparator that orders every pair ('sort by byHeldPrice'). If it does, give it a type whose '<=>' is total: a Price that holds the Money and compares '#>' then '#<' ('Box of Price'). For an open type, put a hand-written '<=>' on its abstract base or trait (action 4).
Common causes
- 'else Integer()' on a '<=>' that compares only values of the same unit, kind or currency
- Comparing two Money or Dimension fields with '<=>' directly
- A '<=>' that calls a helper whose result is unset for some pair of values
- A comparator function for 'sort by' or a Comparator that compares Money or Dimension values directly
- Comparing a field whose type is open (or abstract with a defaulted '<=>'): a value of the type and one of a sub-type compare unset
- Giving a generic whose '<=>' compares its T values an open type, an abstract type with a defaulted '<=>', Money or Dimension
See also
- E07620: a use of an ordering that is not available (a defaulted '<=>' over a Money, Dimension or open-typed field is withdrawn)
- E07187: '<?' and '>?' over a type whose values can be of different run-time types
- E07200: 'default operator <=>' written out over a field that is not totally ordered
- E08050: a returning variable that is not assigned on every path
- E07181: comparisons generated from the sameness
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E07188: an ordering - a '<=>' or a comparator function - must order every two set values of its type: it cannot return an unset Integer, nor build its result on a '<=>' (a field's, a super's, a function's) that answers unset for two set values
#!ek9 defines module error.e07188.bad defines class Length amount as Integer: Integer() unit as String: String() Length() -> amount as Integer unit as String this.amount: amount this.unit: unit measure() as pure <- rtn as String: unit quantity() as pure <- rtn as Integer: amount //Two lengths in different units are set, but this '<=>' gives them no order: it returns an unset Integer operator <=> as pure -> other as Length <- rtn as Integer: unit == other.measure() <- amount <=> other.quantity() else Integer() default operator ? default operator $ default operator $$ operator #? as pure <- rtn as Integer: #?amount + #?unit default operator :=: Priced name as String: String() price as Money: Money() Priced() -> name as String price as Money this.name: name this.price: price //Money orders only within one currency: 10 GBP <=> 5 USD is unset, and so is this '<=>' operator <=> as pure -> other as Priced <- rtn as Integer: price <=> other.price default operator ? default operator $ default operator $$ operator #? as pure <- rtn as Integer: #?price default operator :=: defines function //Money orders only within one currency: a comparator built on its '<=>' cannot order 10 GBP and 5 USD byAmount() as pure -> first as Money second as Money <- rtn as Integer: first <=> second defines program SortPrices() stdout <- Stdout() prices <- [10#GBP, 5#USD, 3#GBP] sorted <- cat prices | sort by byAmount | collect as List of Money stdout.println($sorted)
Fixed: Order every two set values: compare the unit or currency first, then the amount
#!ek9 defines module error.e07188.good defines class Length amount as Integer: Integer() unit as String: String() Length() -> amount as Integer unit as String this.amount: amount this.unit: unit measure() as pure <- rtn as String: unit quantity() as pure <- rtn as Integer: amount //Every pair of lengths is ordered: by unit first, then by amount - no path returns an unset Integer operator <=> as pure -> other as Length <- rtn as Integer: unit <=> other.measure() if rtn == 0 rtn: amount <=> other.quantity() default operator ? default operator $ default operator $$ operator #? as pure <- rtn as Integer: #?amount + #?unit default operator :=: Priced name as String: String() price as Money: Money() Priced() -> name as String price as Money this.name: name this.price: price //The currency (String) is compared first, then the amount (Float): both orders are total operator <=> as pure -> other as Priced <- rtn as Integer: #> price <=> #> other.price if rtn == 0 rtn: #< price <=> #< other.price default operator ? default operator $ default operator $$ operator #? as pure <- rtn as Integer: #?price default operator :=:
Fixed: Order Money in a comparator: compare the currency first, then the amount
#!ek9 defines module error.e07188.good.comparator defines function //Every pair of prices is ordered: by currency (a String) first, then by amount (a Float) byCurrencyThenAmount() as pure -> first as Money second as Money <- rtn as Integer: #> first <=> #> second if rtn == 0 rtn: #< first <=> #< second defines program SortPrices() stdout <- Stdout() prices <- [10#GBP, 5#USD, 3#GBP] sorted <- cat prices | sort by byCurrencyThenAmount | collect as List of Money stdout.println($sorted)
Fixed: A generic whose '<=>' compares its T values: order with a comparator, or give it a type with a total '<=>'
#!ek9 defines module error.e07188.good.generic defines class //Shelf has no '<=>' of its own, so 'Shelf of Money' is fine: something that needs an order uses a comparator Shelf of type T item as T? default private Shelf() Shelf() -> initial as T item: initial held() as pure <- rtn as T: item default operator ? default operator $ //Box's '<=>' compares its T values, so it is only as total as the '<=>' of the type it is given Box of type T item as T? default private Box() Box() -> initial as T item: initial operator <=> as pure -> other as Box of T <- rtn as Integer: item <=> other.item operator #? as pure <- rtn as Integer: #? item default operator ? default operator $ //A Money with a total '<=>': the currency (a String) first, then the amount (a Float) Price money as Money: Money() Price() -> money as Money this.money: money operator <=> as pure -> other as Price <- rtn as Integer: #> money <=> #> other.money if rtn == 0 rtn: #< money <=> #< other.money default operator ? default operator $ default operator $$ operator #? as pure <- rtn as Integer: #?money default operator :=: defines function //Every pair of shelves is ordered: by the currency of what each holds, then by its amount byHeldPrice() as pure -> first as Shelf of Money second as Shelf of Money <- rtn as Integer: #> first.held() <=> #> second.held() if rtn == 0 rtn: #< first.held() <=> #< second.held() defines program OrderPrices() stdout <- Stdout() shelves <- [Shelf(10#GBP), Shelf(5#USD), Shelf(3#GBP)] byComparator <- cat shelves | sort by byHeldPrice | collect as List of Shelf of Money stdout.println($byComparator) boxes <- [Box(Price(10#GBP)), Box(Price(5#USD)), Box(Price(3#GBP))] byOwnOrder <- cat boxes | sort | collect as List of Box of Price stdout.println($byOwnOrder)
Questions and answers
These Q&A entries show E07188 and its fix:
Found a problem?
If EK9 reported E07188 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.