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

Implementation of 'new' keyword #690

Closed
NotSoSeriouss opened this issue Sep 3, 2023 · 3 comments
Closed

Implementation of 'new' keyword #690

NotSoSeriouss opened this issue Sep 3, 2023 · 3 comments
Labels
duplicate This issue or pull request already exists feature request New feature or request

Comments

@NotSoSeriouss
Copy link

I know that this might be not so important but it might also make some code cleaner. I already took a look at #638 and realized how important is the existance for both static functions and methods inside of records. But I think a cleaner way of working with methods will also lead to cleaner code:

Right now if I want to create a constructor I have to do it this way:

local record Point
    x: integer
    y: integer
    new: function(x: integer, y: integer) : Point -- Constructor
end

function Point.new(x: integer, y: integer) : Point -- Constructor definition
    local self: Point = setmetatable({}, {__index = Point})
    self.x = x
    self.y = y
    return self
end

And to create a new method:

local record Point
    x: integer
    y: integer
    new: function(x: integer, y: integer) : Point
    incrementX: function(self: Point, n: integer) -- Method declaration
end

And

function Point:incrementX(n: integer) -- Method definition
    self.x = self.x + n
end

But I propose a different way of building constructors and methods:

To make a constructor for an object we can simply declare a function with the same name of the record. This shouldn't cause any compatibiliy issues since Teal already doesn't allow that. But instead of giving an error, this:

local record Point
    x: integer
    y: integer
end

function Point(x: integer, y: integer)
    self.x = x
    self.y = y
end

Could get automatically compiled as if it were:

local record Point
    x: integer
    y: integer
    new: function(x: integer, y: integer) : Point
end

function Point.new(x: integer, y: integer) : Point
    local self: Point = setmetatable({}, {__index = Point})
    self.x = x
    self.y = y
    return self
end

And if there is no declaration of the constructor, then this would be the case:

local record Point
    x: integer
    y: integer
    new: function() : Point
end

This can allow for a nice improvement: the use of new

local record Point
    x: integer
    y: integer
end

local a: Point = new Point()
-- Or even
local a: Point = new() -- Since the type is already known

which is only shugar syntax for:

local a: Point = Point.new()
@NotSoSeriouss
Copy link
Author

NotSoSeriouss commented Sep 3, 2023

About methods: I didn't include the part about methods because this is already too munch by itself so I didn't want to overload the issue. But it would work in a similar way by using the keyword "method" instead of "function" in the declaration inside the record. Just like how #638 proposed so I didn't want to repeat that.

@NotSoSeriouss
Copy link
Author

Just realized this is a kind of duplicate of #200. I will leave the issue open just in case

@hishamhm hishamhm added duplicate This issue or pull request already exists feature request New feature or request labels Sep 4, 2023
@hishamhm
Copy link
Member

hishamhm commented Sep 4, 2023

@NotSoSeriouss Yes, this is a duplicate of #200 and #97. I'm going to close this one, but these references will create backlinks at the open issues so that your comments can be found from there as well.

@hishamhm hishamhm closed this as completed Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants