3

I have a function that returns a dynamically-bound Type - in essence, ConT $ mkName "MyType". Of course, the actual function is significantly more complicated, enough so that I'd like to write tests for it, and preferably legible ones. But the following:

import Language.Haskell.TH
import MyTypeModule (MyType)

myFn :: Type    
myFn = ConT $ mkName "MyType"

test = ... $ do
  m <- runQ [t| MyType |]
  myFn `shouldBe` m

Will always fail, since m will resolve to ConT MyTypeModule.MyType rather than to ConT MyType.

Is there a nice way to transform the myFn type to something fully-qualified, or otherwise check that m and myFn are the same (in the current context)?

user2141650
  • 2,827
  • 1
  • 15
  • 23
  • I think the simplest thing to do here would be to write a custom `==` function which compares `Name`s in a way that the quoted name and not-quoted name are equal. This is the same as stripping away module qualifiers from both types and comparing them normally. – user2407038 Feb 16 '15 at 19:44

1 Answers1

2

Wow long time without an answer, but never too late to post answers on the Internet!

myFn = ConT ''MyType

The ticks are enabled with the -XTemplateHaskell language extension, and are documented here:

https://hackage.haskell.org/package/template-haskell-2.13.0.0/docs/Language-Haskell-TH-Syntax.html#t:Name

  • Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Greg the Incredulous Mar 05 '19 at 02:00