I am writing a Xamarin.Forms app using XAML for my views, and I am trying to write an IValueConverter
whose job should be returning false
if the input is "empty" for types where those semantics make sense (strings/lists/sequences/arrays/IEnumerables). I have started with the following, which returns false for empty strings, but I can't figure out how to extend this to lists, sequences, arrays, and IEnumerables:
type FalseIfEmptyConverter() =
interface IValueConverter with
member __.Convert(value:obj, _, _, _) =
match value with
| :? string as s -> (s <> "" && not (isNull s)) |> box
// TODO: extend to enumerables
| x -> invalidOp <| "unsupported type " + x.GetType().FullName
member __.ConvertBack(_, _, _, _) =
raise <| System.NotImplementedException()
Things I've tried that don't work:
:? list<_>
does not match a (boxed) list (at least not of ints) and produces a warningThis construct causes code to be less generic than indicated by its type annotations. The type variable implied by the use of a '#', '_' or other type annotation at or near [...] has been constrained to be type 'obj'
:? list<obj>
does not produce the warning, but also doesn't match a boxed list of ints- It's the same with
:? seq<_>
and:? seq<obj>
- It's the same with
:? System.Collections.Generic.IEnumerable<obj>
andIEnumerable<_>
(and if I place it below a similarseq
match as given above, it warns that the rule will never be matched, which makes sense since AFAIKseq
corresponds toIEnumerable
)
match value with | :? System.Collections.IEnumerable as s -> s.GetEnumerator().MoveNext() |> not | x -> invalidOp <| "unsupported type " + x.GetType().FullName
– Symmetry