4
$\begingroup$

In Lua, a function is no different from a method, except that a method has this as the first parameter, and calling a method on an object automatically passes the object as the first parameter. In C++, there is special handling of functions versus member functions.

The advantage of the first option is simplicity and flexibility, as there is no 'unnecessary' distinction between methods and functions. But are there any disadvantages?

$\endgroup$
8
  • 2
    $\begingroup$ What even is the alternate option? In every language methods are just functions $\endgroup$
    – mousetail
    Jul 5, 2023 at 18:08
  • $\begingroup$ In Swift, there is a calling convention difference: methods always put self in a dedicated register, so the first argument (besides self) is in the same place either way. Not sure whether that's worth writing an answer for. $\endgroup$
    – Bbrk24
    Jul 5, 2023 at 18:08
  • 2
    $\begingroup$ @mousetail It reads to me like they're asking about Java void foo() + static void bar() vs Python def foo(self) + def bar(). $\endgroup$
    – Bbrk24
    Jul 5, 2023 at 18:09
  • $\begingroup$ In python you need @staticmethod for static methods $\endgroup$
    – mousetail
    Jul 5, 2023 at 18:24
  • $\begingroup$ Lua doesn't actually have instance methods unless you count syntactic sugar - functions that take this as a first parameter still require a different syntax a:b() instead of a.b() $\endgroup$
    – RubenVerg
    Jul 5, 2023 at 18:31

2 Answers 2

2
$\begingroup$

Maybe one advantage of having function/method be fundamentally different is, at the syntax level, making it easy to prove that your code doesn't perform method calls.

When reading C++ code, visually one can instantly tell if a call is going to be to a function or to a method:

f(a, b);
a.g(b);

This might be useful to C++ programmers, as they sometimes want to know when a call could go through a vtable, because of the potential cost of pointer indirection.


I imagine that the reason it's like this is, back when C++ was made, OOP was still controversial and not well understood, things like UCS or extension methods hadn't been invented, and people just didn't want to confuse this brand new "method" concept (and its weird virtual/override magic) with a regular function.

Today, it just seems like a good idea to have a way to abstract over whether something is a method call or a function call, because they are so similar.

Maybe I'm completely wrong though.

$\endgroup$
1
$\begingroup$

In languages like Java, there are no "free standing" functions. Every function is a method, hence there is no reason to specify explicitly that you are calling a method.

If there are both methods and "free standing" functions like C++ or Python, this.call() looks like a useful way to show that the method is being called. However developer that defines and then calls mostly methods, not functions, may anyway prefer to mark explicitly that this is a free-standing function rather than the method call. This can be done by explicitly specifying the namespace: my_functions::my_function(x).

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .