Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix polyglot number comparison #5623

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Any.enso
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ type Any

## PRIVATE
Checks if both comparators of the given objects are both of same type and ordered.
If they are not of same type, a `Type_Error` is thrown.
If the comparators are either `Incomparable`, or unordered, `False` is returned.
If they are not of same type, a `Type_Error` is thrown. If the comparators are
either `Incomparable`, or unordered, `Incomparable_Values` is thrown.
assert_ordered_comparators : Any -> (Any -> Any) -> Any ! (Type_Error | Incomparable_Values)
assert_ordered_comparators this that ~action =
comp_this = Comparable.from this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ from project.Data.Boolean import all
- Symmetry: if x == y then y == x
- Reflexivity: x == x
- Transitivity: if x < y and y < z then x < z
- Antisymmetry (?): if x > y then y < x
- if x > y then y < x

Users are responsible for the compliance to the aforementioned semantics.
Should the semantics be violated, an unexpected behavior may be encountered, e.g.,
Expand Down
2 changes: 2 additions & 0 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Polyglot.enso
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ get_members object = @Builtin_Method "Polyglot.get_members"

> Example
Instantiate a new Java Integer with the value 1.
polyglot java import java.lang.Integer

Polyglot.new Integer [1]
new : Any -> Vector -> Any
new constructor arguments = @Builtin_Method "Polyglot.new"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,31 @@ Object doConvertText(
}
}

@Specialization(
guards = {
"!typesLib.hasType(that)",
"!typesLib.hasSpecialDispatch(that)",
"interop.fitsInLong(that)"
})
Object doConvertNumber(
VirtualFrame frame,
State state,
UnresolvedConversion conversion,
Object self,
Object that,
Object[] arguments,
@CachedLibrary(limit = "10") InteropLibrary interop,
@CachedLibrary(limit = "10") TypesLibrary typesLib,
@Cached ConversionResolverNode conversionResolverNode) {
Function function =
conversionResolverNode.expectNonNull(
that,
extractConstructor(self),
EnsoContext.get(this).getBuiltins().number().getNumber(),
conversion);
return invokeFunctionNode.execute(function, frame, state, arguments);
}

@Specialization(
guards = {
"!typesLib.hasType(that)",
Expand Down
10 changes: 10 additions & 0 deletions test/Tests/src/Semantic/Equals_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Standard.Test.Extensions

polyglot java import java.nio.file.Path as JavaPath
polyglot java import java.util.Random as Java_Random
polyglot java import java.lang.Integer
polyglot java import java.lang.Double

type CustomEqType
C1 f1
Expand Down Expand Up @@ -161,6 +163,14 @@ spec =
four_field = FourFieldType.Value 1 2 3 4
(rect == four_field).should_be_false

Test.specify "should handle equality of numbers with Polyglot.new" <|
int = Polyglot.new Integer [42]
double = Polyglot.new Double [42.0]
int.should_equal 42
double.should_equal 42
int.should_equal double


Test.specify "should handle `==` on types" <|
(Child == Child).should_be_true
(Child == Point).should_be_false
Expand Down