1

i've a simpleNode class with two inputs that u can only fill one of them which are both Map in Scala but i have to check to the type of data in maps in order to fill any of the inputs

the code i've written to do so is:

class SimpleNode (
  val uriTriples: collection.mutable.Map[String, List[String]] = collection.mutable.Map.empty,
  val valueTriples: collection.mutable.Map[String, Map[String,String]] = collection.mutable.Map.empty
                ) 
  {              
    def this(map:collection.mutable.Map) = {
      map.values.head match {
      case uri : List[String] => this(uris,null) 
      case values : Map[String,String] => this(null,values)
      case _=>
    }
  }
}

I always face the error :

a:34: error: 'this' expected but identifier found.
[INFO]        map.values.head match {
[INFO]        ^                       
2

1 Answer 1

3

Usual strategy for disambiguation:

class SimpleNode (
  val uriTriples: collection.mutable.Map[String, List[String]] = collection.mutable.Map.empty,
  val valueTriples: collection.mutable.Map[String, Map[String,String]] = collection.mutable.Map.empty
)
  {
    def this(map:mutable.Map[String, List[String]]) = this(map, null)
    def this(map:mutable.Map[String, Map[String,String]])(implicit d: DummyImplicit) = this(null, map)
}

Or a factory is more pedestrian:

object SimpleNode {
  def apply(...) = ???
}
Sign up to request clarification or add additional context in comments.

3 Comments

isn't the dummyimplicit dupricated in scala ?
@HadyElsahar Not to my limited knowledge. scala-lang.org/api/current/…
@HadyElsahar I thought you meant deprecated.

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.