【发布时间】:2020-04-21 19:39:44
【问题描述】:
我在 Elm 中有一个自定义类型来处理错误分支。基本上,我有一个输入,它给出了Strings,我需要将它转换为Ints。
type Seconds
= Error Int
| Valid Int
type alias Model =
{ timeBetweenExercises : Seconds
, roundsSequenceOne : Seconds
, roundsSequenceTwo : Seconds
, roundsSequenceThree : Seconds
, repititionsPerExercise : Seconds
, secondsPerExercise : Seconds
, totalTrainingDuration : Seconds
}
init : Model
init =
{ timeBetweenExercises = Valid 3
, roundsSequenceOne = Valid 3
, roundsSequenceTwo = Valid 3
, roundsSequenceThree = Valid 3
, repetitionsPerExercise = Valid 6
, secondsPerExercise = Valid 6
, totalTrainingDuration = Valid 6
}
我从Evan's "Life of a file" talk 得到了自定义类型的想法。我想在出现错误时记住数字(例如,用户输入了字符串而不是数字)。这是我的更新功能的尝试:
update : Msg -> Model -> Model
update msg model =
case msg of
TimeBetweenExercisesChanged newTime ->
case String.toInt newTime of
Nothing ->
{ model | timeBetweenExercises = Error model.timeBetweenExercises }
Just time ->
{ model | timeBetweenExercises = Valid time }
我的问题是,编译器对我大喊大叫是因为 model.timeBetweenExercises 的类型是 Seconds。有没有办法只能获取自定义类型的Int值?
【问题讨论】:
-
我会说类型错误很清楚,但我不能说如何解决它,因为我不明白你想要做什么。我猜您只是想在
Nothing案例中保持模型不变?我当然看不出你还能做些什么——但正如我所说,我真的不知道你的目标是什么。
标签: elm