我無法理解為什么 Scala 會讓我愉快地撰寫一些欄位驗證:
field -> text().verifying("declaration.additionalDocument.documentTypeCode.unacceptableCode", f => isEmpty(f) or !documentCodesNotAcceptable.contains(f))
但不是當我這樣做時:
text().verifying("declaration.additionalDocument.documentTypeCode.unacceptableCode", isEmpty or !documentCodesNotAcceptable.contains(_))
在第二種情況下,我收到一個編譯錯誤告訴我 type mismatch; found : String => Boolean required: Boolean
但為什么?第二種方式與第一種方式有何不同?
uj5u.com熱心網友回復:
的第二個引數verifying似乎是一個謂詞。也就是說,一個函式接受一個值并回傳trueor false,這是驗證框架的一個共同特征。
在第一個例子中,謂詞是
f => isEmpty(f) or !documentCodesNotAcceptable.contains(f)
決議為
f => (isEmpty(f) or !documentCodesNotAcceptable.contains(f))
這是一個完全合理的謂詞,首先進行測驗f, isEmpty如果失敗,則進行第二次測驗。
在第二個例子中,謂詞是這樣的:
isEmpty or !documentCodesNotAcceptable.contains(_)
其中(如評論中所述)擴展到
(x => isEmpty(x)) or (x => !documentCodesNotAcceptable.contains(x))
所以這個運算式試圖將or兩個函式放在一起,這是不支持的。
解決方案是使用第一個版本:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381444.html
上一篇:如何在Scala中將ListBuffer的專案與Array的專案相匹配?
下一篇:列印資料框中不同的列名
