扩展现有记录类型

WHI*_*LOR 3 purescript

如果我有记录类型:

type Rec_ab = { a :: String, b :: Int }
Run Code Online (Sandbox Code Playgroud)

我可以从它的类型中得到有效扩展的{c :: Boolean}:{ a :: String, b :: Int, c :: Boolean }吗?

如果我有一行,我可以这样做:

Row_ab = ( a :: String, b :: Int )

Rec_abc = { c :: Boolean | Row_ab }

Run Code Online (Sandbox Code Playgroud)

但如果我有记录Rec_ab应该如何解决?我不知道从“记录”->“行”的方式。

Fyo*_*kin 5

一般来说,你不能。

如果您需要这种可扩展性,您的方法Row_ab就足够了,但另一种可能的解决方案是参数化记录,然后使用参数来扩展它:

type Rec_ab r = { a :: String, b :: Int | r }
type Rec_abc = Rec_ab ( c :: Boolean )
Run Code Online (Sandbox Code Playgroud)

这还允许您编写可与Rec_ab和一起使用的函数Rec_abc

add42 :: forall r. Rec_ab r -> Rec_ab r
add42 r = r { b = r.b + 42 }
Run Code Online (Sandbox Code Playgroud)