views:

260

answers:

6

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.

Eg:

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.

A: 

I am not sure what you would need this for - a tuple is simply a structure that holds different types of data. If you really want named properties you will have to create a custom type or create an anonymous type on the fly.

I am not aware of any statically typed language that would support this either but C# certainly doesn't.

Andrew Hare
+2  A: 

You mean, something like Python's collections.namedtuple? Well, Python (the current versions, 2.6 and 3.1) supports them;-). But seriously, I don't know of a statically typed language having them as a built-in.

Alex Martelli
+2  A: 

Whats wrong with using structs or classes in C#?

public class SpeedDistance{
  public double Speed;
  public int Distance;
}
Martin v. Löwis
Well, the public field is a bad idea, of course... but `public double Speed {get;set;}` would do ;-p
Marc Gravell
I like my tuples to be immutable. And that requires readonly variables instead and a constructor, which just starts getting too long.
+1  A: 

I'm not sure if this is exactly what you're looking for, but in Haskell, you can have a record with specified names and types:

data Movement = Movement { speed :: Double, distance :: Int } deriving (Show)

main = do
    print $ Movement 3.14 100
    print Movement {distance = 5, speed = 2.1}
    print Movement {speed = 9, distance = -4}

output:

Movement {speed = 3.14, distance = 100}
Movement {speed = 2.1, distance = 5}
Movement {speed = 9.0, distance = -4}

But it's technically not a tuple. Haskell does have tuples, but they aren't name-able as far as I know.

This really isn't far from a simple struct in any C-derived language, either. Maybe I missed something in the question.

Mark Rushakoff
+3  A: 

In C#, you have anonymous types; these are similar but have their own limitations:

var result = new { Speed = 12.4, Distance = 8, Caption = "car 1" };

However, it is hard to consume these as a caller, unless you use "cast by example" (brittle), reflection, or dynamic. Of the three, the last is the most appetising.

dynamic result = GetSomething();
Console.WriteLine(result.Speed);
Console.WriteLine(result.Distance);

In most cases, it would be a better idea just to use a regular class - but this approach does have some practical uses; for example, look at how they are used in ASP.NET MVC to pass around configuration information simply and conveniently (which would otherwise require a dictionary). A bit like how jQuery allows you to pass options as properties on an object.

Marc Gravell
This appears to be the closest to what I was asking for (at least in c#).
A: 

Eiffel allows named tuples.

James Phillips