Hi Michael,<br><br>This is a known limitation. You can't refer to items in splices that haven't already been mentioned in the source. I don't know why it isn't (prominently) documented, it's definitely part of the folklore.<br>
<div><br></div><div>Ben</div><br><div>On Tue May 20 2014 at 11:43:25, Michael Alan Dorman <<a href="mailto:mdorman@ironicdesign.com">mdorman@ironicdesign.com</a>> wrote:</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hey, all,<br>
<br>
While refactoring some code, I moved a newtype declaration, and suddenly<br>
the compiler (up-to-date ghc-7.6.3 on Debian) started complaining that<br>
it wasn't in scope when I attempted to use it in an ADT.<br>
<br>
I'm still new to Haskell, so I assumed it was my misunderstanding, and<br>
looked for documentation of declaration ordering constraints and<br>
couldn't find any.  So then I went to make a toy case to demonstrate the<br>
problem, and it worked:<br>
<br>
    data Baz = Baz { foo :: Foo, bar :: !(Maybe Bar) }<br>
    newtype Foo = Foo String<br>
    newtype Bar = Bar String<br>
<br>
So I started looking at the differences between that and my real code,<br>
and the only thing that seemed at all signficant was that I was using TH<br>
to generate some lenses.  And sure enough, adding that caused it to<br>
fail:<br>
<br>
    {-# LANGUAGE TemplateHaskell #-}<br>
    import Control.Lens<br>
    data Baz = Baz { foo :: Foo, bar :: !(Maybe Bar) }<br>
    makeLenses ''Baz<br>
    newtype Foo = Foo String<br>
    newtype Bar = Bar String<br>
<br>
If you feed this to ghci, you now get:<br>
<br>
    test.hs:3:25: Not in scope: type constructor or class `Foo'<br>
<br>
    test.hs:3:45:<br>
        Not in scope: type constructor or class `Bar'<br>
        Perhaps you meant `Baz' (line 3)<br>
<br>
Wondering if lens was somehow doing something so exotic it was breaking<br>
things, I tried aeson's TH support instead:<br>
<br>
    {-# LANGUAGE TemplateHaskell #-}<br>
    import Data.Aeson<br>
    data Baz = Baz { foo :: Foo, bar :: !(Maybe Bar) }<br>
    deriveJSON defaultOptions ''Baz<br>
    newtype Foo = Foo String<br>
    newtype Bar = Bar String<br>
<br>
Exact same error.<br>
<br>
That said, if I move the splices to the end of the file, everything<br>
works:<br>
<br>
    {-# LANGUAGE TemplateHaskell #-}<br>
    import Data.Aeson<br>
    data Baz = Baz { foo :: Foo, bar :: !(Maybe Bar) }<br>
    newtype Foo = Foo String<br>
    newtype Bar = Bar String<br>
    deriveJSON defaultOptions ''Baz<br>
<br>
I looked at the GHC docs on TH, and read the wiki page, and didn't see<br>
anything suggesting that this was a known limitation, nor did I see a<br>
bug in the known bug list that seemed pertinent.<br>
<br>
So, I'm wondering if I've found an actual bug, or a known limitation<br>
whose documentation should be made more prominent (since it seems to me<br>
that it has pretty dramatic implications for program structure), or<br>
what?  And is there any way around it other than moving all the splices<br>
to the end of the source file?<br>
<br>
Mike.<br>
______________________________<u></u>_________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/haskell-cafe</a><br>
</blockquote>