Closed bergmark closed 10 years ago
Perhaps a bit overkill, but we could use TH to remove the unsafety.
Hmmm was I thinking about this wrong? Are these actually safe?
What do you mean by "safe" in this context?
e.g is this a valid instance (assuming we want to store Chars in int columns)?
instance ShowConstant Char Char where
showConstant = fmap unsafeCoerce . (showConstant :: Int -> Expr (Wire Int)) . ord
Yes it looks like a reasonable instance to me, as long as you want to represent Char
s as integers inside the database.
Ok, great. unsafe*
got me worried :-)
Do you think it's a good idea to include showThrough1
(with a better name)?
Wire.unsafeCoerce
is "unsafe" in the Postgres sense, not the Haskell sense, i.e. it can lead to Postgres rejecting the query but can't lead to a Haskell crash. It would be unsafe if you created two different instances, one which represents Wire Char
as Postgres integers and one which uses Postgres strings, say. Then you'll get Postgres type mismatches when you try to mix them.
Including something like showThrough1
sounds like a fine idea. I guess it would be used something like
newtype MyType = MyType { fromMyType :: Int }
instance ShowConstant MyType MyType where
showConstant = showThrough1 fromMyType
We could have a little bit of TH to remove this boilerplate too, or maybe just some simple generics.
I have this now:
newtype Id = IdX { unId :: Int }
newtypeColumn ''Id
======>
instance ShowConstant Id Id where
showConstant = showThrough (\ (IdX x_aUU9) -> x_aUU9)
instance Default QueryRunner (Wire Id) Id where
def = fieldQueryRunnerF IdX
Looks good. The Default QueryRunner
instance is already done by this though:
https://github.com/karamaan/karamaan-opaleye/blob/master/Karamaan/Opaleye/RunQuery/TH.hs
Ah I forgot that existed!
I wrote these helpers for defining
ShowConstant
instances:Neither of them actually guarantee that we have a newtype (i.e. that
unsafeCoerce
is safe), and I also don't likeControl.Newtype
since it exposes the constructor.Still, it's better than copy pasting the implementations from
ShowConstant.hs
.Any ideas to improve this?