Overloading and Literal Numerics
Hal Daume III
hdaume@ISI.EDU
Wed, 26 Jun 2002 14:24:18 -0700 (PDT)
The problem is that you might have:
instance Poly Double where ...
and then when you say:
po 5
it doesn't know whether this is an Int or a Double.
writing
po (5::Int)
should be sufficient.
--
Hal Daume III
"Computer science is no more about computers | hdaume@isi.edu
than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume
On Wed, 26 Jun 2002, Alain Cremieux wrote:
> Hi,
> I am trying to create an overloaded function "=E0 la Java" to be able to
> call it either with a string or a number.
> Ex :
> definePort "http"
> definePort 80
> but I have problem with restrictions in Haskell's type system (or with
> my lack of experience with it).
>=20
> The program :
>=20
> data PolyType =3D MkPolyLeft String | MkPolyRight (String, String) |
> MkPolyNum Int deriving Show
>=20
> class Poly a where
> poly :: a -> PolyType
>=20
> instance Poly String where
> poly s =3D MkPolyLeft s
> instance Poly (String, String) where
> poly p =3D MkPolyRight p
> instance Poly Int where
> poly i =3D MkPolyNum i
>=20
> po :: (Poly a) =3D> a -> PolyType
> po =3D poly
>=20
> tpo1, tpo2, tpo3 :: PolyType
> tpo1 =3D po "35"
> tpo2 =3D po ("36", "37")
> tpo3 =3D po 39
>=20
>=20
> gives the following result with ghc (5.03 & -fglasgow-exts) :
>=20
> cl.hs:21:
> Ambiguous type variable(s) `a' in the constraint `Poly a'
> arising from use of `po' at cl.hs:21
> In the definition of `tpo3': po 39
>=20
> cl.hs:21:
> Ambiguous type variable(s) `a' in the constraint `Num a'
> arising from the literal `39' at cl.hs:21
> In the first argument of `po', namely `39'
> In the definition of `tpo3': po 39
>=20
> I think I need the "closed" extension of the 'class' clause to do that
> (but it does not seem to be implemented yet).
>=20
> Is there a better solution ?
> Thank you,
> Alain
>=20
>=20
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>=20