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

Operator Overloading and Overriding #66

Open
Alexius-Huang opened this issue May 21, 2020 · 1 comment
Open

Operator Overloading and Overriding #66

Alexius-Huang opened this issue May 21, 2020 · 1 comment

Comments

@Alexius-Huang
Copy link
Owner

Alexius-Huang commented May 21, 2020

What?
Operator overloading which extends more usage of operator

Why?
Methods and functions can be overloaded, why not also for operators.

How?
Important rules: Operators which forbidden to overload/override are=, => and |> (see #67 )

def +(op1: Str, op2: Str): Str do
  op1.concat(op2)
end

def +(op1: Str, op2: Num): Str => op1 + op2.toStr()
def +(op1: Num, op2: Str): Str => op2 + op1

foo = "123" + "456"
bar = "123" + 456
baz = 123 + "456"

override def +(op1: Num, op2: Num): Num => op1 - op2
bazz = 123 + 456

Compiled Result

function OP_plus_1(op1, op2) {
  return op1.concat(op2);
}

function OP_plus_2(op1, op2) {
  return op1.concat(op2.toString());
}

function OP_plus_3(op1, op2) {
  return OP_plus_2(op2, op1);
}

const foo = OP_plus_1("123", "456");
const bar = OP_plus_2("123", 456);
const baz = OP_plus_3(123, "456");

function OP_plus_0$1(op1, op2) { // compiled result might change because of default function name
  return op1 - op2;
}

const bazz = OP_plus_0$1(123, 456);
@Alexius-Huang
Copy link
Owner Author

It would be better to solve issue #76 first then implement operator overloading, since operator can really get confused with method invocation.

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

No branches or pull requests

1 participant