StanzaOrg / lbstanza-old

L.B. Stanza Programming Language
Other
216 stars 23 forks source link

Operator Overloading with `Unique` #184

Open callendorph opened 1 year ago

callendorph commented 1 year ago

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 ?

callendorph commented 1 year ago

This may be a non-issue. What I'm finding is that Unique/Finalizer doesn't actually work for what I'm trying to accomplish. I end up getting out of order inc/dec on references and object deletion which causes std::abort calls. I'm in the middle of refactoring this code to remove Unique from most objects.

I think that it is possible that another library might want to use Unique and override equal? some day, but I don't have an active use case for such.