试图检查水果的价值
fruit =
Dict.fromList
[ ( 1, { fruitIsGood = False } )
, ( 2, { fruitIsGood = False } )
, ( 3, { fruitIsGood = True } )
]
whichFruitIsGood : Dict.Dict number { fruitIsGood : Bool } -> String
whichFruitIsGood fruit =
case get 0 fruit of
Nothing ->
Debug.crash "nothing found"
Just fruit ->
if fruit.fruitIsGood == True then
"Apple"
else
"I hate Fruit"
我不知道如何获得fruitIsGood道具或任何你在Elm这里称之为..
首先,Debug.crash "nothing found"
不会向您提供任何有用的功能,而是返回nothing found
字符串。
然后你只需要修复编译器指出的错误。它们主要是关于变量,它们被定义多次。让我们将你的第一个函数重命名为fruits
:
fruits =
Dict.fromList
[ ( 1, { fruitIsGood = False } )
, ( 2, { fruitIsGood = False } )
, ( 3, { fruitIsGood = True } )
]
并且第二个函数中的变量:
whichFruitIsGood : Dict.Dict number { fruitIsGood : Bool } -> String
whichFruitIsGood fruit =
case get 3 fruit of
Nothing ->
"nothing found"
Just foundFruit ->
if foundFruit.fruitIsGood == True then
"Apple"
else
"I hate Fruit"
然后你的代码将编译并返回nothing found
作为结果。
这里有一点点修改过的ellie-app example,它显示了代码的实际效果。