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

Record operator overloading #190

Closed
euclidianAce opened this issue Jul 16, 2020 · 2 comments
Closed

Record operator overloading #190

euclidianAce opened this issue Jul 16, 2020 · 2 comments

Comments

@euclidianAce
Copy link
Member

euclidianAce commented Jul 16, 2020

Are there any current plans for how to implement this? I've written up a very rough pr (no tests or anything) for this feature here but would like some feedback/opinions on it first.

  • First of all, syntax: this is what I settled on. The following is valid in the pr
local Vector = record
   {number}

   has +(Vector, Vector): Vector
   has /(Vector, number): Vector
   has ..(Vector, Vector): string

   has -(Vector, Vector): Vector

   -- technically unary ops arent in yet, but would be quite easy
   has #(Vector): number
   -- The framework is there to allow for binary and unary ops to have the same symbol
   has -(Vector): Vector

   -- etc

   -- has is still a valid name
   has: number
end

local a: Vector = {}
local b = a + a
local c = a / 4
  • Later on some assertions can be added that at least one of the arguments contains the type but I want to stress that this is a proof of concept

  • I'm haven't fully studied the parser yet but I was thinking a "has" fact and operator could be added and we could (as @hishamhm mentions in Standardized class model? #97) use the generated table to store the information and define some reserved keys in tables. For example

local Vector = record has+(Vector, Vector):Vector end
local a: Vector = {}
if a has + then
   local b = a + a
end

would generate

local Vector = {["__teal_has_+"] = true}
local a = {}
if Vector["__teal_has_+"] then
   local b = a + a
end

(although I think lua reserves double underscore names)
and the compiler would error when someone tries to use has on a non record type

Speaking of #97, I think this is a decent way of adding operator overloading without stepping too far into committing to an OOP model.

notably this doesn't hook into metatables at all currently. My reasoning for this is that it's hard and I didn't want to get too far into anything without a second look at these ideas. Also metatable info is hard to track and can be (suprise suprise) super dynamic, not in practice usually, but it can be.

(Also I just realized I hadn't thought about generics at all ¯\_(ツ)_/¯)

@euclidianAce
Copy link
Member Author

Closed as metamethod support is now in master

@hishamhm
Copy link
Member

I don't know how I totally missed this issue! Sorry about that!

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

Successfully merging a pull request may close this issue.

2 participants