Skip to content

Instantly share code, notes, and snippets.

@alexpeits
Last active August 4, 2018 09:50
Show Gist options
  • Save alexpeits/8ab6b31fddb0335893675a58901b950d to your computer and use it in GitHub Desktop.
Save alexpeits/8ab6b31fddb0335893675a58901b950d to your computer and use it in GitHub Desktop.
OverloadedLabels
> #a a
"alfa"
>
> #a b
<interactive>:29:1: error:
• Non type-variable argument
in the constraint: IsLabel "a" (M.Map [Char] Int -> t)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall t. IsLabel "a" (M.Map [Char] Int -> t) => t
> :t #a a
#a a :: [Char]
> :t #a b
#a b :: IsLabel "a" (IntMap -> t) => t
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
import Data.Proxy
import qualified Data.Map as M
import GHC.TypeLits
import GHC.OverloadedLabels
--
type StringMap = M.Map String String
a :: StringMap
a = M.fromList [("a", "alpha"), ("b", "beta")]
instance (KnownSymbol s, x ~ String) => IsLabel s (StringMap -> x) where
fromLabel m = m M.! symbolVal (Proxy @s)
--
type IntMap = M.Map String Int
b :: IntMap
b = M.fromList [("a", 1), ("b", 2)]
instance (KnownSymbol s) => IsLabel s (IntMap -> Int) where
fromLabel m = m M.! symbolVal (Proxy @s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment