How do I add an XML attribute, or not, depending on an Option?

后端 未结 3 1151
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 08:28

I have written a makeMsg function but I don\'t like it - it just seems really un-Scala-ish to discriminate based on Option.isDefined. Can you make it better?



        
3条回答
  •  青春惊慌失措
    2020-12-31 08:55

    Canonical Scala that does not require the text field to know to cleverly disappear when it's empty:

    t.map(s => ).getOrElse()
    

    You should think about using this pattern whenever you have an option but need to use something that doesn't know about options. (In this case, Easy Angel has found a more compact solution where it does know about options or something like them.)

提交回复
热议问题