Not meaning to start a flame, just saying the title's a bit inaccurate IMHO...
Not meaning to start a flame, just saying the title's a bit inaccurate IMHO...
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.
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.
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...
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.
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.
The interface may be bad but I'm not sure what is missing.
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.)
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.
More syntax sugar for them would be good, adding first class functions IMHO isn't good.
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?
[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
][] 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.
What do you mean? From what I've seen, everyone uses typeclasses in Haskell.
Here's a paper introducing them.
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?
I think both concepts are probably independent of one another.
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.
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").