[Haskell-beginners] Getting a variables type as a string at runtime

Gareth Morgan gmorgan1984 at gmail.com
Sun Jan 12 14:47:20 UTC 2014


Won't show include all the components? I wanted to include only the type
name.

I could probably get away with bundling all the contents into the error
string but it would be nice to know if there is a standard way to get type
names like this.


On Sun, Jan 12, 2014 at 2:33 PM, fa-ml <fa-ml at ariis.it> wrote:

> On Sun, Jan 12, 2014 at 02:04:40PM +0000, Gareth Morgan wrote:
> > I have a sum type, when the input is type A I want to process it, when it
> > is type B I want to return MyError "Expected type A but actually got type
> > B". However I also have C,D,E,F, etc which should also return similar
> > errors so don't want to hardcode this string.
>
> Would pattern matching be enough for you? i.e. (very crude):
>
>     data Test = Alfa | Beta | Gamma deriving (Show)
>
>     testAlfa Alfa = undefined -- put your process function here
>     testAlfa o    = error $ "wasn't expecting " ++ show o
>
>     main = testAlfa Beta
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140112/89434c11/attachment.html>


More information about the Beginners mailing list