Singleton class methods

David A. Black wrote:

Hi --

David A. Black wrote:

Hi --

At least for "def Foo.f1" and "class << Foo" it is not the same one: one holds "A=10" and another -- "A=20".

But only one of them is a singleton class; the other is simply Foo
itself :slight_smile: When you do def Foo.m, 'self' is Foo; but when you do class

That's where a slight misunderstanding might have occurred: I was calling "Foo itself" an original singleton class (isn't it? ;-)), and the one created by "class << Foo" -- an anonymous singleton class of Foo (i omitted word "anonymous" from my original reply, sorry).

I realize that we should probably be saying 'virtual class' instead of
'singleton class' (at least, that's what shows up in the error
messages when you try to instantiate one) -- but either way, Foo isn't
one :slight_smile: It's just a regular class.

David

Hmm, I like "virtual class" much better. I used term "singleton" for describing Foo's class simply to reflect that there's only one instance of class "Class" representing "Foo". Seems like my terminology was misleading.

It's not a very useful usage -- you'd have to call every object in
Ruby a 'singleton' then :slight_smile:

'Singleton class' has always had the problem that there's also the
Singleton module, so 'singleton class' can also mean a class that
includes that module. 'Virtual class' strikes me as possibly causing
confusion, or false expectations, due to the use of that term for very
different things in other languages. But I don't have a perfect term

Neither Java, nor C++ have term "virtual class", rather "abstract class". "virtual" may be member functions in C++ (or implicitly "non-virtual" in Java if you specify "final" keyword for a method). Also, "virtual" may be inheritance specification in C++.

So at least for those two languages, term "virtual class" is vacant ;-).

for it by any means. (I think there was a discussion about this on
ruby-core or somewhere lately... couldn't find it in a quick search.)

David

Gennady.

···

On Wed, 11 Aug 2004, Gennady wrote:

On Wed, 11 Aug 2004, Gennady wrote:

"Gennady" <gfb@tonesoft.com> schrieb im Newsbeitrag
news:411966D7.2040101@tonesoft.com...

David A. Black wrote:
> Hi --
>
>
>
>>David A. Black wrote:
>>
>>
>>>Hi --
>>>
>>>
>>>
>>>
>>>>>>At least for "def Foo.f1" and "class << Foo" it is not the same

one: one

>>>>>>holds "A=10" and another -- "A=20".
>>>>>
>>>>>
>>>>>But only one of them is a singleton class; the other is simply Foo
>>>>>itself :slight_smile: When you do def Foo.m, 'self' is Foo; but when you do

class

>>>>
>>>>That's where a slight misunderstanding might have occurred: I was
>>>>calling "Foo itself" an original singleton class (isn't it? ;-)),

and

>>>>the one created by "class << Foo" -- an anonymous singleton class of

Foo

>>>>(i omitted word "anonymous" from my original reply, sorry).
>>>
>>>
>>>I realize that we should probably be saying 'virtual class' instead

of

>>>'singleton class' (at least, that's what shows up in the error
>>>messages when you try to instantiate one) -- but either way, Foo

isn't

>>>one :slight_smile: It's just a regular class.
>>>
>>>
>>>David
>>>
>>
>>Hmm, I like "virtual class" much better. I used term "singleton" for
>>describing Foo's class simply to reflect that there's only one

instance

>>of class "Class" representing "Foo". Seems like my terminology was
>>misleading.
>
>
> It's not a very useful usage -- you'd have to call every object in
> Ruby a 'singleton' then :slight_smile:
>
> 'Singleton class' has always had the problem that there's also the
> Singleton module, so 'singleton class' can also mean a class that
> includes that module. 'Virtual class' strikes me as possibly causing
> confusion, or false expectations, due to the use of that term for very
> different things in other languages. But I don't have a perfect term

Neither Java, nor C++ have term "virtual class", rather "abstract
class". "virtual" may be member functions in C++ (or implicitly
"non-virtual" in Java if you specify "final" keyword for a method).
Also, "virtual" may be inheritance specification in C++.

So at least for those two languages, term "virtual class" is vacant ;-).

I'd still opt for using "singleton class" since that is the term used by
most publications (news and books) about the matter.

That would mean:

fooSingle = class Foo
  class <<self
    self
  end
end

f = Foo.new
fooInstanceSingle = class <<f; self; end

fooSingle = class Foo
  class <<self
    self
  end
end

=> #<Class:Foo>

f = Foo.new

=> #<Foo:0x10184920>

fooInstanceSingle = class <<f; self; end

=> #<Class:#<Foo:0x10184920>>

fooSingle

=> #<Class:Foo>

fooInstanceSingle

=> #<Class:#<Foo:0x10184920>>

fooInstanceSingle === f

=> true

fooInstanceSingle === Foo.new

=> false

fooSingle === Foo

=> true

Now we have:

Foo is a class.
fooSingle is the singleton class of the instance Foo (i.e. the class).
f is an instance of Foo.
fooInstanceSingle is the singleton class of the instance f.

Kind regards

    robert

···

> On Wed, 11 Aug 2004, Gennady wrote:
>>>On Wed, 11 Aug 2004, Gennady wrote: