Skip to content

Commit b5c313b

Browse files
ndwarshuislocallycompact
authored andcommitted
make option for adding strictness to TH-generated fields (dhall-lang#2504)
1 parent 8f827a3 commit b5c313b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

dhall/src/Dhall/TH.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ toConstructor
305305
toConstructor GenerateOptions{..} haskellTypes outerTypeName (constructorName, maybeAlternativeType) = do
306306
let name = Syntax.mkName (Text.unpack $ constructorModifier constructorName)
307307

308-
let bang = Bang NoSourceUnpackedness NoSourceStrictness
308+
let strictness = if makeStrict then SourceStrict else NoSourceStrictness
309+
310+
let bang = Bang NoSourceUnpackedness strictness
309311

310312
case maybeAlternativeType of
311313
Just dhallType
@@ -400,6 +402,8 @@ data GenerateOptions = GenerateOptions
400402
-- ^ Generate a `FromDhall` instance for the Haskell type
401403
, generateToDhallInstance :: Bool
402404
-- ^ Generate a `ToDhall` instance for the Haskell type
405+
, makeStrict :: Bool
406+
-- ^ Make all fields strict.
403407
}
404408

405409
-- | A default set of options used by `makeHaskellTypes`. That means:
@@ -412,6 +416,7 @@ defaultGenerateOptions = GenerateOptions
412416
, fieldModifier = id
413417
, generateFromDhallInstance = True
414418
, generateToDhallInstance = True
419+
, makeStrict = False
415420
}
416421

417422
-- | This function generates `Dhall.InterpretOptions` that can be used for the

dhall/tests/Dhall/Test/TH.hs

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
module Dhall.Test.TH where
99

10-
import Control.Exception (throwIO)
11-
import Data.Either.Validation (Validation(..))
12-
import Dhall.TH (HaskellType (..))
13-
import Test.Tasty (TestTree)
10+
import Control.Exception (throwIO)
11+
import Data.Either.Validation (Validation (..))
12+
import Dhall.TH (HaskellType (..))
13+
import Test.Tasty (TestTree)
1414

1515
import qualified Data.Text
1616
import qualified Dhall
@@ -153,3 +153,11 @@ Dhall.TH.makeHaskellTypesWith (Dhall.TH.defaultGenerateOptions
153153
deriving instance Dhall.Generic NoInstancesT
154154
instance Dhall.FromDhall NoInstancesT
155155
instance Dhall.ToDhall NoInstancesT
156+
157+
Dhall.TH.makeHaskellTypesWith (Dhall.TH.defaultGenerateOptions
158+
{ Dhall.TH.constructorModifier = ("Strict" <>)
159+
, Dhall.TH.fieldModifier = ("strict" <>) . Data.Text.toTitle
160+
, Dhall.TH.makeStrict = True
161+
})
162+
[ MultipleConstructors "StrictFields" "./tests/th/example.dhall"
163+
]

0 commit comments

Comments
 (0)