Tuple: to be a value type or not to be

1 view
Skip to first unread message

Paweł Różański

unread,
Aug 2, 2008, 9:15:06 AM8/2/08
to nemer...@googlegroups.com
I have written a simple test:

#pragma indent
using Nemerle.IO

def cmp(a : object, b : object)
print(a == b)

def tuple2 = (0,0)
cmp(tuple2, tuple2)
def tuple4 = (0,0,0,0)
cmp(tuple4, tuple4)

which gives: FalseTrue as a result

...and I was completely confused that actually two-tuple is value type
but four-tuple isn't.

Yep, these are immutable object so actually it's a little difference,
unless someone want to compare by reference. I suppose performance
consideration is a purpose of this. Is it worth it (feels awkward)?

Take care,
--
Paweł Różański

VladD2

unread,
Aug 2, 2008, 9:57:29 AM8/2/08
to nemer...@googlegroups.com
2008/8/2 Paweł Różański <pawel.r...@gmail.com>:

You use reference comparison. A tuple with 2-3 args represent (in
.NET) as ValueType - struct.
If you cast ValueType to object ValueType will be boxed and reference
comparison give False.

Use Equals method:
print(a.Equals(b))

Paweł Różański

unread,
Aug 2, 2008, 6:06:46 PM8/2/08
to nemer...@googlegroups.com
> You use reference comparison. A tuple with 2-3 args represent (in
> .NET) as ValueType - struct.
> If you cast ValueType to object ValueType will be boxed and reference
> comparison give False.
Yes, but behaviour based on tuple arg count seems like comparing
bigger fruits by colour, and smaller ones by taste :)

I just wanted be sure that this is expected behaviour.

It seems so :) thanks
--
Paweł Różański

Ķaмȋļ ๏ Şκaļşκȋ

unread,
Aug 3, 2008, 2:33:12 PM8/3/08
to nemer...@googlegroups.com
W dniu 3 sierpnia 2008 00:06 użytkownik Paweł Różański
<pawel.r...@gmail.com> napisał:

Yes, this is by design. I think we even specify somewhere that 2 and 3
tuples are structs.

> --
> Paweł Różański
>
> >
>

--
Kamil Skalski
http://nazgul.omega.pl

Reply all
Reply to author
Forward
0 new messages