Partial equivalence #6883
Labels
lib: core
Core library
lib: std
Standard library
tracking-issue
Tracking issue for experimental Sway features
This is a tracking issue for Partial equivalence.
The experimental feature flag for the issue is
partial_eq
.Description
Currently,
core::ops
provides only theEq
trait, thus, allowing expressing equivalence, but not partial equivalence. In simple terms, we expect that for every type that supports comparison, each instance of the type is always equal to itself, a property called reflexivity. While this assumption holds for majority of types used in blockchain development, we want to lift this restriction and support partial equivalence. A typical example of a type supporting partial equivalence, but not equivalence, is a floating point number, whereNaN
is different from any other number, including itself:NaN != NaN
.The need for partial equivalence is first expressed in #6668.
Breaking Changes
Replace
Eq
trait implementations withPartialEq
andEq
implementationsPartial equivalence feature renames the current
Eq
trait toPartialEq
and adds a new, emptyEq
trait withPartialEq
as a supertrait.Every existing
Eq
trait implementation needs to be renamed toPartialEq
, and in addition, an emptyEq
implementations needs to be added.E.g., this implementation:
will become:
If the original implementation implements
Eq
for a generic type and in addition hasEq
on trait constraints, thoseEq
trait constraints must be replaced byPartialEq
in the newPartialEq
impl, and remainEq
in the new, empty,Eq
impl.E.g., this implementation:
will become:
After doing these replacements for
Eq
trait impls, consider looking at trait constraints (where
keywords) in other impls that useEq
as a trait constraint, and eventually lowering the constraint by replacing it with thePartialEq
. Note that this is not strictly necessary, because the above change did not change the existing semantics ofEq
. It is just that some of the constraints might be lowered, if the full equivalence is not strictly needed.If you decide to review the usages of
Eq
inwhere
s, the following regex-search will give you these usages:[^:]:[^:]\s*Eq
.Check name clashes with existing types called
Error
orEnum
The
partial_eq
feature introduces a new traits in thecore
prelude:core::ops::PartialEq
.If the existing code already has a type with that name, and only if that type is imported via glob (
*
) import the "Multiple bindings exist for symbol in the scope" error will be emitted.E.g., suppose that we have a type
my::impls::PartialEq
, and import it in scope byuse my::impls::*
. After migrating to partial equivalence, the following error will appear:The solution is, as proposed in the help message, to either fully qualify the
PartialEq
symbol, or to import it by specifying its full path:use my::impls::PartialEq
.Steps
Eq
trait withPartialEq
+Eq
PartialEq
andEq
in trait constraints #6898PartialEq
andEq
#6899The text was updated successfully, but these errors were encountered: