Listening To Reason

random musings about technologies by Andy Norris

23 August 2006

I want first-class functions with my c#

With all of the cool functional improvements to the c# language in 2.0 and 3... uh... 3.5, it seems like it's about time for c# to leave the kingdom of nouns and hop the express train to having first-class functions. We have delegates, anonymous functions, continuations, and lots of other great functional elements in the language, but we don't have functions that can stand on their own, without having a class as a crutch.

Here's what I want:


public function int Foo(int bar) {
  return bar * bar;
}

int baz = Foo(42);
That's not asking all that much, is it?

But I thought everything was supposed to be an object!

So don't these insidious little functions just muck up the works for everyone? They don't even have classes! What the heck is that about?

Well, here's the thing. It's already possible to write the above code sample in c# -- it's just ugly and borked:


public static class Foo {
  public static int Invoke (int bar) {
    return bar * bar;
  }
}

int baz = Foo.Invoke(42);

It might be a little homely, but there's nothing evil or insidious about it, right? It's just a plain old static method. And really, is it more homely than a Factory or Strategy pattern?

Why would anyone in their right mind prefer


public abstract class SortStrategy {
  public abstract void Sort (int[] items);
}

public class QuickSortStrategy : SortStrategy {
  public QuickSortStrategy() 
  {
    // don't do anything here because this object is totally useless
  }

  public override void Sort (int[] items) {
    ...
  }
}

SortStrategy sorter = new QuickSortStrategy();
int[] myarray = { 1, 3, 7, 4, 2 };
sorter.Sort(myarray);

to


public delegate void Sort(int[] items);

public function void QuickSort(int[] items) {
  ...
}

Sort sortfn = QuickSort;
int[] mylist = { 1, 3, 7, 4, 2 };
sortfn(mylist);

And yes, there are already more concise ways to represent this in c# than by using an actual strategy pattern. For example, you could use the same syntax I used up above with Foo.Invoke:


public delegate void Sort(int[] items);

public static class QuickSort {
  public static void Sort (int[] items) {
    ...
  }
}

Sort sortfn = QuickSort.Sort;
int[] mylist = { 1, 3, 7, 4, 2 };
sortfn(mylist);

This is mostly as good as having the ability to declare a function. Here's the thing, though: if you're just using a class as a container for declaring a function, why bother? Just write it as a damn function, and be done with it.

It would be an easy piece of syntactic sugar to add to the language -- as far as the CTS is concerned, it could bytecode compile to Foo.Invoke anyway. We've come so far with the functional capabilities of c#, I just want to finish the job.

Tags: , ,

9 Comments:

At 6:00 AM, Anonymous Anonymous said...

It's got em: anonymous delegates. They even work as lexical closures.

 
At 11:14 PM, Anonymous Anonymous said...

I am so happy to get some hero gold and the hero online gold is given by my close friend who tells me that the hero online money is the basis to enter into the game. Therefore, I should buy hero gold with the spare money and I gain some hero money from other players.

 
At 12:52 AM, Blogger Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.

 
At 3:43 AM, Anonymous Anonymous said...

i dont know if there suitalbe to write something good website i meet here, for i hope every body could have a good shopping experience online.

here i want to introduce this website, mainly selling nfl jerseys,
ghd, christian louboutin, and air max 95.

they offer 24 hours online service, and very very good customer service.
i buy the air max 90 shoes online on the website, nice design and good quality. that is why i strongly introduce you here.

it is not a advertisment, just share my shopping experience with everybody. hope you have a nice day, and could enjoy the good shopping.

 
At 3:44 AM, Anonymous Anonymous said...

i dont know if there suitalbe to write something good website i meet here, for i hope every body could have a good shopping experience online.

here i want to introduce this website, mainly selling nfl jerseys,
ghd, christian louboutin, and air max 95.

they offer 24 hours online service, and very very good customer service.
i buy the air max 90 shoes online on the website, nice design and good quality. that is why i strongly introduce you here.

it is not a advertisment, just share my shopping experience with everybody. hope you have a nice day, and could enjoy the good shopping.

 
At 1:22 AM, Anonymous Microsoft Office 2007 said...

Office 2010
Microsoft Office 2010
Microsoft word
Office 2007
Microsoft Office
Microsoft Office 2007
Office 2007 key
Office 2007 download
Office 2007 Professional
Outlook 2010
Microsoft outlook
Microsoft outlook 2010
Windows 7

 
At 12:57 PM, Anonymous Cialis said...

I hope this worked out in the end for you!

 
At 5:40 AM, Anonymous Computer said...

Wow! This can be one particular of the most beneficial blogs we have ever arrived across on this subject. Actually Magnificent. I am also a specialist in this topic so I can understand your hard work.

Computer Accessories | buy webcam

 
At 3:39 PM, Anonymous price per head said...

Thanks so much for this post. A combination of advice that I've heard before but always bears repeating; plus new tips that I really ought to consider

 

Post a Comment

Links to this post:

Create a Link

<< Home