score:4

Accepted answer

no there is no way to do this in c#. the compiler does not support capturing variables by value / const. nor can you convert a non-const value into a const value at runtime in this manner.

additionally the c# compiler only does constant folding during the initial compilation for known constant values. if it was possible to freeze a value at runtime into a constant it wouldn't participate in compiler constant folding because it happens at runtime.

score:0

x can't be a constant, because you're doing runtime magic to determine what it is. however, if you know that x and y don't change, try:

int x = magic(), y = moremagic();
int xovery = x/y;
return i => i + xovery;

i should also mention that even though the compiled il code for i => i + (x/y) will show the division, the jit compiler is almost certainly going to optimize this away.

score:0

one technique i used in vb2005 was to use a generic delegate factory to effect by-value closures. i only implemented it for subs rather than functions, but it could be done for functions as well. if extended in that way:

functionof.newinv()

would be a static function which would accept as parameters a function (described later), a t3, and a t4. the passed-in function should accept parameters of types t2, t3, and t4, and return a t1. the function returned by newinv would accept one parameter of type t2 and call the passed-in function with that parameter and the ones given to newinv.

the invocation would look something like:

return functionof.newinv((i,x,y) => i+x/y, x, y)

score:0

if you(like me) are creating some expression builder for sql queries you may consirer the following: first create a class variable, make it a constant and then access it like this:

var constant= expression.constant(values);
var start = expression.makememberaccess(constant, values.getmemberinfo(f => f.start));
var end = expression.makememberaccess(constant, values.getmemberinfo(f => f.end));

var more = expression.greaterthanorequal(memberbody, start);
var less = expression.lessthanorequal(memberbody, end);

score:2

the compiler doesn't do this type of "value caching". constant folding is done at compile time for constants only, not for readonly fields and certainly not for local variables which do not have a known value at compile time.

you have to make this yourself, but it has to stay a closure reference (since the value is in fact not determinable at compile time, which is why it is likely to be put in the closure when the expression is built):

int x = magic(), y = moremagic();
int xy = x/y;
return i => i + xy;

Related Query

More Query from same tag