Mam następujący sposób:scala: zdefiniować parametry domyślne funkcji (val) vs użyciu metody (def)
scala> def method_with_default(x: String = "default") = {x + "!"}
method_with_default: (x: String)java.lang.String
scala> method_with_default()
res5: java.lang.String = default!
scala> method_with_default("value")
res6: java.lang.String = value!
staram się osiągnąć to samo z Val, ale dostaję błąd składni, tak:
(bez wartości domyślnej, ten kompiluje ok)
scala> val function_with_default = (x: String) => {x + "!"}
function_with_default: String => java.lang.String = <function1>
(ale nie mogłem dostać ten skompilować ...)
scala> val function_with_default = (x: String = "default") => {x + "!"}
<console>:1: error: ')' expected but '=' found.
val function_with_default = (x: String = "default") => {x + "!"}
^
jakiś pomysł?
Function1 z Function0 jest dość sprytny, ale powiedziałbym, że jest to trochę śmierdzący. Tylko moja opinia tutaj. Żadna dyskusja nie jest przeznaczona. :) – pedrofurla
BTW możesz użyć cukrów dla funkcji: 'new (String => String) z (() => String) {override def apply = apply (" default "); nadpisuj def zastosuj (x: String) = x + "!" } 'działa. – pedrofurla
Zgadzam się, że to trochę śmierdzące, ale nie widzę rozwiązania, które nie jest. –