With that key
default?
func preference<K>(key _: K.Type = K.self, value: K.Value) -> some View where K : PreferenceKey
I can see no way to write code that omits the key
parameter because I don't see how the compiler can infer K
from value: K.Value
. The key
param is only for type info and not used iside the func as its name is _
.
Same issue with these:
func anchorPreference<A, K>(key _: K.Type = K.self, value: Anchor<A>.Source, transform: @escaping (Anchor<A>) -> K.Value) -> some View where K : PreferenceKey
func onPreferenceChange<K>(_ key: K.Type = K.self, perform action: @escaping (K.Value) -> Void) -> some View where K : PreferenceKey, K.Value : Equatable
func overlayPreferenceValue<Key, T>(_ key: Key.Type = Key.self, @ViewBuilder _ transform: @escaping (Key.Value) -> T) -> some View where Key : PreferenceKey, T : View
func backgroundPreferenceValue<Key, T>(_ key: Key.Type = Key.self, @ViewBuilder _ transform: @escaping (Key.Value) -> T) -> some View where Key : PreferenceKey, T : View
if you omit key
, there is no way the compiler can infer K
from knowing only K.Value
See https://forums.swift.org/t/swiftui-anchorpreference-a-k-key-k-type-k-self-how-to-make-use-of-key-params-default-value/35081