2011-11-25 78 views
3

我有以下幾種類型:表格嵌套結構耶索德

data Cheese = Cheddar Int | Edam String String | Cottage String Int 

data Meal = Meal { 
     nameOfMeal :: String, 
     ... other generic fields 
     cheese :: Cheese 
} 

目前我的形式是這樣的:

cheddarForm = renderTable $ construct 
      <$> areq textField "Name of meal" Nothing 
      <*> areq intField "Cheddar fat" Nothing 
     where 
      construct name fat = Meal name (Cheddar fat) 

我目前的事實很高興,我需要一種形式每種類型的「奶酪」(儘管我不會介意有一個動態的形式..)。但是,我真的很想擺脫每一種形式重複'餐的名稱'。我能以某種方式結合形式,還是必須最終選擇Monadic形式?

回答

3

你不能只是繞過

conWithNOM ctr = ctr 
    <$> areq textField "Name of meal" Nothing 

,並呼籲,對你的其他表單字段?

cheddarForm = renderTable $ conWithNOM construct 
    <*> areq intField "Cheddar fat" Nothing 
    where 
     construct name fat = Meal name (Cheddar fat)