Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any official ways to write an Immediately Invoked Function Expression?

Tags:

Something like this:

var myObject = new MyClass() {     x = " ".Select(y =>     {         //Do stuff..         if (2 + 2 == 5)             return "I like cookies";         else if (2 + 2 == 3)             return "I like muffins";         //More conditions...         else             return "I'm a bitter old man";     }) }; 

I realize Select is not intended to be used this way. But yeah, what are some other ways to do the same thing?

like image 307
elite5472 Avatar asked Aug 16 '13 18:08

elite5472


People also ask

What is an IIFE immediately invoked function expression )? Can you give an example?

An Immediate-Invoked Function Expression (IIFE) is a function that is executed instantly after it's defined. This pattern has been used to alias global variables, make variables and functions private and to ensure asynchronous code in loops are executed correctly.

What is IIFE immediately invoked function expression in JS?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.

What is the valid syntax of declaring IIFE function?

The following expression is called an immediately invoked function expression (IIFE) because the function is created as an expression and executed immediately: (function(a,b){ return a + b; })(10,20); Code language: JavaScript (javascript) This is the general syntax for defining an IIFE: (function(){ //...

Does declared function execute immediately?

Function expressions are evaluated during execution. So you can't execute function declarations immediately because even variables don't exist yet and other code that the function might depend on hasn't been executed either.


2 Answers

For real code make it a function... For entertainment purposes C# equivalent of JavaScript IIFE is more direct than Select:

var myObject = new MyClass() {    x =((Func<int>)(() => {return 2;}))(),... 
like image 70
Alexei Levenkov Avatar answered Sep 18 '22 07:09

Alexei Levenkov


I'm surprised no one's mentioned this yet, but you could use the Lazy<T> class:

var myObject = new MyClass() {     x = new Lazy<string>(() =>     {         //Do stuff..         if (2 + 2 == 5)             return "I like cookies";         else if (2 + 2 == 3)             return "I like muffins";         //More conditions...         else             return "I'm a bitter old man";     }).Value // <-- Evaluate the function here }; 

Alternatively, if you want to avoid having to specify the return type anywhere (as you do with new Lazy<string> because constructors do not support type inference), you can implement a simple generic method like this:

public static T Eval<T>(Func<T> func) {     return func(); } 

And then you can call like this:

var myObject = new MyClass() {     x = Eval(() =>     {         //Do stuff..         if (2 + 2 == 5)             return "I like cookies";         else if (2 + 2 == 3)             return "I like muffins";         //More conditions...         else             return "I'm a bitter old man";     }) }; 

Update: C#7 introduces local functions. These are not really IFFEs, but they may solve a variety of related issues. For example:

var myObject = new MyClass() {     x = GetX() };  string GetX() {     //Do stuff..     if (2 + 2 == 5)         return "I like cookies";     else if (2 + 2 == 3)         return "I like muffins";     //More conditions...     else         return "I'm a bitter old man"; } 

The key here is that GetX can be declared within the same method as myObject share the same scope as it.

like image 35
p.s.w.g Avatar answered Sep 21 '22 07:09

p.s.w.g