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

Post

Posted
Rating:
#1 (In Topic #819)
Trainee
 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?
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

kwhitefoot said

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()
/comp/gb/object/call - Gambas Documentation

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

Object.Call(MyClass, "MyFunctionName", [My, Arguments])
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’

kwhitefoot said

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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

kwhitefoot said

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.

Code (gambas)

  1.  
  2. Property MyPropertyName As Variant
  3.  
  4.  
  5. Private Function MyPropertyName_Read() As Variant
  6.  
  7.   ' this is a function not a simple variable
  8.  
  9.   Dim vResult As Variant = PerformSomeFunction()
  10.   Return vResult
  11.  
  12.  
  13. Private Sub MyPropertyName_Write(Value As Variant)
  14.  
  15.   Dim vVar As Variant = Value
  16.   DoSomethingWith(vVar)
  17.  
  18.  
  19.  

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
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’

kwhitefoot said

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 /lang/methoddecl - Gambas Documentation

I don't know truly what you mean by a Lambda function but there are ways to accomplish many things in gambas.
Online now: No Back to the top
1 guest and 0 members have just viewed this.