Skip to content

Operator Overloading with Unique #184

Description

@callendorph

Hi,

I'm attempting to use operator overloading. I've run into an issue when attempting to use the Unique type as a sub-type for a class which has the equal? operator.

Here is an example project:

op_overload_test.zip

When I run without the Unique subtype - I get:

$> stanza build op-tests-no-unique && ./op-tests-norm
[Test 1] test-basic
C: a < b == d
[PASS]

Tests Finished: 1/1 tests passed. 0 tests skipped. 0 tests failed.

Longest Running Tests:
[PASS] test-basic (656 us)

This is the expected result.

When I run with the Unique subtype:

stanza build op-tests-with-unique
/mnt/c/Users/callendorph/Documents/AFT/Jitx/op_overload/tests/ASTBuild.stanza:13.13: Ambiguous call to overloaded function equal? with arguments of type (AST, AST). Possibilities are:
  equal?: (AST, AST) -> AST at /mnt/c/Users/callendorph/Documents/AFT/Jitx/op_overload/src/Operators.stanza:28.12
  equal?: (Equalable, Equalable) -> True|False in package core

/mnt/c/Users/callendorph/Documents/AFT/Jitx/op_overload/tests/ASTBuild.stanza:14.23: Cannot call function name of type AST -> String with arguments of type (True|False|AST).

So the issue here is the Unique has the Equalable subtype. Equalable defines its method as:

public defmulti equal? (a:Equalable, b:Equalable) -> True|False

Which means that the == operator can only return True|False - which means it can't return a value of type AST and meet the interface requirements.

I can clearly work around this by not using Unique but for lostanza C wrappers, the Unique class is pretty critical for making easy to use code.

I don't see in core.stanza where Unique 's requirement for Equalable comes from. The Liveness tracker seems to convert the ref<Unique> to a long for storage. I don't 'see a case where a Unique object requires the == comparison but I clearly haven't been through every bit of the compiler/core code.

Do you have a suggestion for how to proceed ? Does Unique require the Equalable subtype ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions