我有以下几种类型:

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表格?

最佳答案

你不能只是过去

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)

关于haskell - Yesod中嵌套结构的表单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8272397/

10-12 02:02