Help re Delegates and Lambdas

  • Thread starter Thread starter Simon Woods
  • Start date Start date
S

Simon Woods

Hi

I'm trying to generate a simple evaluator-type app.

I want to generate a list of operators and say how each is to be evaluated.

So, e.g., I want to add to a list of operators

Operators.Add "+", evaluation function here

Now I think I should be able to use lambda expressions here, but I don't
think I've quite got my head around how they relate to delegates. (I
think I want to use a delegate because in due course I want to
generalise it further to have other evaluation mechanism e.g.
DateDiff/SQL etc)

So, trying to get a more simple case working, I have a delegate

Public Function MathFunction(of T) as Func(Of T,T)

(I assume that the Func(of T,T) is right as I expect 2 arguments)

In the Operator list, I think I want to have a method

Public Sub Add(Byval operator as String, Byval evaluator as MathFunction)

My problem comes when I try and add the lambda. I've got something like

Operators.Add "+", Function(x,y) x+y

But I'm getting an error saying "nested function does not have the same
signature as delegate".

I'm getting a bit lost and wonder if anyone could offer some help.

Thx

Simon
 
Simon said:
Hi

I'm trying to generate a simple evaluator-type app.

I want to generate a list of operators and say how each is to be evaluated.

So, e.g., I want to add to a list of operators

Operators.Add "+", evaluation function here

Now I think I should be able to use lambda expressions here, but I don't
think I've quite got my head around how they relate to delegates. (I
think I want to use a delegate because in due course I want to
generalise it further to have other evaluation mechanism e.g.
DateDiff/SQL etc)

So, trying to get a more simple case working, I have a delegate

Public Function MathFunction(of T) as Func(Of T,T)

(I assume that the Func(of T,T) is right as I expect 2 arguments)

In the Operator list, I think I want to have a method

Public Sub Add(Byval operator as String, Byval evaluator as MathFunction)

My problem comes when I try and add the lambda. I've got something like

Operators.Add "+", Function(x,y) x+y

But I'm getting an error saying "nested function does not have the same
signature as delegate".

I'm getting a bit lost and wonder if anyone could offer some help.

Thx

Simon

OK ... I'm sorted ... my delegate declaration was wrong. It should have
been something like

Public Delegate Function MathFunction(of T)(byval arg1 as T, byval arg2
as T) as T

or perhaps either, hence my later post wrt Generics and Numbers

Public Delegate Function MathFunction(byval arg1 as Integer, byval arg2
as Integer) as Integer

Public Delegate Function MathFunction(byval arg1 as Double, byval arg2
as Double) as Double
 
Back
Top