2017-08-13 82 views
1

我的編譯器抱怨兩個值:一些奇怪的故障(在我的模型記錄?)

在此摘錄model.firstChord.fifthmodel.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 

哪裏能是問題嗎?

謝謝。

回答

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

+0

你確定它是'let'繼'case'? – Timo

+0

'它看起來像關鍵字案例被用作一個變量。 - 將它重命名爲其他東西......這是消息。 – Timo

+0

是的,這是一個錯誤。我修好了它。 –

0

你還沒有提供你的Model,但是在你的init函數中你已經聲明和絃是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 
    )