agiliq
agiliq

Reputation: 7738

What languages other than Python have an explicit self?

It seems a bit weird that Python requires you to explicitly pass self as the first argument to all class functions. Are there other languages that require something similar?

Upvotes: 4

Views: 671

Answers (8)

mikera
mikera

Reputation: 106351

Clojure isn't an OOP language but does use explicit self parameters in some circumstances: most notably when you implement a protocol, and the "self" argument (you can name it anything you like) is the first parameter to a protocol method. This argument is then used for polymorphic dispatch to determine the right function implementation, e.g.:

(defprotocol MyProtocol
  (foo [this that]))

(extend-protocol MyProtocol String
  (foo [this that]
    (str this " and " that)))

(extend-protocol MyProtocol Long
  (foo [this that]
    (* this that)))

(foo "Cat" "Dog")
=> "Cat and Dog"

(foo 10 20)
=> 200

Also, the first parameter to a function is often used by convention to mean the object that is being acted upon, e.g. the following code to append to a vector:

(conj [1 2 3] 4)
=> [1 2 3 4]

Upvotes: 0

linked
linked

Reputation: 1278

By explicit, do you mean "explicitly passed as an argument to each class function"? If so, then Python is the only one I know off-hand.

Most OO languages support this or self in some form, but most of them let you define class functions without always defining self as the first argument.

Upvotes: 5

Jason Sundram
Jason Sundram

Reputation: 12524

Modula-3 does. Which is not too surprising since Python's class mechanism is a mixture of the ones found in Modula-3 and C++.

Upvotes: 2

grrussel
grrussel

Reputation: 7339

The programming language Oberon 2 has an explicitly named but not explicitly passed 'this' or 'self' argument for member functions of classes (known as type bound procedures in Oberon terminology)

The following example is an Insert method on a type Text, where the identifier 't' is specified to bind to the explicit 'this' or 'self' reference.

PROCEDURE (t: Text) Insert (string: ARRAY OF CHAR; pos: LONGINT);
BEGIN ...
END Insert;

More details on Object Orientation in Oberon are here.

Upvotes: 4

Ahmad Dwaik
Ahmad Dwaik

Reputation: 971

many object oriented languages if not all of them

for example c++ support "this" instead of "self"

but you dont have to pass it, it is passed passively

hope that helps ;)

Upvotes: -1

Scott Evernden
Scott Evernden

Reputation: 39926

any Object-Oriented language has a notion of this or self within member functions.

Upvotes: 1

Boojum
Boojum

Reputation: 6692

Depending on your point of view, Lua. To quote the reference: "A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once." You can also define methods using either notation. So you could say that Lua has an optional explicit self.

Upvotes: 4

Steve Gilham
Steve Gilham

Reputation: 11277

F# (presumably from its OCAML heritage) requires an explicit name for all self-references; though the name is any arbitrary identifier e.g.

 override x.BeforeAnalysis() = 
    base.BeforeAnalysis()
    DoWithLock x.AddReference

Here we're defining an overriding member function BeforeAnalysis which calls another member function AddReference. The identifier x here is arbitrary, but is required in both the declaration and any reference to members of the "this"/"self" instance.

Upvotes: 3

Related Questions