Good examples of value types
Andrei Alexandrescu via Digitalmars-d
digitalmars-d at puremagic.com
Thu May 7 07:36:56 PDT 2015
On 5/7/15 4:15 AM, Dejan Lekic wrote:
> On Tuesday, 5 May 2015 at 20:40:59 UTC, Luís Marques wrote:
>> Hi,
>>
>> For a comparison with the Java language, I'm trying to come up with
>> some good examples of custom types that should be value types (but
>> that must be ref types in Java). I think the most obvious ones are
>> numeric types. So BigNum, MyNum, etc. are good examples because
>> programmers are used to numeric types being value types, and having
>> them suddenly become a ref type just because it's MyNum instead of
>> long is really annoying. Still, could you come up with some type that
>> would really benefit from being a value type but that isn't numeric
>> (or otherwise similar)?
>>
>> Thanks for your help!
>>
>> Luís
>
> To add to what others have said - whenever you think you will benefit
> from stack-allocation. Read this article:
> http://www.ibm.com/developerworks/library/j-jtp09275/
> Java is good at escape analysis. But I find it really useful to be able
> to specify a type that will always be allocated on the stack (unless you
> really want it on the heap).
(Not sure I'm not being confused about the topic.) A coworker mentioned
they have big problems in Java whenever they need to return more than
one thing from one function.
Reference types don't compose nicely that way. In a language with valye
types, the juxtaposition of two items (value or reference) is a va;ue.
In Java, it's a reference (so you need to allocate a new object etc.)
Andrei
More information about the Digitalmars-d
mailing list