Why must the promote operator #^ return a different type than the class?
← Operators and Expressions · Ref: Q839
The promote operator #^ converts an object to a DIFFERENT type. Returning the same type as the enclosing class is not promotion — it is a copy or identity, which is meaningless.
WHAT PROMOTE DOES
Promotion converts one type to another:
Temperature -> String (displaying degrees) Distance -> Float (extracting raw measurement) Score -> Integer (extracting numeric score)
CORRECT PATTERN
operator #^ as pure <- rtn as String: `${degrees} degrees`
The return type (String) differs from the class (Temperature).
INCORRECT PATTERN
operator #^ as pure <- rtn as C5: this
Returning the SAME type (C5) from #^ on class C5 triggers E07420. This is not promotion.
USAGE
reading <- Temperature(98.6) promoted <- #^reading // promoted is now a String
See Q238 for operator overview. See Q242 for conversion operators.
Example
defines module qa.operators.promote.different.type defines class <?- Temperature with promote operator returning String (a different type). This is the correct pattern for promotion. -?> Temperature degrees <- 0.0 Temperature() -> d as Float degrees :=: d //Promote to String — returns DIFFERENT type (correct) operator #^ as pure <- rtn as String: `${degrees} degrees` default operator ? defines program PromoteDemo() stdout <- Stdout() reading <- Temperature(98.6) promoted <- #^reading stdout.println(promoted)
Common mistakes
E07420 — The promote operator #^ must return a DIFFERENT type than the enclosing class. Returning Temperature from a Temperature class is identity, not promotion. Return String, Integer, Float, or another distinct type. See ek9 -h E07420 for details.
Incorrect:
operator #^ as pure <- rtn as Temperature: this
Correct:
operator #^ as pure <- rtn as String: `${degrees} degrees`
Other ways to ask this
- What triggers E07420 MUST_NOT_RETURN_SAME_TYPE?
- Why does my promote operator fail with same type error?
- How do I correctly implement the #^ promote operator in EK9?
Coming from another language?
Java: No promote operator. Implicit conversions via widening (int to long). Explicit casts for narrowing. Python: No operator-based promotion; uses __int__(), __float__(), __str__() dunder methods with no return type enforcement. Rust: From/Into traits enforce different types by design. Kotlin: toInt(), toString() methods with no operator syntax. EK9: #^ operator with compile-time enforcement that return type differs from enclosing class.
Keywords: conversion, E07420, operator, return, promote, #^, type, different