<=) is a binary relational operator that evaluates the ordering of two operands. While conventionally used to return a boolean indicating numerical comparison, the operator’s specific logic, parameter types, and return type are determined by the implementation of the underlying method named <= on the left-hand operand.
Syntax
Semantics and Desugaring
The expressiona <= b is syntactic sugar for a method invocation on the left-hand operand (a). The Dart compiler resolves this expression using the following logic:
- Instance Method Resolution: The compiler looks for an instance method named
<=on the class ofa. - Extension Method Resolution: If no instance member is found, the compiler looks for an applicable extension method named
<=that acceptsaas the receiver.
a <= b desugars to a.<=(b). However, a.<=(b) is not valid Dart syntax; the operator symbol must be used in the infix position.
Operator Declaration
To define or overload the<= operator, a class or extension must declare a method using the operator keyword followed by the symbol <=.
- Method Name: The identifier of the method is
<=. Theoperatorkeyword is a syntactic marker indicating that the method is an operator overload. - Parameter: The method accepts exactly one parameter, which corresponds to the right-hand operand.
- Return Type: The Dart language specification permits any return type. However, the Dart Style Guide and standard library conventions dictate returning
bool.
Standard Library Behavior
For the built-innum types (int and double), the operator adheres to IEEE 754 standards:
- Finite Numbers: Returns
trueif the left operand is numerically less than or equal to the right operand. - Infinity:
-double.infinity <= valuereturnstruefor any value (exceptNaN).value <= double.infinityreturnstruefor any value (exceptNaN).double.infinity <= valuereturnsfalsefor any finite value.
- NaN: Any comparison involving
double.nanreturnsfalse, includingdouble.nan <= double.nan.
Example: Custom Implementation
The following example demonstrates overloading the<= operator in a custom class.
Master Dart with Deep Grasping Methodology!Learn More





