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

Allow operators as method names #27

Closed
faultyserver opened this issue Nov 5, 2017 · 0 comments
Closed

Allow operators as method names #27

faultyserver opened this issue Nov 5, 2017 · 0 comments
Labels
feature-request Any request for a new feature of the language. Includes both syntax and library features. syntax Any issue relating to the syntax of Myst.
Milestone

Comments

@faultyserver
Copy link
Member

The parser and native library already implement +, -, *, / as Calls to methods on objects. This should also be allowed in definitions within the language. For example:

deftype Foo
  def a; @a; end

  def initialize(a)
    @a = a
  end

  def +(other : Foo)
    @a = @a + other.a
  end
end

f1 = %Foo{1}
f2 = %Foo{2}

f3 = f1 + f2
f3.a #=> 3

This is a contrived example, for sure, but shows how the syntax can be used. This is particularly helpful for things like DateTimes or numeric replacements (e.g., BigInteger).

The only change needed for this should be allowing the operators as method names. The parser should already handle the infix calls properly.

The operators that should be supported are: +, -, *, /, %, <, <=, ==, !=, >=, >, [], []=, and =: (see #11). Handling unary operators (-, !, *) can come later, since the interpreter doesn't currently support these anyway.

@faultyserver faultyserver added feature-request Any request for a new feature of the language. Includes both syntax and library features. syntax Any issue relating to the syntax of Myst. labels Nov 5, 2017
@faultyserver faultyserver added this to the v0.2.0 milestone Nov 5, 2017
faultyserver added a commit that referenced this issue Nov 6, 2017
The interpreter already treated operators as Calls on the receiving objects. This allows user-written code to define overloads for those operators on their own types.
faultyserver added a commit that referenced this issue Nov 6, 2017
Operator overloading can also be used with `defstatic` or on a module to define operators for things other than instances. This could potentially be used for some type algebra tricks, though it doesn't seem overly useful outside of metaprogramming.
faultyserver added a commit that referenced this issue Nov 10, 2017
The interpreter already treated operators as Calls on the receiving objects. This allows user-written code to define overloads for those operators on their own types.
faultyserver added a commit that referenced this issue Nov 10, 2017
Operator overloading can also be used with `defstatic` or on a module to define operators for things other than instances. This could potentially be used for some type algebra tricks, though it doesn't seem overly useful outside of metaprogramming.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Any request for a new feature of the language. Includes both syntax and library features. syntax Any issue relating to the syntax of Myst.
Projects
None yet
Development

No branches or pull requests

1 participant