Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala function pointer

Tags:

scala

Google didn't help me with this question, I hope that doesn't mean it is not possible:

In my class I want to have a method that has a signature defined, but the body is not defined (method1)

There will be many defined methods that satisfy this signature (impl1, impl2, impl3)

When I initialise the object, I will then choose (based on some criteria) which method implementation impl1, impl2, impl3 to assign to the function pointer method1

Basically I'm asking how I can have a function pointer that can point to any function satisfying its signature.

EDIT:

So, it turns out it is actually very straight forward:

var method: Int => Int = (x => x+1)

method = (x => x-1)
method = (x => x*2)
etc...

My problem before was that I was using "val" or "def" to define "method"

Not sure why this wasn't suggested directly. Many people favoured the way of having the function as a parameter to some secondary class then initialise that class with a specific implementation. Maybe there is something that I'm missing.

EDIT 2: I realise now that I didn't get the answer I was looking for because I didn't word my question properly, I should have said that I wanted "delegate" behaviour as it is in C#.

like image 260
SpaceMonkey Avatar asked Apr 29 '14 12:04

SpaceMonkey


People also ask

Can a function be a pointer?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.

Can a pointer call a function?

Like variables, instructions of a function are also stored in memory and have an address. A pointer pointing to the address of a function is called function pointer. A function pointer in C can be used to create function calls to the function they point to just like a normal function.

How do you define a pointer to a function?

int foo(int); Here foo is a function that returns int and takes one argument of int type. So as a logical guy will think, by putting a * operator between int and foo(int) should create a pointer to a function i.e. int * foo(int);

What is the meaning of => in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .


1 Answers

In Scala, functions are objects, so you can do:

class Foo(val func : Int => Int){
}
object Main{
    def main(args: Array[String]) {
        val foo1=new Foo(x => x + 1)
        val foo2=new Foo(x => x + 2)
        val foo3=new Foo(x => x + 3)
        println(foo1.func(10)) // Prints 11
        println(foo2.func(10)) // Prints 12
        println(foo3.func(10)) // Prints 13
    }
}
like image 113
Idan Arye Avatar answered Oct 18 '22 08:10

Idan Arye