2

I am wondering how to create an instance of type alias. More specifically this:

type Graph = scala.collection.mutable.Map[Statement, Statement]

I tried this but I get a type error saying Map[Statement, Statement] is not equivalent to Graph:

val graph: Graph = Map[Statement, Statement]().asInstanceOf[Graph]

Is this (i.e. creating a type alias) anti-pattern in Scala?

3
  • 2
    I can't reproduce with simple types in Scala. Is it possible you're accidentally using scala.collection.immutable.Map in the second snippet? Commented Feb 18, 2021 at 3:52
  • @SilvioMayolo oops, yes. I am sorry :) Commented Feb 18, 2021 at 3:54
  • 1
    Honestly, it's a common mistake (mutable vs immutable collections of the same name). I'm glad there's a question on here now so others can clear it up. Commented Feb 18, 2021 at 21:19

1 Answer 1

6

Map[?,?] is always immutable except when an import might shadow it.

This works:

val graph: Graph = collection.mutable.Map[Statement, Statement]()
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.