How would i go about naming a static function?
Up to your style. It’s just about using .
instead of :
, but the name itself is up to you.
so are services using static methods then?
Arguably some are static, but for Roblox services the :
syntax in all methods is most likely used to avoid confusion for new scripters. It would also make singleton cleanup when teleporting and such much simpler.
Considering the fact that a function is only considered a method if it belongs to a particulate object/instance, how is a function considered a method in the first place if it’s under a class?? Let alone a static method.
For example:
function Gui:ShowWepInfo()
MAIN.WepInfo.Visible = true
end
^ this is not acting on an object in the first place. It’s acting on a ‘GUI’ class
To answer this question for future readers of this thread, see here: OOP, Organization, and Tips - #18 by IdiomicLanguage
To summarize, here are some excerpts from the above:
Static ‘methods’ are an anti-pattern, a residual of functional programming. Much like how this code:
if (this.isActive == true) {
...
}
should actually be this in OOP:
this.isActive.ifTrue(() => {
...
})
Booleans should be classes themselves. That primitive types are not classes and objects themselves is a functional programming feature. (both primitive types and static members are mentioned in Why is Java not a purely object oriented language)
If by primitive you mean a built in type the language has with no other specification than it being “global”, then you could probably discard this.
If we’re assuming primitive is a built in data type that has a 1:1 (or is representative of such) with the memory it inhabits, I think this is a wrong simplification. A limitation of dynamically typed languages, I would say. Primitive types do not have to be based on a class blueprint (and should not be!) to behave as an object. This is what a compiler is for.
Ownership of methods is actually just really a Lua/JS/whatever else that is dynamically typed concept as far as I know. Often times, an instance “owns” a method because it’s attached somewhere in memory to that instance and resolved at runtime (such as C++ RTTI or Lua’s table lookup). This sort of behavior should only be expected from dynamically typed languages or places in code which may exhibit polymorphism.
To say primitives existing is a functional programming residue is probably not quite right. Static method resolution is probably one of the best tools that languages have to offer which really discredit that.
Booleans could have method overloading which is resolved at compile time, but them being a class or instance of a class often times doesn’t look right.
Ownership of methods is an OOP concept. A duck quacks and a car drives. Lisp, the near incarnate version of lambda calculus itself (as much of a functional language as they come) is dynamically typed and contains no concept of method ownership. Likewise, Rust (a statically typed language), has the concept of ownership and it supports OOP concepts like traits. “Unowned” or static methods is a functional programming concept, where actions are defined in terms of other actions on data. OO languages are defined by objects’ relations with other objects; each objects’ actions influence other objects by calling their actions. The middle man, the object owning the action, is an OOP concept.
To have anything besides objects doesn’t sound right to me. In pure OOP, everything is an Object.
I’m referring to owning a method dynamically; the actual concept of dynamic ownership is only present when polymorphism or some other generic container is involved. Often times, the compiler is the one resolving references internally.
Just because int
is not a class, doesn’t mean it can’t have methods. It isn’t an “object”, but nothing is stopping the compiler from translating (1).Thing()
to Thing(1)
when it sees it. If a data type is a true primitive (referring to 1:1 memory) then it can not do dynamic method resolution, which is the Lua style of OOP or use abstract/virtual inheritance.
The point is, your primitives don’t need to be full-class objects to behave like one, and as such, can remain that definition of primitives.
Whats the difference between a static method and a static function? Examples please?
Static method and static function are usually used interchangeably; they usually don’t rely on a self
/this
object and don’t require an instantiation to happen.
I’d add that a static method still belongs to a class. In other words, you still need to use the dot operator on the class to access the method. On the other hand while sometimes static function is used to describe a static method (methods are like a special case of functions) a static function is more often used to describe functions that are independent from any other entity, like at the top of a file outside of a class. Java doesn’t allow for static functions, but C and Lua do.
Another example to illustrate the meaning of static can be found in shared libraries. If you wrote a shared library in C (.dll in windows or .so in linux) then a static variable would be one which is shared between all of the processes loading the library. Normally, each process would just borrow the shared library’s code and use their own data section, but with static variables in the library, each process can access the same memory location through library functions. It is independent of the number of processes using the library.