News for hackers. Stuff that matters.
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by bromagosa on Friday March 30, 2012 @08:11AM (#3775539)
All-OOP? So functions will be objects too? And classes? And contexts?

Not meaning to start a flame, just saying the title's a bit inaccurate IMHO...

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by kirinan on Friday March 30, 2012 @08:16AM (#3775555)
I think similar to C# where the primitives are objects is what they want, terribly misleading title I agree. However, I cringe when I think of what this does to performance. The memory overhead of objects in java is pretty high (relative) while primitives are very memory efficient. If they can make the primitives just as efficient, it would be awesome, but I guess its a wait and see. I don't currently mind using the static classes on the object representation of the primitives to do things.
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by chrisb on Friday March 30, 2012 @08:36AM (#3775628)
In C# primitives are not objects, but they can be boxed into objects.

So if you have a class:

  public class X {
    public Int32 i;
    public Int32 j;
    public Int32 k;
  }
Then it takes 12 bytes + single object overhead (which in MS .NET I believe is two pointers).

But if you assign a primitive (e.g. Int32) to an variable of type 'object' then it will be boxed and then require the object overhead.

A neat thing about C#/.NET is that generics are baked deeply into the platform, so using primitive (actually, any value-type struct) types as generic arguments will not require object memory or casting overheads.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by bad_user on Friday March 30, 2012 @09:21AM (#3775783)
Well, actually, in C# struct instances ARE objects allocated on the stack, instead of the heap.

A parallel can be drawn with C++ in which the user of a class decides on declaration/initialization where to store the object (either on the stack, or on the heap). The difference with C++ is that in C# the author of the class decides where instances of it should be stored, so this decision happens when the class is declared, not when it's used.

Otherwise there are few differences between structs and classes and all differences stem from the differences in storage. For example struct instances must be passed by value and not reference, because by definition stack-allocated values are short-lived and playing with references to stack-allocated values is dangerous. Structs must also have an implicit constructor because stack-allocated values cannot be NULL (logically, you need a reference to represent NULL).

In my experience, all discussions about what is or isn't an object are counter-productive.

What really bugs me about Java is that you cannot build your own types that behave just like the built-in types. For instance you cannot override operators like "+" (which works for primitives or Strings), you cannot override [] (which works for arrays), you cannot declare other stack-allocated structures, arrays are reified and yet you cannot declare your own reified data-structures and so on. The presence of primitives doesn't bother me as much as lacking the means to build my own primitives.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by maximilianburke on Friday March 30, 2012 @12:29PM (#3776751)
> Well, actually, in C# struct instances ARE objects allocated on the stack, instead of the heap.

You have it backwards, they're not value types because they're allocated on the stack, structs are allocated on the stack because they're value types.

See Eric Lippert's article "The Stack is an Implementation Detail": http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-s...

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by bad_user on Friday March 30, 2012 @03:35PM (#3777699)
So no, I don't have it backwards.

Also Eric Lippert nails it when he answers the question of why reference types are not stack allocated and value types are ... “because they can”.

I do agree with the article, but with all due respect to Eric Lippert, C#/.NET was meant to be reasonably fast for all kinds of user-land applications and if structs weren't added, then they had to add special cases (primitives) for dealing with integer and floating point arithmetic, just as Java did.

I do agree that structs have semantic value, but the implementation itself allows the available primitives to be described in terms of structs, which is a really elegant and cost-effective solution to a problem that the JVM engineers are trying to solve with complicated tricks like escape analysis. If you remove the performance/efficiency benefit, there isn't a lot of value left in structs - at least nothing that can't be solved by immutable data-structures and/or a better type system.

Nice article btw, thanks.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by moonchrome on Friday March 30, 2012 @10:28AM (#3776068)
>In C# primitives are not objects

http://msdn.microsoft.com/en-us/library/system.valuetype(v=V...

>Data types are separated into value types and reference types. Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated. Both reference and value types are derived from the ultimate base class Object.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by technomancy on Friday March 30, 2012 @11:35AM (#3776438)
I assumed they were talking about tagged fixnums, which have primitive-like performance but can still be treated like objects. https://blogs.oracle.com/jrose/entry/fixnums_in_the_vm
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by CodeMage on Friday March 30, 2012 @08:18AM (#3775560)
Classes are already objects. I don't know whether Java 8 will finally introduce function as a data type, but if it does, I'm pretty sure they won't do it the way Ruby blocks work (i.e. block is not an object).
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by EdiX on Friday March 30, 2012 @08:53AM (#3775684)
I'd rather they didn't. It would be redundant with inner classes that are already there, I don't like this piling up of features. It would be more interesting if they allowed inner classes to close over non-final variables.
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by ramblerman on Friday March 30, 2012 @09:39AM (#3775849)
An inner class is the ugly half-brother of a closure. 99% of the time they're used they define just one method.

When they are used for more than 1 method implementations, well ... they shouldn't be inner classes.

They just exist to fill the gap of having no closures. I say this as a long time and affectionate java user but project lambda in JDK8 is long overdue.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by yummyfajitas on Friday March 30, 2012 @11:05AM (#3776251)
When they are used for more than 1 method implementations, well ... they shouldn't be inner classes.

Why not? Very often, a class will have some data members which are pretty much meaningless elsewhere. Why should the class used to represent that data be exposed elsewhere?

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by EdiX on Friday March 30, 2012 @10:00AM (#3775936)
They are there, they have been there for over ten years, much code exists that needs them to be there. You can't take them out of the language.

More syntax sugar for them would be good, adding first class functions IMHO isn't good.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by rukkyg on Friday March 30, 2012 @08:27AM (#3775594)
Aren't methods already an object of class java.lang.reflect.Method? Do you mean you should be able to do something like foo(PrintStream.println.method) like you can do foo(PrintStream.class)?
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by eternalban on Friday March 30, 2012 @08:36AM (#3775632)
No, a class's methods are not instances of java.lang.reflect.Method. The latter is simply a 'reflection', per the package name, of first class JVM construct e.g. a reflection on a method.
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by riffraff on Friday March 30, 2012 @11:13AM (#3776305)
would you care to explain why this is not enough? You can pass them around, invoke them, get their properties.

The interface may be bad but I'm not sure what is missing.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by eternalban on Friday March 30, 2012 @01:35PM (#3777117)
In the original comment, I was merely addressing the misunderstanding of the GP regarding the reflection packages constructs.

The interface is your basic (irreducible) disconnected/unbound procedure invocation API, with all the positive/warts associated. I agree that in principle, we have enough information in the reflection data structure to allow a (specific) JVM implementation to provide a non-standard 'method object' feature. Consensus, possibly? (Good question, really. C. Nutter is one to hit with that one.)

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by c4n4rd on Friday March 30, 2012 @08:14AM (#3775547)
This is a very good point. If functions become objects, interfaces would become literally useless. Somehow it seems that it would not be the case since this would be a drastic change (even thought getting rid of primitives are as well.)
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by CodeMage on Friday March 30, 2012 @08:19AM (#3775563)
Literally useless? Would you like to elaborate on how specifying a contract that has to be implemented is suddenly useless because you have function types?
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by yummyfajitas on Friday March 30, 2012 @08:23AM (#3775583)
I think the experience of Haskell is relevant here. Once Haskell introduced first class functions, everyone stopped using typeclasses.

[edit: apparently my sarcasm was too subtle. HOF and typeclasses are both vitally important pieces of Haskell, which are orthogonal to each other. I'm pretty sure both were in Haskell from day 1. Some code for which both are essential:

    class Monad m where
      (>>=) :: m a -> (a -> m b) -> m b
      (>>) :: m a -> m b -> m b
      return :: a -> m a
      fail :: String -> m a

]
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by CodeMage on Friday March 30, 2012 @08:35AM (#3775621)
I'm genuinely curious: is this because Haskell is generally hard to learn[] or because of some inherent property of Haskell that makes type classes unnecessary when you have first class functions?

[] This statement is based purely on my own experience. I find the Haskell documentation a bit unfriendly with its "academic" style. The content is great, but the form makes it a bit hard to assimilate. Monads were especially painful.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by calpaterson on Friday March 30, 2012 @10:03AM (#3775948)
I don't think typeclasses were in from day one, they were added quite late in the development of the language (but some years before H98). I don't think they're in Miranda, for example.

Here's a paper introducing them.

http://dl.acm.org/citation.cfm?id=227700

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by SamReidHughes on Saturday March 31, 2012 @11:55PM (#3782596)
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by yummyfajitas on Friday March 30, 2012 @10:37AM (#3776104)
I stand corrected. I guess Haskell before H98 was a very different language than it is today.
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by xyzzyz on Friday March 30, 2012 @08:45AM (#3775657)
Once Haskell introduced first class functions, everyone stopped using typeclasses.

What do you mean? From what I've seen, everyone uses typeclasses in Haskell.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by hythloday on Friday March 30, 2012 @09:06AM (#3775721)
Not sure I agree with the GP's point, but I believe he's arguing that when fulfilling the contract of a fictional MouseClickListener:

  interface MouseClickListener {
    void mouseClicked(MouseEvent e);
  }
one often uses an anonymous class to do so:

  void init() {
    mouse.setClickListener(new MouseClickListener() {
      void mouseClicked(MouseEvent e) {
        println(e);
      }
    });
  }
the exact effect could be achieved with a reference to a named function (if Java supported them, which presumably it will once "everything is an object"):

  void init() {
    void mouseClick(MouseEvent e) { println(e); }
    mouse.setClickListener(mouseClick);
  }
given that interfaces are simply syntax for function routing, when you have the ability to reference functions by first-class types (i.e. you have the ability to determine function routing yourself), the set of things that you can only sensibly do with interfaces is a lot smaller.

This is how C# delegates work, right? Any Java -> C# programmer want to comment on whether they rely less on interfaces now and what they use them for?

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by bermanoid on Friday March 30, 2012 @10:18AM (#3776027)
First class functions pretty much have no bearing on whether or not interfaces are useful. Sure, one use case for inner classes is to implement interfaces in-place in situations where what you really want to do is just pass a function, and in those cases it's probably better to use a function instead, but the equivalence doesn't go the other way.

There are plenty of cases where you really need to pass an object to a function and know that the object supports multiple operations, for instance take a look at Map<K,V>: http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

There are 14 methods there, and when I write code that takes a Map object, I really mean it - I'm not just using the interface as a hack because I want a function pointer, I need an object that supports all of those methods, and I'm probably going to be using several of them.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by eropple on Friday March 30, 2012 @09:20AM (#3775777)
I bounce between Java and C#. I'm not sure at all what he's talking about. I use interfaces all the time in C#. I even specify delegate properties (as distinct from events, when I don't want them to be multicast) in interfaces.

I think both concepts are probably independent of one another.

Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by cynicalkane on Friday March 30, 2012 @08:21AM (#3775574)
Clojure has both function objects and interfaces (called 'protocols'). In fact, it encourages the use of interfaces while strongly discouraging the traditional object inheritance model. Most people who've tried it AFIAK consider Clojure a well-designed language.
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by jemeshsu on Friday March 30, 2012 @09:05AM (#3775714)
Clojure, Golang, Scala and Rust all implement similar interface.
Re:Java 9 with GPU processing, Java 10 will be all-OOP witho... by Roboprog on Friday March 30, 2012 @12:13PM (#3776678)
While I agree that I hate interfaces with one method, what about interfaces that actually define several methods (e.g. - perhaps a driver for something that needs to do operations like "open", "close", "get", "put")?

Interfaces still serve a purpose bundling together related operations with replaceable implementations. They're just a nuisance for single-purpose functions/functors (e.g. - "run").