Scala Option object inside another Option object

后端 未结 4 2000
情书的邮戳
情书的邮戳 2021-02-08 02:05

I have a model, which has some Option fields, which contain another Option fields. For example:

case class First(second: Option[Second], name: Option[String])
ca         


        
4条回答
  •  臣服心动
    2021-02-08 02:56

    I think it is an overkill for your problem but just as a general reference:

    This nested access problem is addressed by a concept called Lenses. They provide a nice mechanism to access nested data types by simple composition. As introduction you might want to check for instance this SO answer or this tutorial. The question whether it makes sense to use Lenses in your case is whether you also have to perform a lot of updates in you nested option structure (note: update not in the mutable sense, but returning a new modified but immutable instance). Without Lenses this leads to lengthy nested case class copy code. If you do not have to update at all, I would stick to om-nom-nom's suggestion.

提交回复
热议问题