一些奇怪的错误(在我的模型记录中?)
Some Strange Fault (In My Model Record ?)
我的编译器抱怨两个值:
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
does not have a field named fifth
. - The type of model.firstChord
is:
Maybe Chord
Which does not contain a field named 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
问题出在哪里?
谢谢。
您还没有提供 Model
,但在您的初始化函数中您已将和弦声明为 Maybe。如果编译器对此感到满意,则意味着您的模型还包含 Maybe
。作为第一个解决方案,删除 Just
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
)
编译错误准确说明了问题所在
Maybe Chord
是 Just Chord
或 Nothing
。这两个中的 None 包含一个名为 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
[= 的字段23=]
我的编译器抱怨两个值:
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
does not have a field namedfifth
. - The type ofmodel.firstChord
is:Maybe Chord
Which does not contain a field named
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
问题出在哪里?
谢谢。
您还没有提供 Model
,但在您的初始化函数中您已将和弦声明为 Maybe。如果编译器对此感到满意,则意味着您的模型还包含 Maybe
。作为第一个解决方案,删除 Just
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
)
编译错误准确说明了问题所在
Maybe Chord
是 Just Chord
或 Nothing
。这两个中的 None 包含一个名为 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
[= 的字段23=]