0

I have the following code:

val actions = Map(
"index" ->  Map(
  "description" -> "Makes CAEServer index project with provided project_id "
  , "usage" -> "index project_id"
  , "action" -> (
    (args: Array[String]) => {
      if (checkSecondArgument(args, "Project ID wasn't specified. Please supply project ID.")) {
        new CAEServer(args{0}).index(args{2})
      }
    }
)))

actions{providedAction}{"action"}(args)

and when I'm trying to compile it, compiler says

error: MainConsole.this.actions.apply(providedAction).apply("action") of type java.lang.Object does not take parameters
[INFO]       actions{providedAction}{"action"}(args)
[INFO]       ^
[ERROR] one error found

What's wrong?

1
  • That is probably because actions has type Map[String, Map[String, Any]] where Any doesn't take parameters. Commented May 6, 2013 at 17:03

1 Answer 1

3

Remember: Scala is statically typed.

When you create the (outer) Map, Scala infers the type of actions based on what you put in there. The most restrictive but still matching type (so called least-upper-bound) is:

Map[String,Map[String,Object]]

So a Map that maps Strings to Maps that map Strings to Objects. So when you retrieve any element, it will be of type Object, not Function so you cannot call it.

You should use case classes:

case class ActionElement(
    description: String,
    usage: String,
    action: Array[String] => CAEServer)

val actions = Map("index" ->  ActionElement(
   "Makes CAEServer index project with provided project_id ",
   "index project_id",
   args => { if (checkSecondArgument(args, "Project ID wasn't ...")) {
     new CAEServer(args{0}).index(args{2})
   }
))

Now you can call:

actions(providedAction).action(args)
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.