Passion * Technology * Ruthless Competence

Monday, August 21, 2006

Language Features I Wish C# Had - Tuples

Several languages, such as Python, have the concept of a Tuple built into the lanugage. One of things it's used for in Python is multiple return values. So you can call "return x,y" to return two values. Of course, C# can only return one. If you need to return more values, you have to define out parameters.

LINQ / C# 3.0 / VB 9 support the idea of anonymous types, which is similar to a tuple. The big difference is that, because they're anonymous, they can't leave the scope they're defined in. In other words, they're great within a function, but if you want to pass them out of your function type-safely, you have to define a non-anonymous type for them.

Interestingly enough, F# supports tuples, though it a bit of a hack. Since the CLR doesn't support tuples, F# basically defines different Tuple classes for up to seven tuple parameters (i.e. Tuple<t1,t2,t3,t4,t5,t6,t7>), For .NET 1.x, it's even worse - they have to define different type names (Tuple2, Tuple3, etc). Ugh.

Update - Robert Pickering pointed out that F#'s tuple implementation is entirely transparent inside of F#. He's right - I was writing from the perspective of a C# developer using F#'s implementation of tuples. Maybe I need to be looking closer at F#?

Posted By Harry Pierson at 11:01 PM Pacific Daylight Time
Tuesday, August 22, 2006 4:41:18 AM (Pacific Standard Time, UTC-08:00)
Harry,

How does a Truples diffe from an 'object' in .NET core. Could I not return an object from a routine, and define the structure dynamically? What about a collection? It seem that Truples appear to be nothing more then a UDT model, and that already is there.

I may be missing something, so if I am off base, just let me know :)

Tom
Tuesday, August 22, 2006 6:42:58 AM (Pacific Standard Time, UTC-08:00)
I don’t the implementation of tuples in F# is a hack; you are simply talking about the implementation details. If you are passing the tuple between two functions in F# then it is totally transparent you do not see the implantation details you describe. Sure if you pass a tuple between F# and C# then you are forced to deal with the Tuple classes that F# provides, and this is a little ugly, but I can see another way this could be supported in languages that don’t support tuples.
Tuesday, August 22, 2006 6:49:39 AM (Pacific Standard Time, UTC-08:00)
Tom,

Tuples are different to what you describe. A tuple is just a way of returning two values from a method with out having to define a type to contain them. For instance in F# you can say:

let my_tuple = 1, 2

To create a tuple, then to retrive a value from a tuple you would say:

let _, x = my_tuple

And then x would hold the value 2.

Wikipedia has a good definition:
http://en.wikipedia.org/wiki/Tuple

Cheers,
Rob
Tuesday, August 22, 2006 7:16:10 AM (Pacific Standard Time, UTC-08:00)
Robert, you're right. I think it feels like a hack if you use F# tuples outside of F#, which was the context of my post in the first place.
DevHawk
Comments are closed.
TechEd New Zealand
TechEd Australia

PDC08

patterns & practices
Summit 2008

Change Congress
Recent Bookmarks
Tags .NET Framework (2) ADO.NET (5) Agile (7) AJAX (3) Architecture (282) Guidance (6) Interop (2) Modelling (61) Patterns (7) Process (4) SOA (93) Web Services (5) ASP.NET (18) Battlestar Galactica (3) BI (2) BizTalk (4) Blogging (114) dasBlog (11) Podcasting (4) BPM (1) C# (6) C++ (4) Capitals (5) CardSpace (3) CLR (2) College Football (10) Comedy Central (1) Community (81) Concurrency (6) Consumer Electronics (1) Database (12) Dependency Injection (2) Development (115) C Plus Plus (1) Embedded (5) Lanugages (37) Media (2) P2P (11) Rotor (1) SharePoint (6) SOP (3) DIY (1) DLR (9) Domain Specific Languages (13) Durable Messaging (5) Dynamic Languages (9) Dynamic Silverlight (1) Education (3) Enterprise 2.0 (1) Entertainment (14) ETech (15) F# (39) Functional Programming (11) Game Development (2) Guidance Automation (3) Hardware (8) HawkEye (3) Hockey (29) Home Electronics (1) Home Network (4) Humor (5) IASA (1) Idempotence (3) infrastructure (5) Instrumentation (4) Integration (2) IronPython (15) IronRuby (4) Java (2) Job (3) LINQ (19) Lost (1) Master Data Management (1) Media 2.0 (6) Microsoft (28) MIX06 (2) Mobile Phone (1) Morning Coffee (166) Object Oriented (4) Office (5) Open Source (4) Open Space (2) Operations (3) Other (135) Art (1) Books (1) Family (30) Games (17) General Geekery (25) Home Theater (1) Movies (22) Music (20) Politics (3) Society (1) Sports (37) Working at MSFT (15) Parsing Expression Grammar (15) patterns & practices (2) PDC08 (1) Politics (39) PowerPoint (2) PowerShell (28) Presentation (5) Projects (1) HawkWiki (1) Python (3) Quote of the Day (4) Refactoring (1) Research (2) REST (18) Reuse (5) Robotics (1) Rome (5) Ruby (23) Sci-Fi (2) Scripting (4) Security (3) Service Broker (14) SharePoint (2) Silverlight (15) Social Software (1) Software + Services (2) Software Factories (11) Software Industry (1) Spark (1) SQL Server (2) Stephen Colbert (1) TechEd (7) TechEd06 (1) TechRec League (1) Television (6) Travel (6) Unified Client (1) Unit Testing (3) UX (1) Virtual PC (2) Visual Basic (1) Visual Studio (19) Volta (2) Washington Capitals (33) WCF (31) Web 2.0 (64) Web Services (5) WF (20) Windows Live (21) Xbox (1) Xbox 360 (51) XML (7) XNA (13)
Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion.