views:

249

answers:

2

The VS2010 Beta 2 F# compiler always complains about my usage of the when keyword, even when using copy-pasted code which is supposed to work, such as either of these snippets. For instance, this is the error I get when trying to execute a very trivial expression:

"Error FS0010: Unexpected keyword 'when' in expression. Expected '->' or other token. "

[for i in 1..50 when i < 10 -> i]  
---------------^^^^
+7  A: 

You should use the yield keyword now. Like that:

[for i in 1 .. 50 do if i < 10 then yield i]
Stringer Bell
Thank you, that works. But what's wrong with 'when'?
Martin
According to the F# keyword reference (http://msdn.microsoft.com/en-us/library/dd233249(VS.100).aspx), when is used only in pattern matches and generic type constraints. Possibly a change to the language since the article you cited came out?
itowlson
I just can advise you to be a little careful when using snippets from old blog posts (minor language changes could have occurred).
Stringer Bell
+9  A: 

You want

[for i in 1..50 do
    if i < 10 then
        yield i]

The 'short' syntax with 'when' was removed a while back. See

http://blogs.msdn.com/dsyme/archive/2008/08/29/detailed-release-notes-for-the-f-september-2008-ctp-release.aspx

and look for "compact sequence expressions" in that document.

Brian