Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Distinction between a class and instance (of a class) - static vs instance methods/variables

See original GitHub issue


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?(重现步骤?)

  1. 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 = name

---@return MyClass
    --todo: return new instance
  1. Instantiate an instance of the class (or @type MyClass a variable):
local instance =
  1. 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:

screen shot 2018-10-18 at 2 43 04 am

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
    --todo: return new instance

function transport:move()end

---@class Car : Transport @Car extends Transport
local car = {}
function car:move()end

---@return Car
    --todo: return new instance

---@class Ship : Transport @Ship extends Transport
local ship = {}

---@return Ship
    --todo: return new instance

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)

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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

Benjamin-Dobellcommented, Oct 17, 2018

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:

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.

tangzxcommented, Oct 18, 2018

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 understand that we only provide support for the most popular and widely used approach of simulating class-based object-oriented programming in a prototyping language.

I believe there’s also no way to type annotate a reference to a class.

Please take a look at documentation for annotation @generic.

---@generic T : Transport @ <<< define a generic parameter type T
---@param transportCls T @ <<< @param not @type
---@return T
function buildTransport(transportCls)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Difference between Static methods and Instance methods
Static methods, variables belongs to the whole class, not just an object instance. A static method, variable is associated with the class as ......
Read more >
Static methods vs Instance methods in Java - GeeksforGeeks
Instance method(s) belong to the Object of the class, not to the class i.e. they can be called after creating the Object of...
Read more >
Static methods vs Instance methods in Java - Tutorialspoint
Static methods can't access instance methods/variables directly while instance methods can access static variables and static methods directly.
Read more >
Java Tutorials: Difference between Instance vs Static Keyword
Static vs Instance keyword: Instance keyword: - They are related to object - It can be instance variable & methods - Instance variables...
Read more >
Static and Instance Methods, Variables, and Initialization Code
Differences Between Apex Classes and Java Classes ... A static method or variable doesn't require an instance of the class in order to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found