News for hackers. Stuff that matters.
Re:Perl 11 by chrisseaton on Tuesday October 23, 2018 @08:18PM (#18288628)
> what do you mean a class is an object

I think that's pretty standard for object orientated languages. I can't find of any where it isn't the case off the top of my head. Is it not the case in Perl's object orientated support?

And why wouldn't it be an object? Seems more surprising for classes to be an exception.

Re:Perl 11 by rileyphone on Tuesday October 23, 2018 @11:03PM (#18289377)
It means they lack a metaobject protocol.
Re:Perl 11 by Fellshard on Tuesday October 23, 2018 @08:25PM (#18288668)
People coming from Javaville or C#land definitely don't have that kind of abstraction (and yes, the usual rebuttal is that they're procedural, not OO).
Re:Perl 11 by jcranmer on Tuesday October 23, 2018 @09:18PM (#18288929)
Classes are objects in Java. You can even create new classes at runtime in Java or use java.lang.reflect.Proxy to make existing objects implement different interfaces.

I once did this in a class project, including even manually creating the bytecode at runtime. Ironically, it's probably the most understandable and maintainable of the crazy metaclass-style reflection I've done (although that's probably mostly due to the fact that the scope was much more limited: I was only mirroring a set of interfaces into a different namespace to work around some stupid limitation in a different library).

Re:Perl 11 by Fellshard on Tuesday October 23, 2018 @09:32PM (#18288983)
You can, yes, but not nearly in the same way.
Re:Perl 11 by chrisseaton on Tuesday October 23, 2018 @08:37PM (#18288735)
> what do you mean a class is an object

> People coming from Javaville or C#land definitely don't have that kind of abstraction

What do you mean? A class is an object in both Java and C#.

Re:Perl 11 by Fellshard on Tuesday October 23, 2018 @09:31PM (#18288979)
Not exactly.

What you see as the Class type in Java, at least, is simply a representation of a class. It can tell you about a class, but it doesn't represent an actual thing you can manipulate, change, or use. I can't add a method to that class, or add a field, or manipulate its instances beyond basic method lookup and dispatch.

In something like Ruby or Smalltalk, a Class is an object. It can be changed live, altered, adapted, cloned, or otherwise used just like another object; and the application will change its behavior accordingly, right there on the spot.

I'd recommend reading up on prototype inheritance, as that is the model generally used in the latter languages.

Re:Perl 11 by chrisseaton on Tuesday October 23, 2018 @09:42PM (#18289025)
You’re telling me that class objects in Java are immutable while they’re mutable in Ruby. That’s orthogonal to whether they’re objects.

> I'd recommend reading up on prototype inheritance

Thanks but I’ve already literally got my PhD in metaprogramming in Ruby and object model implementation in Java. Ruby doesn’t use anything like prototype inheritance.

Re:Perl 11 by kazagistar on Wednesday October 24, 2018 @11:10AM (#18293092)
Its more then that... It's not even inmutable, not really. There are some things, like privaCy of access, that can be mutated by manipulating Java class objects.

That said, maybe the difference here is how often the janky metaprogramming of classes interferes with day to day peogramming; a java programmer might have never seen this stuff, moreso then a ruby one?

Re:Perl 11 by Fellshard on Wednesday October 24, 2018 @05:58PM (#18296809)
Yeah, the correction re: prototype inheritance is a good one.

That said, there is a qualitative difference in what we call 'classes' in those two environments, which is part of why I think there's confusion. Class in Java is a fairly different concept than a Class in Smalltalk, Ruby, or JS.

Re:Perl 11 by jonathankoren on Tuesday October 23, 2018 @08:41PM (#18288754)
Java has Class which is an Object.
Re:Perl 11 by repsilat on Tuesday October 23, 2018 @09:03PM (#18288844)
Honest question: Can you write

    Class foo = String;
in Java? If so I guess classes really are objects, if not I'd say "there are some objects that represent (or wrap) some classes somehow."
Re:Perl 11 by chrisseaton on Tuesday October 23, 2018 @09:15PM (#18288915)
I think what you mean is that classes are referenced by a separate namespace in Java and aren’t in Ruby.

That’s completely orthogonal to whether they’re objects or not.

You can keep fiction and non-fiction in separate bookshelves if you want but when you get either off the shelf it’s still a book.

Re:Perl 11 by repsilat on Wednesday October 24, 2018 @02:08AM (#18290081)
I guess I'm not sure what "object" means in Java, then.

In languages I'm used to, an object is a type of value, and values are things that you can store in (or refer to with) variables.

The only vaguely object-like feature of Java classes I can think of is that you can look up (static) methods/attributes on them with a dot.

Or is there more? You can't call `String.toString()`, so `String` isn't an instance of something that inherits from `Object`. I'm at a bit of a loss here... There obviously exist "class objects", but they're not normally what people refer to when they talk about classes. I'd say Java classes are almost purely lexical constructs (though I could be wrong there -- again, I don't know much about Java...)

Re:Perl 11 by chrisseaton on Wednesday October 24, 2018 @02:31AM (#18290157)
> In languages I'm used to, an object is a type of value, and values are things that you can store in (or refer to with) variables.

Class theStringClass = String.class;

Class[] myArrayOfClasses = new Class[1];

myArrayOfClasses[0] = theStringClass;

callSomeOtherMethod(theStringClass);

> You can't call `String.toString()`

String.class.toString() => "class java.lang.String"

theStringClass.toString() => "class java.lang.String"

myArrayOfClasses[0].toString() => "class java.lang.String"

somethingThatReturnsAClass().toString() => "class java.lang.String"

> `String` isn't an instance of something that inherits from `Object`

Class theObjectClass = Object.class;

theObjectClass.isAssignableFrom(theStringClass) => true

> I'd say Java classes are almost purely lexical constructs

They aren't - here's the source code for the Class class.

https://github.com/Project-Skara/jdk/blob/c2105ced865fba11fb...

Here's the source code for the toString method we were calling

https://github.com/Project-Skara/jdk/blob/c2105ced865fba11fb...

Re:Perl 11 by akdas on Tuesday October 23, 2018 @09:09PM (#18288879)

    Class foo = String.class;
(Though `Class` is actually a generic type, so you'd want to include the type parameter in actual code.)
Re:Perl 11 by repsilat on Wednesday October 24, 2018 @02:10AM (#18290087)
Sure, I'd say that fits my second suggested wording -- `String.class` is an object that represents or wraps the `String` class.
Re:Perl 11 by chrisseaton on Wednesday October 24, 2018 @02:59AM (#18290250)
How do you tell the difference between an object that is the String class, and an object that represents the String class? If you can call .toString on it, and you can compare it to other classes, and you can call .newInstance, then how is it more of a representation than being the actual thing?
Re:Perl 11 by repsilat on Wednesday October 24, 2018 @11:52AM (#18293477)
> How do you tell the difference between an object that is the String class, and an object that represents the String class?

Hah, I think "an object that is the String class" begs the question :-). I think that `String.class` is an object because you can do objecty things with it (call methods, assign it to a variable etc) and `String` is not an object because you can't do any of those things with it.

To me they're not the same kind of thing, though they're intimately, inseparably and exclusively related. I personally would say even if

  new String()
was just syntactic sugar for

  String.class.newInstance()
, and all other "bare" uses of `String` were similarly shorthands for operations on `String.class`, I'd still hesitate to say that `String` itself was an object.

My logic is probably less useful than yours, though. I'd say "`A` is a `class`, but `a` is an instance of `Class`" and in your system where `A` and `a` are "the same thing" there's no difference between being a `class` and being an instance of `Class`. Because the things appear in different namespaces (as you say), and are related one-to-one, we could as well say they are two ways to refer to "the same thing", with different operations possible "depending on the phrasing."

Shrugs, it doesn't come naturally to me, but it's at least a self-consistent system.

Re:Perl 11 by lmm on Wednesday October 24, 2018 @04:54AM (#18290756)
In java you have to call the special and awkward .newInstance rather than being able to use standard "new". Whereas in e.g. Python I can equally well do

    >>> str("foo")
    'foo'
    >>> x = str
    >>> x("foo")
    'foo'
and the "builtin" str and "userspace" x are equally first-class.
Re:Perl 11 by kazagistar on Wednesday October 24, 2018 @11:15AM (#18293131)
Sure, the ergonomics are different. One is a statically compiled language and one is dynamic. But it's still an object.
Re:Perl 11 by lmm on Wednesday October 24, 2018 @12:02PM (#18293607)
If I write

    new String("foo");
in Java, how do I get an object that can take the place of String in that expression? String.class has some relationship with the "String" in the code I quoted, but it's not the same thing; the thing that I apply new to is not a thing I can call methods on, and the thing that I can call methods on is not a thing I can apply new to.