一些奇怪的错误(在我的模型记录?)

时间:2017-08-13 12:53:00

标签: elm

我的编译器抱怨两个值:

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}}

哪里可能是问题?

谢谢。

2 个答案:

答案 0 :(得分:2)

编译错误确切地说明问题是什么

Maybe Chord可以是Just ChordNothing。这两个都不包含名为fifth的字段。

为了完成这项工作,您需要确保model.firstChordmodel.secondChordJust 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)firstChordsecondChord表达式似乎是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
    )