Distinction between a class and instance (of a class) - static vs instance methods/variables
See original GitHub issueEnvironment(环境)
name | version |
---|---|
IDEA version | IntelliJ IDEA 2018.2.4 (Ultimate Edition) - Build #IU-182.4505.22, built on September 18, 2018 |
EmmyLua version | 1.2.5-IDEA182 |
OS | macOS 10.13.6 |
What are the steps to reproduce this issue?(重现步骤?)
- Create a class, add a static method e.g. using the sample code found in the README animation:
---@class MyClass
---@field public child MyClass
local cls = {}
function cls:init(name, child)
self.child = child
self.name = name
end
---@return MyClass
function cls.new()
--todo: return new instance
end
- Instantiate an instance of the class (or
@type MyClass
a variable):
local instance = cls.new()
- Access auto-complete on the instance e.g.
instance.-- Leave your text cursor here
What happens?(出现什么问题?)
Auto-complete suggests static methods which do not exist on the instance, in this case new
:
The inverse is also true, if you have a reference to cls
, instance methods are suggested.
What were you expecting to happen?(期望?)
Only see instance variables/functions on references to an instance of a class, only see static variables/instances on references to a class. …
Any logs, error output, etc?(有没有什么log, error输出?)
As shown in the screenshot above.
Any other comments?(其它说明)
I believe there’s also no way to type annotate a reference to a class.
e.g. Dynamic instantiation example:
---@class Transport @parent class
---@public field name string
local transport = {}
---@return Transport
function transport.new()
--todo: return new instance
end
function transport:move()end
---@class Car : Transport @Car extends Transport
local car = {}
function car:move()end
---@return Car
function car.new()
--todo: return new instance
end
---@class Ship : Transport @Ship extends Transport
local ship = {}
---@return Ship
function ship.new()
--todo: return new instance
end
Now let’s create a function that takes a reference to a Transport class (not an instance):
---@type transportCls ?????? -- <=== WHAT GOES HERE?
---@return Transport
function buildTransport(transportCls)
return transportCls.new()
end
If we were to annotate transportCls
as ---@type transportCls Transport
that would be incorrect, as that’s how you annotate a method that takes an instance of a class. Instances of the class do not have the required new
function.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:12 (10 by maintainers)
Top GitHub Comments
It may be popular, but it’s also unsafe, slower at runtime, and doesn’t support common OOP such as encapsulation.
I’m using another approach which is also quite common and documented on the same website you just linked:
http://lua-users.org/wiki/ObjectOrientationClosureApproach
The advantages are also documented on that same page.
Nonetheless, at the very least, static methods should never be accessible on instances, no matter which approach you’re using.
As @ice1000 and @adriweb say, your suggestion is rigorous and can be apply to oop based language, but not lua. It’s hard to decide whether or not a property/method is static.
Please take a look at documentation for annotation
@generic
.