My #1 Feature Request for C# vNext: Tuple Syntax

I love the C# language. It’s clear, yet succinct. I do most of my work in it. That said, there have always been a few “nagging” things that I think would improve the C# language. My number one request for C# vNext (C#6?) is a syntax for creating Tuples easily.

F# (more appropriately OCaml in this case – but I was first exposed to it in F#) has dedicated syntax for tuples. For example:

let foo = (1, 1)

That is a tuple, which in this case is a pair of Int32s. In this case, it becomes a System.Tuple<int,int>, and the types are inferred with type inference. Functions can take tuples as arguments, too. Here is a more complete example:

let foo = (1,1)
let add (a, b) = 
    a + b
let answer = add foo

C# would need to do the syntax a little differently, but I don’t think it is far fetched. The syntax for declaring a tuple could more-or-less stay the same. I don’t believe there is any construct in C#, other than a method invocation / declaration where commas are allowed between expressions immediately within a set of parenthesis. C#’s type inference also works a lot differently, so I would imagine you would have to specify the type of the tuple explicitly if it were a method argument. For example:

This is purely fictitious syntax. 

public static void Main(string[] args)
{
    var foo = (1, 1);
    var answer = Add(foo, "Performing add");
}

private static int Add((int a, int b), string message)
{
    Console.WriteLine(message);
    return a + b;
}

I’m no Anders, so I am sure there are problems associated with that, however I think the general idea would be very useful. How it would work with other C# features like optional parameters, generics, constraints, etc. I am not so sure.

This would be a very helpful syntax when you need to return more than one thing from a method, which you can’t do with an anonymous type. For example:

private static (int * int) GetAddends()
{
    return (1, 2);
}

Currently, I would either need to construct my own return type or use Tuple.Create(1, 2). Anonymous types can’t be returned unless they are with object, and lose all type information, or as a dynamic, which just feels a little weird.

Helpd Crashing – OSX Voodoo

While I have been enjoying my Mac as of late, I find myself doing some Googling to resolve odd crashes of services I have no special knowledge of.

Case in point: helpd

Helpd, or Help Daemon, is a daemon that runs in the background for help service.

Ultimately, I was presented with this problem:


Exception Type: EXC_BAD_ACCESS (SIGSEGV)<br />
Exception Codes: KERN_INVALID_ADDRESS at 0x000000000000003a

Occasionally, the daemon decides it’s time to run helpd.

We can determine what, exactly, triggers helpd by looking at the plist file for it. Specifically, it’s found in /System/Library/LaunchAgents/com.apple.helpd.plist. Here’s an interesting bit:


<key>WatchPaths</key><br />
<array><br />
<string>/Applications/</string><br />
<string>/Applications/Utilities/</string><br />
</array>

Ah ha, so now we know that helpd gets started with anything in the /Applications or /Applications/Utilities directory changes (There are other things that trigger it at well.)

I could consistently reproduce the crash by running touch /Applications from Terminal, which triggered the daemon.

I decided to pop open Console to see if anything was able to point me in the right direction. (Warning: Extreme sarcasm ahead) Of course, I got this wonderful bit of diagnostic information:

6/18/12 9:11:21.180 AM helpd: CarbonCore.framework: Unable to connect to coreservicesd. Things likely won’t work. Sorry.

Apology excepted. Moving on, I found this in the Console along the same time.

6/17/12 2:34:15.314 AM helpd: Error trying to unarchive index data. Original path
is </Users/kjones/Library/Caches/com.apple.helpd/Generated/com.apple.iWork.Keynote.help/English.helpindex>. Error: *** -initForReadingWithData: nil argument

Hm, so something is wrong with the content in a caches directory. Generally speaking, it’s safe to delete things in ~/Library/Caches, so I decided to delete ~/Library/Caches/com.apple.helpd, which is easy enough to do from the Terminal:


rm -rf ~/Library/Caches/com.apple.helpd

Afterwards, I ran touch /Applications, and helpd promptly spun up. After a few minutes of waiting, supposedly to rebuild the cache, it completed without any errors.

I’m happy enough that the problem is resolved, but I can’t help but wonder why helpd couldn’t resolve this problem itself. It knew there was something wrong with the cache, and logged it.