我也想知道,为什么没有}这种或TYPEOF在榆树。然后仔细阅读文档以深入了解FRP,它变得清晰起来!您应该始终知道数据的形状。为了在Elms Tagged Unions中使用case ...的模式,您应该可以通过过滤已知类型来访问所需的值。警告,所有分支的情况下必须是相同的类型,但是这并不排除你使用一个具有所有已知类型的超集和虚拟警戒值的元组,以允许你访问你想要的类型。
下面见代码来加以说明:
假设:
使用
时
1.您所希望的类型的标签
2,采用定点值
import Html exposing (text)
type Input = Nothing | ILetter String | INumber Int | IFloat Float
inputs: List (Input)
inputs = [ Nothing, IFloat 8.34, ILetter "A", INumber 5, INumber -1, IFloat -12.0, ILetter "123!"]
--
doSomething: Input -> (String, Int, Float)
doSomething myInput =
case myInput of
Nothing ->
("not here!", -69, 69.0)
ILetter string ->
if string /= "DarnString" then
(string, 0, 0)
else
("DarnString", -1, -1.0)
INumber int ->
if int > 0 then
("GoodInt" , int, toFloat int)
else
("DarnInt" , int, toFloat int)
IFloat float ->
if float < 0 then
("Goodfloat", truncate float, float)
else
("Darnfloat", truncate float, float)
-- I am only interested in using strings
myStringFilter (mString, mInt, mFloat) =
if mString == "DarnString" || mString == "Darnfloat" || mFloat < 0 || mString == "DarnInt" || mInt > 0 then
"We are not the String(s) you were looking for!"
else
mString
myFloatFilter (mString, mInt, mFloat) =
if mString == "DarnString" || mString == "Darnfloat" || mString == "DarnInt" then
696969696.696969696969
else
mFloat
myIntFilter (mString, mInt, mFloat) =
if mString == "DarnString" || mString == "Darnfloat" || mString == "DarnInt" then
-696969696
else
mInt
main =
text (toString <| List.map myStringFilter (List.map doSomething inputs))
--text <| myStringFilter <| doSomething (IFloat 14.83)
-- text <| toString <| myFloatFilter <| doSomething (IFloat -14.83)
--text <| toString <| myIntFilter <| doSomething (INumber 5)
你能解释一下为什么要检查一个变量的类型吗?也许显示你将如何使用这样的功能? – ianmjones
当然 - 更新的问题。 – category