纯脚本行多态性。正确的语法是什么?
Purescript row polymorphism. What is the correct syntax?
使用基于 "Purescript by Example" 第 5 章中示例的函数,对如何声明多态行类型有点困惑。
下面编译正常
type Student = {
first :: String,
last :: String,
class :: String
}
type GymMember = {
first :: String,
last :: String,
benchPressPB :: Int
}
daveG :: GymMember
daveG = {
first: "Dave",
last: "Bro",
benchPressPB: 300
}
philS :: Student
philS = {
first : "Dave",
last : "Swat",
class : "1A"
}
schoolRollName :: forall t15.
{ last :: String
, first :: String
| t15
} -> String
schoolRollName rec = rec.last <> ", " <> rec.first
firstAndSurname :: forall t82.
{ first :: String
, last :: String
| t82
}
-> String
firstAndSurname rec = rec.first <> " " <> rec.last
daveFandS :: String
daveFandS = firstAndSurname daveG
daveSR :: String
daveSR = schoolRollName daveG
philFandS :: String
philFandS = firstAndSurname philS
philSR :: String
philSR = schoolRollName philS
但是如何删除 schoolRollName 和 firstAndSurname 的类型签名中的重复项。
我认为以下方法可行,但类型不匹配:
type NamedThing = forall t15.
{ last :: String
, first :: String
| t15
}
schoolRollName :: NamedThing -> String
schoolRollName rec = rec.last <> ", " <> rec.first
firstAndSurname :: NamedThing -> String
firstAndSurname rec = rec.first <> " " <> rec.last
-- !! Could not match type
daveFandS :: String
daveFandS = firstAndSurname daveG
NamedThing 声明不正确。要匹配 NamedThing 类型,您必须提供一个有效的值 forall 可能的记录至少first 和 last 字段。由于 daveG 不是编译器抱怨的值 - 更进一步,这种类型没有值。
将 t 移动到类型别名:
type NamedThing t = {first :: String, last :: String | t}
现在 firstAndSurname 确实必须提供工作函数 forall NamedThings 与任何额外字段。简单的解决方案:
firstAndSurname :: forall t. NamedThing t -> String
firstAndSurname rec = rec.first <> " " <> rec.last
编译器终于满意了:
daveFandS :: String
daveFandS = firstAndSurname daveG
使用基于 "Purescript by Example" 第 5 章中示例的函数,对如何声明多态行类型有点困惑。
下面编译正常
type Student = {
first :: String,
last :: String,
class :: String
}
type GymMember = {
first :: String,
last :: String,
benchPressPB :: Int
}
daveG :: GymMember
daveG = {
first: "Dave",
last: "Bro",
benchPressPB: 300
}
philS :: Student
philS = {
first : "Dave",
last : "Swat",
class : "1A"
}
schoolRollName :: forall t15.
{ last :: String
, first :: String
| t15
} -> String
schoolRollName rec = rec.last <> ", " <> rec.first
firstAndSurname :: forall t82.
{ first :: String
, last :: String
| t82
}
-> String
firstAndSurname rec = rec.first <> " " <> rec.last
daveFandS :: String
daveFandS = firstAndSurname daveG
daveSR :: String
daveSR = schoolRollName daveG
philFandS :: String
philFandS = firstAndSurname philS
philSR :: String
philSR = schoolRollName philS
但是如何删除 schoolRollName 和 firstAndSurname 的类型签名中的重复项。
我认为以下方法可行,但类型不匹配:
type NamedThing = forall t15.
{ last :: String
, first :: String
| t15
}
schoolRollName :: NamedThing -> String
schoolRollName rec = rec.last <> ", " <> rec.first
firstAndSurname :: NamedThing -> String
firstAndSurname rec = rec.first <> " " <> rec.last
-- !! Could not match type
daveFandS :: String
daveFandS = firstAndSurname daveG
NamedThing 声明不正确。要匹配 NamedThing 类型,您必须提供一个有效的值 forall 可能的记录至少first 和 last 字段。由于 daveG 不是编译器抱怨的值 - 更进一步,这种类型没有值。
将 t 移动到类型别名:
type NamedThing t = {first :: String, last :: String | t}
现在 firstAndSurname 确实必须提供工作函数 forall NamedThings 与任何额外字段。简单的解决方案:
firstAndSurname :: forall t. NamedThing t -> String
firstAndSurname rec = rec.first <> " " <> rec.last
编译器终于满意了:
daveFandS :: String
daveFandS = firstAndSurname daveG