on master help sign in
Add IsLabel instance
5b0c027Csongor Kiss
examples/Examples.hs+8
@@ -20,6 +20,7 @@ import Data.Generics.Internal.Lens
20import Data.Generics.Product
20import Data.Generics.Product
21import Data.Generics.Sum
21import Data.Generics.Sum
22import GHC.Generics
22import GHC.Generics
23+import Data.Generics.Labels
23
24
24data Animal = Animal
25data Animal = Animal
25 { name :: String
26 { name :: String
@@ -102,3 +103,10 @@ stuff ::
102 , HasField "bar" a' b' s t
103 , HasField "bar" a' b' s t
103 ) => s' -> t'
104 ) => s' -> t'
104stuff r = r & field @"test" . field @"bar" . position @15 .~ "hello"
105stuff r = r & field @"test" . field @"bar" . position @15 .~ "hello"
106+
107+stuff' ::
108+ ( HasPosition 15 s t a String
109+ , HasField "test" s' t' a' b'
110+ , HasField "bar" a' b' s t
111+ ) => s' -> t'
112+stuff' r = r & #test . #bar . position @15 .~ "hello"
generic-lens.cabal+1
@@ -25,6 +25,7 @@ library
25 , Data.Generics.Product.Positions
25 , Data.Generics.Product.Positions
26 , Data.Generics.Product.Subtype
26 , Data.Generics.Product.Subtype
27 , Data.Generics.Product.Typed
27 , Data.Generics.Product.Typed
28+ , Data.Generics.Labels
28
29
29 , Data.Generics.Sum
30 , Data.Generics.Sum
30 , Data.Generics.Sum.Any
31 , Data.Generics.Sum.Any
src/Data/Generics/Labels.hsnew file+36
@@ -0,0 +1,36 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE FlexibleInstances #-}
3+{-# LANGUAGE MultiParamTypeClasses #-}
4+{-# LANGUAGE ScopedTypeVariables #-}
5+{-# LANGUAGE TypeApplications #-}
6+{-# LANGUAGE TypeFamilies #-}
7+{-# LANGUAGE UndecidableInstances #-}
8+{-# OPTIONS_GHC -Wno-orphans #-}
9+
10+
11+--------------------------------------------------------------------------------
12+-- |
13+-- Module : Data.Generics.Labels
14+-- Copyright : (C) 2017 Csongor Kiss
15+-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
16+-- License : BSD3
17+-- Stability : experimental
18+-- Portability : non-portable
19+--
20+-- Provide an (orphan) IsLabel instance for field lenses
21+--
22+--------------------------------------------------------------------------------
23+
24+module Data.Generics.Labels () where
25+
26+import Data.Generics.Product
27+import GHC.OverloadedLabels
28+
29+
30+instance (HasField field s t a b, Functor f, sft ~ (s -> f t)) =>
31+ IsLabel field ((a -> f b) -> sft) where
32+#if __GLASGOW_HASKELL__ >= 802
33+ fromLabel = field @field
34+#else
35+ fromLabel _ = field @field
36+#endif