【发布时间】:2017-11-14 08:05:59
【问题描述】:
我正在为我的视图编写一个使用 XAML 的 Xamarin.Forms 应用程序,并且我正在尝试编写一个 IValueConverter,如果对于这些语义有意义的类型输入为“空”,则其工作应该返回 false(字符串/列表/序列/数组/IEnumerables)。我从以下开始,它为空字符串返回 false,但我不知道如何将其扩展到列表、序列、数组和 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()
我尝试过但不起作用的方法:
-
:? list<_>不匹配(盒装)列表(至少不是整数)并产生警告This 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>不会产生警告,但也不匹配装箱的整数列表 - 与
:? seq<_>和:? seq<obj>相同 - 这与
:? System.Collections.Generic.IEnumerable<obj>和IEnumerable<_>相同(如果我将它放在与上面给出的类似seq匹配的下方,它会警告该规则永远不会匹配,这是有道理的,因为AFAIKseq对应到IEnumerable)
【问题讨论】:
-
match value with | :? System.Collections.IEnumerable as s -> s.GetEnumerator().MoveNext() |> not | x -> invalidOp <| "unsupported type " + x.GetType().FullName
标签: f# ivalueconverter