gpt4 book ai didi

haskell - 重载标签 : No instance for IsLabel on record data type

转载 作者:行者123 更新时间:2023-12-02 02:42:30 28 4
gpt4 key购买 nike

在下面的代码中,

{-# LANGUAGE OverloadedLabels #-}
module Foo where

data R = R { x :: Int }

g :: Int
g = #x (R { x = 1 })

我希望这会进行类型检查,但我得到了:
foo.hs:7:5: error:
• No instance for (GHC.OverloadedLabels.IsLabel "x" (R -> Int))
arising from the overloaded label ‘#x’
(maybe you haven't applied a function to enough arguments?)
• In the expression: #x
In the expression: #x (R {x = 1})
In an equation for ‘g’: g = #x (R {x = 1})

鉴于 Overloaded Record Fields建议,我希望有一个 IsLabel "x" (R -> Int) 的内置实例.仍然是这种情况还是实现偏离了提案?

最佳答案

没有IsLabel OverloadedLabels 的实例在(至少是当前的)基础中(参见讨论 here)。
您可能想要使用一些定义孤立实例的库,例如 generic-lens .
当然,您可以自己定义它:

{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}

import GHC.Records
import GHC.OverloadedLabels

instance HasField x r a => IsLabel x (r -> a) where
fromLabel = getField @x

关于haskell - 重载标签 : No instance for IsLabel on record data type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60698141/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com