0
我有要麼應該返回Map[String, Seq[String]]
與按鍵錯誤的序列(萬一有是錯誤),或返回成功的功能(可能,Unit
) 。我想知道,最好的回報類型是什麼?選擇一個正確的返回類型,函數返回地圖錯誤的
def foo1(a: Int): Map[String, Seq[String]]
def foo2(a: Int): Option[Map[String, Seq[String]]]
def foo3(a: Int): Either[Map[String, Seq[String]], ???]
//def foo4(a: Int): something else?
如果第一種情況,那就要返回Map["", Seq()]
如果沒有錯誤,在我看來,它看起來並不優雅。在第二種情況下,它會返回None
,沒關係,但似乎是多餘的。對於第三種情況也是如此,但還有一個問題 - 我將如何返回Right
?
您認爲如何?
它看起來像我主要是基於意見的問題。所以這裏是我的觀點:它應該被命名爲'getErrors'(或類似的)來強調結果是結果類型爲'Option [...]'的錯誤集合,或者你應該使用'scalaz.Validation'作爲結果類型:'Validation [Map [String,Seq [String]],Unit]'或'ValidationNEL [(String,Seq [String]),Unit]'。 – senia
@senia在客戶端檢查map.isEmpty會更好嗎? –
@senia你認爲return Option [Map [String,Seq [String]]]是否合理?爲什麼不只是映射[String,Seq [String]]? –