Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Languages that allow named tuples

I was wondering if there are any languages that allow for named tuples. Ie: an object with multiple variables of different type and configurable name.

E.g.:

public NamedTuple<double:Speed, int:Distance> CalculateStuff(int arg1, int arg2)

var result = CalculateStuffTuple(1,2);

Console.WriteLine("Speed is: " + result.Speed.ToString())
Console.WriteLine("Distance is: " + result.Distance.ToString())

I could conceive how a dynamic could support such a feature. The static languages I usually swim in (like c#) can do a Dictionary, but that's not type safe unless all items are of the same type. Or you can use a Tuple type, but that means you have fixed names of the members (Var1, Var2, etc).

You could also write a small custom class, but that's the situation I'd like to avoid.

I could imagine a macro processing language could script something like that up for you in a static language, but I don't know of such a language.

This comes out of my answer from this question about return types.

like image 556
ligos Avatar asked Sep 29 '09 03:09

ligos


1 Answers

Old question, but in need of a better solution I think.

You can get named parameters by taking advantage of the Tuple type, but wrapping it in a custom named type that wraps .Item1, .Item2, etc. in meaningful property names.

I too hate the fact that Tuples have unnamed parameters that make code unreadable, but can't ignore the time it saves having to implement IComparable, IStructuralEquatable, etc on your own so that you can safely use your structs as a dictionary key, for instance.

I think this is a very pleasant compromise:

public class Velocity : Tuple<double, double, string>
{
    public Velocity(double Speed, double Direction, string Units) : base(Speed, Direction, Units) { }
    public double Speed { get { return this.Item1; } }
    public double Direction { get { return this.Item2; } }
    public string Units { get { return this.Item3; } }
}

Now instead of this garbage:

Tuple<double, double, string> myVelocity = new Tuple<double, double, string>(10, 2.34, "cm/s");
System.Diagnostics.Debug.Print("Speed: " + myVelocity.Item1);
System.Diagnostics.Debug.Print("Direction: " + myVelocity.Item2);
System.Diagnostics.Debug.Print("Units: " + myVelocity.Item3);

You get to do this:

Velocity myVelocity2 = new Velocity(10, 2.34, "cm/s");
System.Diagnostics.Debug.Print("Speed: " + myVelocity2.Speed);
System.Diagnostics.Debug.Print("Direction: " + myVelocity2.Direction);
System.Diagnostics.Debug.Print("Units: " + myVelocity2.Units);

And you still benefit from all the wonderful tuple features that let you use it as a complex key in dictionaries, etc.

The only downside is that if you only planned on using this tuple within the scope of a single method, you have to declare a type within the scope of that method's containing class. For most applications, I don't think that's a problem.

like image 164
Alain Avatar answered Oct 25 '22 02:10

Alain