Please explain use of Option's orNull method

后端 未结 5 757
北荒
北荒 2021-02-01 21:29

Scala\'s Option class has an orNull method, whose signature is shown below.

orNull [A1 >: A](implicit ev : <:<[Null, A1]) : A1
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 21:36

    Re : 'how' is this used - one place we are finding this useful is when dealing with java api mappings where null is commonplace, e.g. on jdbc prepared statements to nullable sql columns. The Optional internal model fields can be mapped:

    stmt.setDate("field", myModel.myDateField.orNull)
    

    Instead of the more verbose:

    stmt.setDate("field", myModel.myDateField.getOrElse(null))
    

提交回复
热议问题