luau icon indicating copy to clipboard operation
luau copied to clipboard

Autocomplete Issues with "self" and OOP

Open karl-police opened this issue 2 years ago • 2 comments

Could be a Bug or Feature request. I don't think this is a bug, because the implementation for what I am specifically asking here, doesn't exist. So it's more of a "Feature request". Can't change the label anymore though.

I will mainly note 2 issues.

The autocomplete is part of this repository, I believe?

local test = {}
test.__index = test

test.value = "hello"

function test.new()
	local self = setmetatable({}, test)
	
	return self
end



return test

Let's work with this.

Issue 1 - Fields and Methods interpretation

. and :

Defining the function like so image

Successfully gives you :GET as an autocomplete.

However, this doesn't. image

And that is Issue 1.

Issue 2 - "self" Inheritance?

image

self. is unable to autocomplete more.

At least self could be showing all the methods from it's original object, which is test. But it doesn't do that.

One could use export but this is not automatic, so it's annoying.

That's Issue 2.

I mean, self can be seen as any other variable. But it gets highlighted as red. It could get special treatment to do better autocompletion.

karl-police avatar Aug 02 '23 11:08 karl-police

The core issue here is that Luau currently takes the very conservative stance that self can only be inferred from the way it is used within each individual method. It is, after all, completely valid to write test.GET(5).

This approach is pretty simple and general, but I agree that it has some pretty unfortunate consequences.

This is something we're looking to improve. Take a look at this RFC for details: https://github.com/luau-lang/rfcs/pull/5

andyfriesen avatar Dec 08 '23 17:12 andyfriesen

I think I still have a discussion open about AST.

karl-police avatar Jan 14 '24 01:01 karl-police