6

I would need to map strings to functions. I tried the following:

def a(a: Int,..) = ... 

which represents a generic function with various arguments,

then

val m: Map[String, Funcs] = Map("a"-> a)

where

 type Funcs = (Any*) => Any

but it doesn't really work.. I would like to make maps of mixed functions with a string as key.

2
  • What do you mean by mixed functions? Do you mean functions with one argument of any type, or functions with any number of arguments? Commented Jul 31, 2014 at 13:25
  • functions of variable arguments of any type mapped to any return type Commented Jul 31, 2014 at 13:28

1 Answer 1

2

This is not easy. In fact, it's nearly impossible at runtime to do this. At compile time, there are several tricks you can use, all of them found in the library Shapeless. You don't have to use Shapeless as Miles showed in a gist wherein he explained:

trait Assoc[K] { type V ; val value: V }
def mkAssoc[V0](k: String, v: V0): Assoc[k.type] { type V = V0 } = new Assoc[k.type]{ type V = V0 }

and now at compile time you can match all the different things you need

implicit def fAssoc = mkAssoc("f", f)
implicit def gAssoc = mkAssoc("g", g)

and retrieve them as

def lookup(k: String)(implicit assoc: Assoc[k.type]): assoc.V = assoc.value

if you push these into a class like he's done with his HMap, you could do something along the lines of a Poly1:

abstract class FMapper{
  protected def mkAssoc[V0](k: String, v: V0): Assoc[k.type] { type V = V0 } = 
    new Assoc[k.type]{ type V = V0 }

  def apply(k: String)(implicit assoc: Assoc[k.type]): assoc.V = assoc.value

and create a class such as

class Mapped extends FMapper{
  implicit val f = mkAssoc("f", f)
  implicit val g = mkAssoc("g", g)

but as you can see, it starts to get ugly fast...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.