Skip to content

Commit

Permalink
[spec] (#27) Assert that operator overloads work for Types and Modules.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
faultyserver committed Nov 6, 2017
1 parent d5e9937 commit 2744cee
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions spec/interpreter/nodes/call_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@ describe "Interpreter - Call" do
f3 = f1 #{op} f2
f3.a
), [val(3)]

# Operators can also be defined statically to do some type algebra.
it_interprets %Q(
deftype Foo
defstatic #{op}(other : Foo)
:called_op_on_type
end
end
Foo #{op} Foo
), [val(:called_op_on_type)]

# Or on modules, for whatever that might be worth... (I guess this could
# define operators in a module that could be included? Not sure why it
# would be done outside of an `include`, though).
it_interprets %Q(
defmodule Foo
def #{op}(other)
:called_op_on_module
end
end
Foo #{op} Foo
), [val(:called_op_on_module)]
end

# Access and access assignment can also be overloaded.
Expand Down

0 comments on commit 2744cee

Please sign in to comment.