xsharp.eu • What is a tuple literal?
Page 1 of 1

What is a tuple literal?

Posted: Wed May 06, 2020 1:32 pm
by pemo18
Hello X#-Team,

This is more out of curiosity but it could also be interesting for other X# developers.

What is a tuple literal? I asume from the help file and by experimenting with it that X# supports Tuples like in C# but I have no clue how to instantiate one. The help file is mentioning that I would have to use a tuple literal.

Tuples could be an alternative when a method shall return more than one value. I would prefer tuples over ref parameters.

May be its worth mentioning in the X# language talk tomorrow.

Kind regards,
Peter

What is a tuple literal?

Posted: Wed May 06, 2020 3:06 pm
by Chris
Hi Peter,

Is this really mentioned in the help? Where? I might be missing something, but I do not think this is implemented yet, but Robert may need to correct me of course! (Here or in an online session :) )

A tuple literal is something like this:

VAR oMyTuple := (1,"abc")

which creates a tuple with two generic parameters, an INT and a STRING and assigns their values. I think we have decided that this is the syntax that will be used in X# as well (same to c#).

What is a tuple literal?

Posted: Wed May 06, 2020 3:36 pm
by pemo18
Hi Robert,

It was a false alarm. The System.Tuple class was only used as an example for being able to use >> for declaring generics in the section Changes in 0.2.2.

And there are several error mesages like XS8138 that states "use the tuple syntax to define tuple names" etc.

What striked me was error XS8181 - 'new' cannot be used with tuple type. Use a tuple literal expression instead.

But thats ok. It would be definitely a helpful feature in a future release.

Regards,
Peter

What is a tuple literal?

Posted: Wed May 06, 2020 3:49 pm
by FFF
Chris wrote:VAR oMyTuple := (1,"abc")
which creates a tuple with two generic parameters, an INT and a STRING and assigns their values. I think we have decided that this is the syntax that will be used in X# as well (same to c#).
While in curiosity mode: I read that you may at max set 8 params in a tuple. And "the main purpose of tuples is to safely return multiple values from a method without resorting to out parameters".
In what differs this from a crippled Vo-style array?
EDIT: did further reading, ok, there's quite more behind ;) main difference being tuples "value type" with mutable elements - makes me wonder, what trickery they do behind the scenes to make this work...

What is a tuple literal?

Posted: Wed May 06, 2020 4:13 pm
by Chris
Hi Karl,

I would say that the main difference is that the tuple parameters are strongly typed and the length of the parameters is known at compile time and they can even have "names". So you cannot access by mistake an element of the VO array that does not exit, or is of the wrong type (because of a mistake in the element number) etc.