【问题标题】:Understanding code generated by Yesod Persistent TH了解 Yesod Persistent TH 生成的代码
【发布时间】:2013-07-31 03:25:07
【问题描述】:

我花了一段时间试图理解这个示例中模板 haskell 生成的代码,取自 Yesod 书:

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int
    deriving Show
Car
    color String
    make String
    model String
    deriving Show
|]

我觉得我主要看到发生了什么(很多类型编组),但有一个部分仍然让我感到困惑:

instance PersistEntity (PersonGeneric backend) where
  data instance Unique (PersonGeneric backend) =
  data instance EntityField (PersonGeneric backend) typ
      = typ ~ KeyBackend backend (PersonGeneric backend) => PersonId |
        typ ~ String => PersonName |
        typ ~ Int => PersonAge
  type instance PersistEntityBackend (PersonGeneric backend) =
      backend

数据实例instance EntityField (PersonGeneric backend) typ 有三个数据构造函数,这是有道理的(数据库中的每一列一个),但即使在查看了波浪号在haskell 中的作用之后,我也无法理解它在那里做了什么。为什么=>,通常用于通用量化,在似乎不限制任何类型的东西之后使用?

如果我能以某种方式更清楚,请告诉我。

【问题讨论】:

    标签: haskell yesod persistent


    【解决方案1】:

    此语法用于声明没有 GADT 语法的 GADT。

    例如,

    data Z a b = (a ~ Int, b ~ Bool) => Z1 a b
               | (Show a, b ~ Float) => Z2 a b
    

    等价于

    data Z a b where
        Z1 :: Int -> Bool -> Z Int Bool
        Z2 :: Show a => a -> Float -> Z a Float
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 2012-06-14
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      相关资源
      最近更新 更多