Ich habe Protokoll und seine Umsetzung in Swift geschrieben:Standardparameter als generischer Typ
protocol P {
}
struct A: P {
}
Protokoll ist als generischer Typ für eine Funktion verwendet:
func foo<T: P>(param: T) {
}
func foo() {
foo(param: A())
}
Bisher funktioniert alles einwandfrei. Aber ich möchte A() als Standardparameter der gegebenen Funktion setzen:
func foo<T: P>(param: T = A()) {
}
Leider mit folgenden Fehler:
Default argument value of type 'A' cannot be converted to type 'T'.
Oder
func foo<T: P>(param: T = A() as P) {
}
,
let a: P = A()
func foo<T: P>(param: T = a) {
}
Rückgabe:
Default argument value of type 'P' cannot be converted to type 'T'
Oder
func foo<T: P>(param: T = A() as T) {
}
Returns:
'A' is not convertible to 'T'; did you mean to use 'as!' to force downcast?
Was ich falsch mache? Wo ist das Problem?
Ich will keine Gewalt Guss wie diese verwenden:
func foo<T: P>(param: T = A() as! T) {
}
Vielen Dank im Voraus.