Is Option wrapping a value a good pattern?

后端 未结 3 1816
攒了一身酷
攒了一身酷 2021-02-05 22:52

I recently wrote the following bit of Scala:

val f: File = ... // pretend this file came from somewhere
val foo = toFoo(io.Source.fromFile(f).mkString)
         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 23:42

    I have no problems with the other answers given here, but did you consider changing the name of toFoo into something that 'flows' better? I mean, toFoo really smells like something that should be on the right of an expression, but if you rename it into something else, it might fit on the left as well.

    // toFoo, as defined by you
    val foo = toFoo(io.Source.fromFile(f).mkString)
    // Same function, different name
    val foo = createFooFrom(io.Source.fromFile(f).mkString)
    

提交回复
热议问题