Lambda expressions with statement block body will not compile  
Author Message
RaimondB





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

According to the C# specs this should work:

Func<int,int> f = x => { Console.WriteLine(x); return x;} ;

However I get the compilation error "Invalid expression term '{'"

What am I doing wrong here




Visual Studio 200830  
 
 
Matt Warren





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Possibly you did not create a LINQ project and are using the 2.0 compiler

 
 
Fabrice MARGUERIE MVP





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Well no, contrarily to what has been announced, the May CTP does not support this syntax.


 
 
Matt Warren





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

The May CTP does in fact support this syntax. If you are seeing an error message to the contrary either you are not using a LINQ project or you are seeing the error show up in the IDE's error list. If its the latter, its likely due to another error existing in the source. The IDE unfortunately still does background builds with the 2.0 compiler, so when 3.0 features are involved it will often show errors that are not actually errors. Until this is fixed, to get the correct list of errors look in the output window. This shows the errors coming out of the 3.0 compiler.

If you are actually seeing this error showing up in the output window and you're certain its a LINQ project, its likely a bug. (you can check by creating a new LINQ project and try to compile the sample there.)



 
 
RaimondB





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Thanks, I was indeed the Error list. Compilation did in fact succeed :-)

 
 
Panks2905





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Hi,
I have gone thru this thread and feel my simple one line lambda expression should compile.
I start a LinQ project. and try to write a lambda statement -
(int x)=>{ x=0; return x++;};
or
(int x)=>{return x+1;}

But I get the following error in "Output window". What mistake I am making

------ Rebuild All started: Project: multiLambdaExpr, Configuration: Debug Any CPU ------
C:\Program Files\LINQ Preview\Bin\Csc.exe /noconfig /nowarn:1701,1702 /errorreportStick out tonguerompt /warn:4 /defineBig SmileEBUG;TRACE /reference:"C:\Program Files\LINQ Preview\Bin\System.Data.DLinq.dll" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:"C:\Program Files\LINQ Preview\Bin\System.Data.Extensions.dll" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:"C:\Program Files\LINQ Preview\Bin\System.Query.dll" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"C:\Program Files\LINQ Preview\Bin\System.Xml.XLinq.dll" /debug+ /debug:full /optimize- /outSurprisebj\Debug\multiLambdaExpr.exe /target:exe Program.cs Properties\AssemblyInfo.cs

Program.cs(37,13): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Done building project "multiLambdaExpr.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========



Thanks
Pankaj

 
 
zproxy





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

It defenetly works with Visual Studio Orcas Beta 1. I suggest you to uninstall the 2006 May Preview and use the latest version.

 
 
Panks2905





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Hello ,

Is it possible to create a System.Expression.Expression<T> Object which contains a lambda Statement body

I tried with this code -

Expression<Func<int, int>> expr = {Console.WriteLine(x); return x;}

It simply refuse to work. I think there must be some other way to create such an expression.

Please guide me.

Thanks

Pankaj


 
 
Panks2905





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Ok I found the answer. Expression can not have a Lambda statement block.
Stupid Me Wink

 
 
JennerJenner





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Hmm, first it would be more reasonable if the statement goes like:



Expression<Func<int, int>> e = x => {var y = x+2; return y};


This is not allowed. I don’t understand the reason of put this constraint on Lambda Expression. A lot of wonderful programming techniques from e.g. Scheme quotation mechanism or Haskells lazy evaluation are disabled by this constraint.

Is there a design issue or is it a feature for the next Beta version for giving more expressiveness for lazy code

Any good explanations of not expanding Expression Trees possibilities


 
 
Matt Warren - MSFT





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Statements will likely appear in the next version after Orcas. I would have added it sooner but they tie my hands behind my back when I write too much code. I'm learning to type with my nose.



 
 
Aaron Erickson





PostPosted: LINQ Project General, Lambda expressions with statement block body will not compile Top

Funny - I was running into this same restriction today, in my efforts to write a program using the Expression API in my metalinq project. Too bad - having a "sequence" node would be really, really useful.