How to convert string to character in runtime in Scala?

I have a case class that looks like this:

case class Outcome(text: Symbol) 

Now I need to change the meaning of the text at runtime. I am trying to do something like this:

 val o2 = o1.copy(text.name = "foo" ++ text.name) 

This obviously gives me a compilation error:

 type mismatch; found : String required: Symbol 

How to convert a character to a string, add / add something and change it to a character again? Or, to be simpler, how can I change the name of a character?

+6
source share
1 answer

You can use the Symbol.apply method:

 Symbol("a" + "b") // Symbol = 'ab val o2 = o1.copy(text = Symbol("foo" + o1.text.name)) 

There is a useful tool for working with nested structures in scalaz - Lens

 import scalaz._, Scalaz._ case class Outcome(symbol: Symbol) val symbolName = Lens.lensu[Symbol, String]( (_, str) => Symbol(str), _.name) val outcomeSymbol = Lens.lensu[Outcome, Symbol]( (o, s) => o.copy(symbol = s), _.symbol) val outcomeSymbolName = outcomeSymbol >=> symbolName val o = Outcome('Bar) val o2 = outcomeSymbolName.mod("foo" + _, o) // o2: Outcome = Outcome('fooBar) 
+10
source

All Articles