Scala map containing mix type values

后端 未结 4 1368
感情败类
感情败类 2020-12-19 13:21

I have a function that returns ( groovy code)

[words: \"one two\", row: 23, col: 45]

In scala I change above to scala Map but then I am for

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 13:46

    Having only two possible value types as in your example would allow to use the Either type with the sub-types Left and Right:

    val m = Map("words" -> Left("one two"), "rows"-> Right(23), "cols"-> Right(45))
    

    If you get back a value from the map, you can check what you have, e.g. with pattern matching or using isLeft and isRight, and "unwrap" it accordingly.

提交回复
热议问题