Lambda, anonymous functions, assign function to variable, etc.

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
kwhitefoot
Posts: 2
Joined: Thursday 03rd February 2022 7:50pm

Lambda, anonymous functions, assign function to variable, etc.

Post by kwhitefoot » Saturday 12th February 2022 5:58pm

Is it possible to assign a function to a variable or pass a function to another function?
Or is creating a separate class for each function the only way to do it like old Java?

BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Lambda, anonymous functions, assign function to variable, etc.

Post by BruceSteers » Saturday 12th February 2022 7:08pm

kwhitefoot wrote:
Saturday 12th February 2022 5:58pm
Is it possible to assign a function to a variable or pass a function to another function?
Or is creating a separate class for each function the only way to do it like old Java?
Possibly find what you are looking for with Object.Call()
http://gambaswiki.org/wiki/comp/gb/object/call

Then you can pass/assign the function (method) as a string and call it that way.

Object.Call(MyClass, "MyFunctionName", [My, Arguments])

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 06th February 2022 8:41am
Location: Russia

Re: Lambda, anonymous functions, assign function to variable, etc.

Post by AMUR-WOLF » Saturday 12th February 2022 11:25pm

kwhitefoot wrote:
Saturday 12th February 2022 5:58pm
Is it possible to assign a function to a variable or pass a function to another function?
Or is creating a separate class for each function the only way to do it like old Java?
Yes. If you want more SOLID in a Gambas application, you should create a separate class for each function. The situation is worse: there is no generics in Gambas, so we should duplicate hierarchies of classes for each type of input and output.

Assigning a function to a variable or passing a function to another function is an illusion in Java. Really, we assign and pass an object of anonymous class that implements a functional interface. When I code on Java, I often prefer anonymous class because I immediately see the name of the interface and signature of overridden method, so readability is better.

BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Lambda, anonymous functions, assign function to variable, etc.

Post by BruceSteers » Sunday 13th February 2022 11:33am

kwhitefoot wrote:
Saturday 12th February 2022 5:58pm
Is it possible to assign a function to a variable or pass a function to another function?
Or is creating a separate class for each function the only way to do it like old Java?
You could also use a Property in any class then reading the property is actually a function.

Property MyPropertyName As Variant


Private Function MyPropertyName_Read() As Variant

  ' this is a function not a simple variable

  Dim vResult As Variant = PerformSomeFunction()
  Return vResult

End

Private Sub MyPropertyName_Write(Value As Variant)

  Dim vVar As Variant = Value
  DoSomethingWith(vVar)

End

then if you did this..
Print Me.MyPropertyName

The MyPropertyName_Read() Function is run and the results returned

Or this...
Me.MyPropertyName = "SomeThing"

Then the MyPropertyName_Write() Sub is run with "SomeThing" as the Value

BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Lambda, anonymous functions, assign function to variable, etc.

Post by BruceSteers » Sunday 13th February 2022 12:10pm

kwhitefoot wrote:
Saturday 12th February 2022 5:58pm
Is it possible to assign a function to a variable or pass a function to another function?
Or is creating a separate class for each function the only way to do it like old Java?
Also there is using the 3 dots and Param.class if it's a variadic expression you want.
See http://gambaswiki.org/wiki/lang/methoddecl

I don't know truly what you mean by a Lambda function but there are ways to accomplish many things in gambas.

Post Reply