1

I have some variables which were given through ref keyword in my function. I want to use ref-variables in lambda expression without using local variables exactly.

I know that C# can't merge lambdas and ref-variables in work. But doesn't exist any perversion to make ref-variables work in lambda-expression?

Pseudo-code (wanna-be C#):

T MyFunction<T>(ref T x, ref T y)
{ 
    return (Func<T>) ( () => ( (100 * (x - y)) / x ) (); 
} 

I want it:

1). Be generic.

2). Accept as arguments the generic-types by references.

3). Using two options upper make it work with lambdas.

4). Return the result with using generic.

5). Be an organic-programming code :) Joke.

And call smth. like that:

int x = 21, y = 9;
int result = MyFunction<int>(ref x, ref y);

And with other types etc...

double x = 21.5, y = 9.3;
double result = MyFunction<double>(ref x, ref y);

Pity, that such constructions and desires C# couldn't give me, so I'm going to look at Lisp/Haskell ( maybe F# ).

(PS) => ("So I want to have a hard sex with C# possibilities exactly as you can see.")

Secret
  • 2,627
  • 7
  • 32
  • 46
  • 3
    i think you mean *conversion* instead of *perversion* ? – Thousand Jan 06 '13 at 19:44
  • @Thousand no, perversion exactly, because ref doesn't work with lambdas and I want not to use local/temp variables or another buffer options to make it work with lambdas. Only what I want is plain work between ref and lambdas, which isn't able by default. – Secret Jan 06 '13 at 19:46
  • 2
    Perversion: any of various means of obtaining sexual gratification that are generally regarded as being abnormal. .. are you sure? :p – Thousand Jan 06 '13 at 19:49
  • 1
    possible duplicate of [Cannot use ref or out parameter in lambda expressions](http://stackoverflow.com/questions/1365689/cannot-use-ref-or-out-parameter-in-lambda-expressions) – nawfal Jan 06 '13 at 19:51
  • 4
    @Thousand perversion is polymorphic word. It references to any not normal solution and may relate not only to the sex-thematics. – Secret Jan 06 '13 at 19:51
  • sidenote for @Thousand: Perverted: Deviating from what is considered right and correct. Probably quite applicable here. ;) – Kjartan Jan 06 '13 at 19:53
  • I think I understand what you mean, but just to be sure - could you possibly provide some code to examplify the problem? (The part that is unclear to me is why you can not / do not want to use local variables - maybe I'm missing something...?) – Kjartan Jan 06 '13 at 19:55
  • @Kjartan void MyFunction(ref T x, ref T y) { return (Func) ( () => ( (100 * (x - y)) / x ) (); } //smth like that – Secret Jan 06 '13 at 19:59
  • @Kjartan the part of not using local/buffer/temp vars is clear is a crystal! Just don't want to allocate new memory and want to work with existing vars in memory without assigning/allocating new vars for temp. – Secret Jan 06 '13 at 20:01
  • 3
    No. See nafwal's link: http://stackoverflow.com/questions/1365689/cannot-use-ref-or-out-parameter-in-lambda-expressions – siride Jan 06 '13 at 20:07
  • @OlegOrlov What you are trying to do is pass references instead of values. You say that you don't want to assign/allocate new variables but what you aim at is simply passing two references to a method (which in your case should be anonymous). Bottom line: you still get to pass parameters, except that in your case the whole concept is out of discussion. – e_ne Jan 06 '13 at 20:12
  • @Eve it's not out, it's in the discussion. Where is it out? – Secret Jan 06 '13 at 20:13
  • @OlegOrlov Out of discussion as in, you can't pass a reference to an anonymous method, period. Most you could do is work in an unsafe context, pin the stack address of your variable(s), pass it as a closure or as a parameter with your lambda and then extract the variable out of it. All of this while making sure that your variable is blittable and clearly, without using generics in your anonymous method's declaration. – e_ne Jan 06 '13 at 20:16
  • @Eve C++ 11 and functional langs as Lisp could do what I want... Pity , that my lovely C# isn't able right now to make such perversions... – Secret Jan 06 '13 at 20:18
  • 1
    This might be the single weirdest question I've come across in StackOverflow. – Acidic Jan 06 '13 at 20:28
  • @Acidic try to use LSD when you writthins a code ( I mean not sorting algorithm by the way :) ) and then you can see super-weirdest pictures in your minde while using both programming & lsd. Black-humour. :) – Secret Jan 06 '13 at 20:30
  • This question is absolutely moot. – Vitaliy Jan 06 '13 at 20:32
  • 1
    @Vitaliy In what place? Try not to combine your personal feelings with SO-standart to questions, it isn't moot by the definitions. – Secret Jan 06 '13 at 20:32
  • @OlegOrlov are you trying to accomplish something practical? Are you asking why it can't be done? – Vitaliy Jan 06 '13 at 20:35
  • @Vitaliy re-read please the topic, you can see what actually I've aksed here ( it's even highlighted ) – Secret Jan 06 '13 at 20:37
  • Even if you solve your issue with `ref` parameters. You won't be able to use generics in the way you desire. There is no way to use operators with them (unless you use `dynamic`) or constrain a generic to be a numeric type. – Mike Zboray Jan 06 '13 at 20:40
  • @OlegOrlov Can you post what your _actual_ code is within `MyFunction`? As it stands, there is _no point in creating the lambda locally then invoking it_. You should just execute and return the expression within your lambda directly and ditch the lambda syntax. Especially if this is coming from your previous question here: http://stackoverflow.com/questions/14185007/lambda-expression-c-sharp-failed-to-convert-to-the-object-type-and-fail-with-ref#comment19663433_14185007 in which, again, there is no point of using a lambda _at all_. – Chris Sinclair Jan 06 '13 at 22:02
  • @ChrisSinclair there are articles of G.V. Rossuma the functional "roots of Python". As it follows from the Python lambda emerged as an essential tool for utilitarian functions map / filter / reduce (taken from Lisp), the need for which gradually matured in the minds of people using Python. So it could be used as for C# and I don't think that MS-developers don't think about it and python isn't purely functional language but imperative too. I think some roots MS-developers in linq and lambdas have taken from Lisp/Haskell Python ideas. So it DOES make sence at all despite on your words. – Secret Jan 06 '13 at 22:10
  • @ChrisSinclair On a subject: if you need it so that's a little function-utility in multiple expressions by simple logic of the place as in the example of sorting, this is what lambdas and create. So I'm calculating the percentage of compression and for such small calculating there must be lambda-expression and it's not bad to make lambda work with references, generics ( as it could be done in C++ 11 ) – Secret Jan 06 '13 at 22:13

1 Answers1

1

I'm posting a simple unsafe method, but you can say goodbye to generics. It's bad practice to use the code, but I guess that it answers the question, somewhat.

static unsafe int MyFunction(ref int value)
{
    fixed (int* temp = &value)
    {
        var pointer = temp;
        return new Func<int>(() => *pointer += 10)();
    }
}

Use it through:

var value = 42;
var returnedValue = MyFunction(ref value);
var success = value == returnedValue;
//success will be true

I repeat it again, I wouldn't suggest you to use it in any case, if you need a language designed for these things, there are many.

e_ne
  • 8,340
  • 32
  • 43
  • Why do you use *var pointer*? You can just do: return new Func(() => *temp += 10)(); and may work too! – Secret Jan 06 '13 at 20:41
  • @OlegOrlov You can't use fixed local variables in lambdas. – e_ne Jan 06 '13 at 20:42
  • really? try in VS , I've running well and compiling with * temp and not using var pointer! static unsafe int MyFunction(ref int value) { fixed (int* temp = &value) { return new Func(() => *temp += 10)(); } } – Secret Jan 06 '13 at 20:45
  • VSC# 2008 Express .NET 3.5 - does work ) I can send proofs ( with screens and video-screening ) – Secret Jan 06 '13 at 20:48
  • there are proves: http://postimage.org/gallery/6xo7e7h4/1dda3571/ ( it's VSC# 2008 Express and no errors ) click one on of two screens – Secret Jan 06 '13 at 20:54
  • earlier I've given you, that there are now errors at compile and now showing screen from the debugging: http://s2.postimage.org/ze2jvqtqw/Screenshot_146.jpg as see it gives me the right result and does work! – Secret Jan 06 '13 at 20:56