on master help sign in
Merge pull request #119 from christiaanb/data-instance-lazy-text

Add feature parity Data instance Text and Text.Lazy

commit1da8b9b9460bee60a7a73b7e18c268dd66699a77 authorBryan O'Sullivan <bos@serpentine.com> dateSat 9 May 2015, 10:00 (11 years ago) parentc5689ec2968631
1 file changed+18−5
Data/Text/Lazy.hs+18−5
Data/Text/Lazy.hs+18−5
@@ -210,8 +210,8 @@ import Control.DeepSeq (NFData(..))
210210import Data.Int (Int64)
211211import qualified Data.List as L
212212import Data.Char (isSpace)
213−import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf))
214−import Data.Data (mkNoRepType)
213+import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf), constrIndex,
214+ Constr, mkConstr, DataType, mkDataType, Fixity(Prefix))
215215import Data.Monoid (Monoid(..))
216216import Data.String (IsString(..))
217217import qualified Data.Text as T
@@ -353,11 +353,24 @@ instance NFData Text where
353353 rnf (Chunk _ ts) = rnf ts
354354#endif
355355
356+-- | This instance preserves data abstraction at the cost of inefficiency.
357+-- We omit reflection services for the sake of data abstraction.
358+--
359+-- This instance was created by copying the updated behavior of
360+-- @"Data.Text".@'Data.Text.Text'
356361instance Data Text where
357362 gfoldl f z txt = z pack `f` (unpack txt)
358− toConstr _ = error "Data.Text.Lazy.Text.toConstr"
359− gunfold _ _ = error "Data.Text.Lazy.Text.gunfold"
360− dataTypeOf _ = mkNoRepType "Data.Text.Lazy.Text"
363+ toConstr _ = packConstr
364+ gunfold k z c = case constrIndex c of
365+ 1 -> k (z pack)
366+ _ -> error "Data.Text.Lazy.Text.gunfold"
367+ dataTypeOf _ = textDataType
368+
369+packConstr :: Constr
370+packConstr = mkConstr textDataType "pack" [] Prefix
371+
372+textDataType :: DataType
373+textDataType = mkDataType "Data.Text.Lazy.Text" [packConstr]
361374
362375-- | /O(n)/ Convert a 'String' into a 'Text'.
363376--