Object::foo syntax -- get a Method object?


#1

Hi,

At the moment, “obj::foo” means the same as “obj.foo”; that is,
“obj.send(:foo)”.

I propose that it be changed to mean “obj.method(:foo)” instead.

Although the obj.foo syntax is generally preferred, the obj::foo syntax
is commonly used for class methods in the standard library, so I assume
such a change is too incompatible for use in the 1.8 branch.

This would provide simpler access to method objects in case you want to
use them functionally, pass them around as blocks, etc. It would mean
that (as in Python, C++ and other languages) it’s as easy to refer to a
method object as to call the method, whereas currently that requires a
wordy method call.

We don’t need two such similar ways to call a method - the dot is fine.

What do you think?

Cheers,
Dave


#2

Dave B. wrote:

At the moment, “obj::foo” means the same as “obj.foo”; that is,
“obj.send(:foo)”.

I propose that it be changed to mean “obj.method(:foo)” instead.

I’m just not sure that method objects are accessed frequently enough to
justify such a syntax. Furthermore, if we were to introduce a
shorthand syntax for accessing method objects, why not use a syntax that
isn’t currently in use?

obj->foo
obj~foo

etc.

Daniel


#3

On 6/2/06, Dave B. removed_email_address@domain.invalid wrote:

Hi,

At the moment, “obj::foo” means the same as “obj.foo”; that is,
“obj.send(:foo)”.

They’re not the same:

$ irb
irb(main):001:0> class Test
irb(main):002:1> class InnerTest
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> Test.InnerTest
NoMethodError: undefined method `InnerTest’ for Test:Class
from (irb):5
from :0
irb(main):006:0> Test::InnerTest
=> Test::InnerTest
irb(main):007:0>

wordy method call.

method (six characters) is not wordy at all. Method objects are
convenient, but I don’t think that justifies special syntax.

We don’t need two such similar ways to call a method - the dot is fine.

What do you think?

Cheers,
Dave

I say leave it as it is.


#4

On Fri, 2006-06-02 at 19:25 +0900, Daniel S. wrote:

obj->foo
obj~foo

I’ve been watching the related thread on the newsgroup, and I agree with
Daniel, in that I’ve not used #method enough to make it worthwhile
having a special syntax for it, and even if we did I think :: is
confusing enough already.

(Aside: Current ruby 1.9 has an experimental lambda syntax involving the
-> operator).


#5

This is taken from facet/kernel/method.rb:

#–

NOTES:

First Class Methods

I think the best solution would be using the notation

::ameth. This would require some minor changes

to Ruby, but with few backward incompatabilites if

parantheticals revert back to the actual method invocation.

Although this later stipulation means capitalized methods

would not be accessible in this way b/c they would intefere with

constant lookup. It’s a trade off.

Current Proposed Alternate

----------------- ------------------


Foo.Bar() method call method call method call

Foo.Bar method call method call method call

Foo.bar() method call method call method call

Foo.bar method call method call method call

Foo::Bar() method call method call 1st class

method

Foo::Bar constant lookup constant lookup constant

lookup

Foo::bar() method call method call 1st class

method

Foo::bar method call 1st class method 1st class

method

Then again this dosen’t address bound versus unbound!!!

Which do you prefer?

#++

class Object

Easy access to method as objects, and they retain state!

def hello

puts “Hello World!”

end

p method!(:hello) #=> <Method: #hello>

def method!(s)
( @methods ||= {} )[s] ||= method(s)
end

end