[Haskell-cafe] Questions about haskell CPP macros

Stephan Friedrichs deduktionstheorem at web.de
Mon Jul 13 17:02:07 EDT 2009


Malcolm Wallace wrote:
> > {-# LANGUAGE CPP #-}
>> main = putStrLn (__FILE__ ++ ":" ++ show __LINE__)
>>
>> This outputs:
>> test.hs:2
> 
>> if I had a module Foo.Bar.Car.MyModule, I would like to be able to
>> output something like this on error:
>> Foo.Bar.Car.MyModule:2
> 
> It works for me.  If you place that text in Try/Me.hs and call
>     ghc -E Try/Me.hs
> you get
>     Try/Me.hs:2
> 
> If you just want to turn slashes into dots, and remove the suffix, that
> is a simple exercise in Haskell itself
> 
> main = putStrLn (mangle __FILE__)
>   where mangle ('/':cs) = '.': mangle cs
>         mangle .....

Careful, '/' might be '\\' on another OS, the file might end with .hsc
instead of .hs, the line numbers might not fit in the .hsc case...


-- 

Früher hieß es ja: Ich denke, also bin ich.
Heute weiß man: Es geht auch so.

 - Dieter Nuhr


More information about the Haskell-Cafe mailing list