[Haskell-cafe] Disambiguating a Num/RealFrac instance

Brandon Allbery allbery.b at gmail.com
Thu May 28 17:28:21 UTC 2015

On Tue, May 26, 2015 at 8:35 PM, <amindfv at gmail.com> wrote:

> Is there any way (without IncoherentInstances or Rebindablesyntax) that I
> can let the user write e.g. "giveGPA 4.0" (and "giveGPA 4") and get back "F
> 4" without getting type errors that "4.0"'s type is ambiguous? I can
> guarantee there won't be any additional instances to "ToGPA"

A typeclass with only one instance is nonsensical, and often a symptom of
trying to use typeclasses as OO classes. All it's doing here is hurting you.

In this case, you don't really want to do anything special at all:

    giveGPA :: Float -> GPA
    giveGPA f = F f

and let Haskell's numeric overloading resolve 4 as (fromInteger 4 ::
Float). (Use of the typeclass blocks this.)

brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150528/128c0578/attachment.html>

More information about the Haskell-Cafe mailing list