by fragmede · 8y ago · 0 comments
Several languages have usage I'd consider "pronoun-like". Perl has $_ and Bash has $?. Unfortunately, "$?" doesn't exactly roll off the tongue, and there's some truth to criticisms of Perl readability. That may be related to the the quantity of the that it supports though.
self in various languages supporting OO is a fairly common example of what I'd consider good pronoun-like usage - if I'm inside a method that belongs to a class, what self refers to is accessible and largely unambiguous.
>>> class Foo:
... def __init__(Foo, x):
... Foo.x = x
...
>>> foo = Foo(5)
>>> foo.x
5
The variable name `self` is a convention as is `cls`. You can name the first argument to a method whatever you please. The same is true for method receivers in Go: type Foo struct {
X int
}
func (Foo Foo) Bar() int {
return Foo.X * 2
}
That will compile and perform as expected. Not sure about Java/C# but in Javascript you can bind `this` to another variable if you'd like: var Foo = {
x: 5,
foo: function() {
Foo = this
return Foo.x * 2;
}
}