我的编译器抱怨两个值:
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
的类型是:也许Chord
其中不包含名为
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
每个球场都有这种类型:
{{1}}
哪里可能是问题?
谢谢。
答案 0 :(得分:2)
编译错误确切地说明问题是什么
Maybe Chord
可以是Just Chord
或Nothing
。这两个都不包含名为fifth
的字段。
为了完成这项工作,您需要确保model.firstChord
和model.secondChord
为Just Chord
:
-- 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`
通过使用模式匹配(Just firstChord, Just secondChord)
,firstChord
和secondChord
表达式似乎是Chord
类型,其中有一个名为fifth
的字段
答案 1 :(得分:0)
你没有提供你的Model
,但在你的初始函数中你已经将和弦声明为一个可能。如果编译器对此感到满意,那么这意味着您的模型还包含Maybe
s。作为第一个解决方案,请删除Just
,同时查看模型
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
)