Elm 一些奇怪的错误(在我的模型记录中?)
我的编译器抱怨两个值:Elm 一些奇怪的错误(在我的模型记录中?),elm,Elm,我的编译器抱怨两个值: model.firstChord.fifth和model.secondChord.fifth 在本摘录中: -- render frets renderFret : Model -> Fret -> Html Msg renderFret model fret = let ( pitchName, pitchLevel ) = fret.pitch ( firstChordRootPitchName, firstChor
model.firstChord.fifth
和model.secondChord.fifth
在本摘录中:
-- render frets
renderFret : Model -> Fret -> Html Msg
renderFret model fret =
let
( pitchName, pitchLevel ) = fret.pitch
( firstChordRootPitchName, firstChordRootPitchLevel ) = model.firstChord.root
( firstChordThirdPitchName, firstChordThirdPitchLevel ) = model.firstChord.third
( firstChordFifthPitchName, firstChordFifthPitchLevel ) = model.firstChord.fifth
( secondChordRootPitchName, secondChordRootPitchLevel ) = model.secondChord.root
( secondChordThirdPitchName, secondChordThirdPitchLevel ) = model.secondChord.third
( secondChordFifthPitchName, secondChordFifthPitchLevel ) = model.secondChord.fifth
in
...
它告诉我:
model.firstChord
没有名为fifth
的字段-model.firstChord
的类型为:
也许是和弦
它不包含名为fifth
的字段
但是我的模型有一个字段fifth
:
-- initial model
init : ( Model, Cmd Msg )
init =
(
{ firstChord =
Just
{ root = ( "C", 3 )
, third = ( "E", 3 )
, fifth = ( "G", 3 )
}
, secondChord =
Just
{ root = ( "F", 3 )
, third = ( "A", 3 )
, fifth = ( "C", 4 )
}
}
,
Cmd.none
)
弦的类型为:
-- chords
type alias Chord =
{ root : Pitch
, third : Pitch
, fifth : Pitch
}
每个音高都有以下类型:
-- pitch
type alias Pitch = ( PitchName, PitchLevel )
-- pitch name
type alias PitchName = String
-- pitch level
type alias PitchLevel = Int
问题出在哪里
谢谢。您没有提供您的
型号
,但在init函数中,您已将和弦声明为“可能”。如果编译器对此感到满意,那么这意味着您的模型还包括可能s。作为第一个解决方案,删除,只删除s,同时查看您的模型
init =
(
{ firstChord =
Just
{ root = ( "C", 3 )
, third = ( "E", 3 )
, fifth = ( "G", 3 )
}
, secondChord =
Just <-------- Here you say that secondChord is Maybe Chord
{ root = ( "F", 3 )
, third = ( "A", 3 )
, fifth = ( "C", 4 )
}
}
,
Cmd.none
)
init=
(
{第一和弦=
只是
{root=(“C”,3)
,第三个=(“E”,3)
,fifth=(“G”,3)
}
,第二和弦=
编译错误正好说明了问题所在
可能Chord
是Just Chord
或Nothing
。这两个字段都不包含名为fifth
的字段
为了实现这一点,您需要确保model.firstChord
和model.secondChord
都是和弦
:
-- render frets
renderFret : Model -> Fret -> Html Msg
renderFret model fret =
case (model.firstChord, model.secondChord) of
(Just firstChord, Just secondChord) ->
let
( pitchName, pitchLevel ) = fret.pitch
( firstChordRootPitchName, firstChordRootPitchLevel ) = firstChord.root
( firstChordThirdPitchName, firstChordThirdPitchLevel ) = firstChord.third
( firstChordFifthPitchName, firstChordFifthPitchLevel ) = firstChord.fifth
( secondChordRootPitchName, secondChordRootPitchLevel ) = secondChord.root
( secondChordThirdPitchName, secondChordThirdPitchLevel ) = secondChord.third
( secondChordFifthPitchName, secondChordFifthPitchLevel ) = model.secondChord.fifth
in
...
_ ->
-- here is something when either `model.firstChord` or `model.secondChord` is `Nothing`
通过使用模式匹配(仅第一个和弦,仅第二个和弦)
,第一和弦
和第二和弦
表达式似乎属于和弦
类型,其中有一个名为第五和弦
的字段,您确定它是让跟随大小写
吗?看起来关键字大小写被用作变量。-将其重命名为其他内容。
…这是乱七八糟的也许你能帮我解决你回答的问题,我会给你发一个链接。但是首先:谢谢。当你根据我的例子修改代码时,你还有编译错误吗?