我也想知道,爲什麼沒有}這種或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