0

I was reading some blog post related to Yesod (http://www.yesodweb.com/blog/2012/04/client-side) from 2012 when a wild instance declaration with constraint appeared I don't even know how to name.

instance (JSTypeString ~ jstype) => IsString (JSValue jstype) where

Can anybody tell me what exactly the tilde does in constraint place? I guessed it means something like jstype must be of type JSTypeString but I'm not sure. A name for this would be nice to know, too.

Thanks in advance!

bash0r
  • 774
  • 6
  • 17
  • It's called "equality constraint". – Sebastian Redl Feb 10 '16 at 09:55
  • 1
    And the reason it is used here instead of simply `instance IsString (JSValue JSTypeString)` is probably related to this answer: http://stackoverflow.com/questions/11553705/haskell-equality-constraint-in-instance - though I can't claim to really understand it. – Sebastian Redl Feb 10 '16 at 09:57
  • 1
    The instance `IsString (JSValue JSTypeString)` will only ever be selected if the type is known to be precisely `JSValue JSTypeString`. If it is `JSValue x` for some type variable `x`, then this instance will not be selected. However, the `(JSTypeString ~ jstype) => IsString (JSValue jstype)` instance will be selected for `JSValue x` for any x, *after* which the compiler will throw an error if `x` is not `JSTypeString`. – user2407038 Feb 10 '16 at 10:06
  • @SebastianRedl Ah yes it's a duplicate. I just couldn't find anything on Google to get a grip of it's name. Thanks a lot! – bash0r Feb 10 '16 at 10:58
  • @user2407038 Thanks for explanation. I was looking for this feature for some time now and stumbled upon it by accident. Thanks for explanation. :) – bash0r Feb 10 '16 at 10:58

0 Answers0