2016-06-29 10 views
0
{-# LANGUAGE TemplateHaskell, DeriveGeneric, DeriveAnyClass #-} 
module Main where 
import Flow 
import Control.Lens hiding ((|>)) 
import Data.Default 
import GHC.Generics 

main :: IO() 
main = putStrLn "hello world" 

append x = (++ [x]) 
equals = (==) 

data SectionedItems s i = SectionedItems{ 
    _section :: Maybe s, 
    _items :: [i], 
    _subsections :: [SectionedItems s i] 
} deriving (Show, Generic) 

instance Default (SectionedItems s i) where 
    def = SectionedItems { _section = Nothing, _items = [], _subsections = [] } 

makeLenses ''SectionedItems 

sectionedItems = SectionedItems{ 
    _section = Nothing, 
    _items = [], 
    _subsections = [] 
} 

data SectionedItemsElement s i = Section s | Item i 


addElementToSectionedItems :: SectionedItems s i -> SectionedItemsElement s i -> SectionedItems s i 
addElementToSectionedItems si (Section x) = 
    (def & section .~ Just x :: SectionedItems s i) -- Error is probably somewhere here 
    |> \subsec -> si & subsections %~ append subsec 

Was ersetze ich und mit, damit es funktioniert? Ich versuchte s und ich, aber ich erhalte einen Fehler Could not match actual type s1 with expected type s auf Just x. Was kann ich verwenden, um vom Funktionskörper aus die Typen s und I zu referenzieren?Verwendung Generics von Funktionstyp Deklaration im Funktionskörper

Antwort

2

Einfache Korrekturen:

  • eine Art Signatur equals hinzufügen - sonst Monomorphie beißt Sie
  • Entfernen Sie die unnötige Typenannotation auf def & section .~ Just x - die Art sowieso geschlossen wird.

So:

... 

equals :: Eq a => a -> a -> Bool 
equals = (==) 
... 
addElementToSectionedItems :: SectionedItems s i -> SectionedItemsElement s i -> SectionedItems s i 
addElementToSectionedItems si (Section x) = 
    (def & section .~ Just x) 
    |> \subsec -> si & subsections %~ append subsec 
+0

Wie würden Sie manuell die Art angeben? – 2426021684

+1

Ich denke, [ScopedTypeVariables] (https://wiki.haskell.org/Scoped_type_variables) könnte helfen – Alec

+0

ScopeTypedVariables funktioniert – 2426021684

Verwandte Themen