Active patterns can be viewed as "anonymous DU's" in some sense, so that's one option:

1
2
3
4
5
6
7
 
let (|Slow|Fast|) speed = ...

let eat speed food = 
   match speed with
   | Slow -> ...
   | Fast -> ...

Also, there are named arguments - they don't work for let-bound fn's, but for "programming in the large" where it matters, I think you're getting what you need here with:

1
2
3
4
5
 
type EatSpeed = | Slow | Fast
  member this.eat(what:string, ?speed: EatSpeed = Slow) = ...
...
eat( "mushrooms", speed=Fast)

So you still have to define EatSpeed (which is good), and you solve your issue of "not very clear what the purpose of the second argument is".

By on 6/18/2010 8:37 AM ()

They might be useful (but really, is it that much work to add the line

type EatingSpeed = | Slowly | Fast

to the code? I think this is a rare case to want this; I recall wanting it only a handful of times in the past five years). It isn't immediately clear how this would work with name-resolution (what is the scope of 'Fast'?), or more general pattern matching (can I match a tuple of (Slowly,x)?), or probably a few other issues I haven't thought of. Anonymous types like this present a variety of new tooling questions/challenges (what does the hover-tooltip say over 'Fast'?), though none insurmountable. To quote Eric Lippert:

[link:blogs.msdn.com]

I am asked "why doesn't C# implement feature X?" all the time. The answer is always the same: because no one ever designed, specified, implemented, tested, documented and shipped that feature. All six of those things are necessary to make a feature happen. All of them cost huge amounts of time, effort and money. Features are not cheap, and we try very hard to make sure that we are only shipping those features which give the best possible benefits to our users given our constrained time, effort and money budgets.

In addition, every feature 'adds concept count', makes every book on F# a page heavier, etc. New features start of with -1000 points. So yeah, this feature might be nice, but I doubt we would add a feature just to save a line of code in this rare case.

Regarding code formatting, see [link:cs.hubfs.net]

By on 6/17/2010 6:17 PM ()

It isn't immediately clear how this would work with name-resolution (what is the scope of 'Fast'?), or more general pattern matching (can I match a tuple of (Slowly,x)?), or probably a few other issues I haven't thought of. Anonymous types like this present a variety of new tooling questions/challenges (what does the hover-tooltip say over 'Fast'?)

Ah, I never thought about all that. I sorta naively thought that if anonymous types (records, in specific) would ever be implemented, this might come almost as a freebie.

Thanks for the link on formatting.

By on 6/18/2010 5:50 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper