2017-08-13 1 views
1

Mon compilateur se plaint de deux valeurs:une faute étrange (dans mon modèle de dossier?)

model.firstChord.fifth et model.secondChord.fifth

dans cet extrait:

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

il me dit:

model.firstChord n'a pas de champ nommé fifth. - Le type de model.firstChord est:

Peut-être Chord

qui ne contient pas un champ nommé fifth.

mais mon modèle a un champ 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 
    ) 

Le type de l'accord est:

-- chords 
type alias Chord = 
    { root : Pitch 
    , third : Pitch 
    , fifth : Pitch 
    } 

Chaque pas a ce type:

-- pitch 
type alias Pitch = (PitchName, PitchLevel) 

-- pitch name 
type alias PitchName = String 

-- pitch level 
type alias PitchLevel = Int 

Où pourrait être le problème?

Merci.

Répondre

2

Compile erreur indique exactement ce que le problème est

Maybe Chord est soit Just Chord ou Nothing. Aucun de ces deux contient un champ nommé fifth.

Pour faire ce travail, vous devez vous assurer que model.firstChord et model.secondChord sont 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` 

En utilisant la correspondance de modèles (Just firstChord, Just secondChord), firstChord et secondChord expressions semblent être de type Chord, qui a un champ nommé fifth

+0

êtes-vous sûr que c'est 'let' suivant' case' ?? – Timo

+0

'On dirait que le mot-clé case est utilisé comme variable. - Renommez-le en quelque chose d'autre ... c'est le message. – Timo

+0

oui, c'est une erreur. Je l'ai corrigé. –

0

Vous n'avez pas fourni votre Model, mais dans votre fonction init, vous avez déclaré l'accord comme un Maybe. Si le compilateur était satisfait de cela, cela signifie que votre modèle inclut également les Maybe s. En tant que première solution, retirez les Just s, mais regardez également à votre modèle

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 
    )