OverloadedLabels: No instance for IsLabel on record data type
Asked Answered
C

1

8

In the code below,

{-# LANGUAGE OverloadedLabels #-}
module Foo where

data R = R { x :: Int }

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

I expected this to type-check, but instead I get:

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})

Given the Overloaded Record Fields proposal, I expected there to be a built-in instance of IsLabel "x" (R -> Int). Is this still the case or does the implementation deviate from the proposal?

Cyclorama answered 15/3, 2020 at 22:6 Comment(0)
P
7

There is no IsLabel instance for OverloadedLabels in (at least current) base (cf. discussion here). You may want to use some libraries which define an orphan instance such as generic-lens. Of cource, you can define it by yourself:

{-# 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

Palpebrate answered 16/3, 2020 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.