开发者

What languages other than Python have an explicit self?

It seems a bit weird that Python req开发者_如何学运维uires you to explicitly pass self as the first argument to all class functions. Are there other languages that require something similar?


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.


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.


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.


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.


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++.


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


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]


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 ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜