Compile error when using type alias

Hello friends,
I’m currently trying to use a type alias to make my code more readable but I fail. My code is:

type VariableName = String
type Environment =  Map[VariableName, Int]

Then i have a function that gets environment : Environment as parameter and there I want to insert a value into the environment like:

environment("a" -> 5)

My compiler doesn’t like that and says:

“Type Mismatch:
Found: (String, Int)
Required: (Objectname.VariableName)
(which expands to) String”

I’m confused because I gave it a string. Anyone knows how I need to declare that VariableName so it gets accepted?

Thanks in advance!

Edit: My question just changed, as I tried to remove the type alias and exchange it with a simple string.
I think it has something to do with how I used it:
case _ : Int => environment("a" -> 5)

What works is

case _ : Int => environment("a")

So maybe it kinda gets that the matched int should be the right side in the map? I will test and close the question if so.


Your problem has nothing to do with type aliases. environment is
Map[String, Int] and then environment(“a” -> 5) is not a valid method call.
environment(“a”) should compile, but it doesn’t do what you want (it
returns whatever value is associated with “a” and throws
NoSuchElementException is there is none).

First of all, is Map here a mutable map? If not, you simply can’t add
something to it. If it is mutable, you could do environment(“a”) = 5.

 Best, Oliver
1 Like

Oh, that explains a lot!

Thanks for your detailed answer, Oliver.

Best, Felix