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

[Request] OOP Structures, Syntactic Sugar #48

Closed
Frityet opened this issue Jul 11, 2022 · 11 comments · Fixed by #76
Closed

[Request] OOP Structures, Syntactic Sugar #48

Frityet opened this issue Jul 11, 2022 · 11 comments · Fixed by #76
Labels
enhancement New feature or request

Comments

@Frityet
Copy link

Frityet commented Jul 11, 2022

Currently in lua, there is quite a bit of boilerplate code for creating "structs" or "classes" for OOP, and the way it is done is hard to get done "properly" and soundly

---@class Foo
local Foo = {
	name = "",
	id = 0
}
setmetatable(Foo, Foo)
Foo.__index = Foo

function Foo:new(name, id)
	---@type Foo
	local o = {}
	setmetatable(o, self)

	o.name = name
	o.id = id

	return o
end

function Foo:print()
	print("My name is " .. self.name .. " and my id is " .. self.id)
end

return Foo

Having this abstracted away would offer usability and readability improvements.

(Some syntax I thought up):

local struct Foo = {
	name;
	id;

	function create(name, id)
		self.name = name
		self.id = id
	end

	function print()
		print("My name is " .. self.name .. " and my id is " .. self.id)
	end
}
@Frityet Frityet changed the title Classes Structures Jul 11, 2022
@well-in-that-case
Copy link
Collaborator

well-in-that-case commented Jul 11, 2022

There's already a patch introduced for this type of function declaration, so this is likely something I can implement with minimal headache (but I could be wrong). As for properties, I will need to review the parser, but I imagine it can be done, but this may be redundant too since it plays no effect in the behavior of the table (you still create them with the create function). Not so sure about the struct keyword, appears redundant. After all, it's not a structure, it's just a table. And if I did implement it, it would just be skipped by the parser. So you could imagine.

But otherwise, suggestion seems nice. I'll keep ya posted.

@well-in-that-case well-in-that-case added the enhancement New feature or request label Jul 11, 2022
@well-in-that-case well-in-that-case changed the title Structures OOP Structures, Syntactic Sugar Jul 11, 2022
@well-in-that-case well-in-that-case changed the title OOP Structures, Syntactic Sugar [Request] OOP Structures, Syntactic Sugar Jul 11, 2022
@Frityet
Copy link
Author

Frityet commented Jul 11, 2022

I was already working on an implementation on my fork, what is the function declaration you are talking about?

@well-in-that-case
Copy link
Collaborator

well-in-that-case commented Jul 11, 2022

I was already working on an implementation on my fork, what is the function declaration you are talking about?

http://lua-users.org/wiki/LuaPowerPatches
Ctrl + F "Function fields". Patched into Lua 5.2 already, so it may only need updating and credit.

Edit, link is down. Searched through the web server manually and got the file:
http://lua-users.org/files/wiki_insecure/jRhQ0hat/5.2/oofunc.diff

@Frityet
Copy link
Author

Frityet commented Jul 11, 2022

The reason why I am making a new keyword is because the functionality it intends to abstract over is not just a table/function, it's also the setmetatable function calls, and the indexing. It would be ambiguous as just a table

@well-in-that-case
Copy link
Collaborator

Why would setmetatable be needed? And why would indexing need to change too?

@Frityet
Copy link
Author

Frityet commented Jul 12, 2022

It is the idomatic way of doing this paradigm. I also want to add a "private" attribute that is only valid in the struct declaration which would make it only available to methods in the struct. This would not make any sense outside of the struct declaration, and even in a regular table

@well-in-that-case
Copy link
Collaborator

That's about inheritance, which is fairly unrelated. Users can do this manually in their create function, or whatever they decide to call it. Private is not possible.

@well-in-that-case
Copy link
Collaborator

@Frityet
Copy link
Author

Frityet commented Jul 17, 2022

Progress update,

https://github.com/well-in-that-case/Pluto/tree/0.3.0

Thank goodness, the lua parser was killing me

@well-in-that-case well-in-that-case mentioned this issue Jul 18, 2022
@Sainan Sainan linked a pull request Jul 22, 2022 that will close this issue
@well-in-that-case well-in-that-case mentioned this issue Jul 22, 2022
@well-in-that-case
Copy link
Collaborator

@Frityet
Copy link
Author

Frityet commented May 25, 2023

@Frityet pluto-lang.org/docs/New%20Features/Object-Oriented%20Programming

Yep! I have been folliwng this project closely, looks great

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

Successfully merging a pull request may close this issue.

2 participants