是否可以撰寫我可以這樣使用的后綴函式:
//...
val myStr: String = "some string"
val myNewStr = myStr myFunction otherMyFunction // Instead of: otherMyFunction(myFunction(myStr))
def myFunction(String: str): String {
//do something
}
def otherMyFunction(String: str): String {
//do something
}
//...
我想呼叫我的函式,就像它是 String 型別的方法一樣。它有這種可能嗎?如果有,我必須使用什么語法?
uj5u.com熱心網友回復:
聽起來您要問的是是否可以將方法添加到 String 型別。是的,您可以使用隱式類來做到這一點。例如,參見https://www.baeldung.com/scala/implicit-classes。
uj5u.com熱心網友回復:
在 Scala 2 中并且沒有 Scalaz 或其他非核心功能庫:
(myFunction compose otherMyFunction) (myStr)
Scala 2.13 你可以使用管道鏈接運算子
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/477943.html
標籤:斯卡拉
下一篇:抽象案例類的正確使用
