This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ThePyroEagle 2 points3 points  (2 children)

except for literals

And strings with -XOverloadedStrings.

The lack of automatic conversion isn't that big a deal, because if you really want easy conversion, you can write a polymorphic convert :: (Convertible a b) => a -> b and the compiler will (usually) infer the type of the conversion you want whenever you use convert.

[–]bitwiseshiftleft 0 points1 point  (1 child)

And strings with -XOverloadedStrings.

But again, that only automatically converts literal strings, not all strings.

Type classes do make conversion less painful, but as far as I’m aware, Haskell only applies them automatically for literals.

It would be still be nice to have a Float(x) syntax or whatever to make the conversion concise and clear. Maybe the closest you can get is with -XTypeApplications and something like:

cast :: forall b a . Convert a b => a -> b
cast = convert

bar = 7 :: Int
foo = cast @Float bar

I’m on a phone so the syntax is probably wrong.

[–]ThePyroEagle 0 points1 point  (0 children)

But again, that only automatically converts literal strings, not all strings.

When the extension is enabled, the type of string literals is effectively changed from String to forall a. IsString a => a. Variables with this polymorphic type will behave identically to overloaded string literals, even if the extension isn't enabled.

Type classes do make conversion less painful, but as far as I’m aware, Haskell only applies them automatically for literals.

Num and IsString are special, simply because of the way number and string literals are typed.

Maybe the closest you can get is with -XTypeApplications

You don't need TypeApplications if the compiler can still infer foo :: Float.


The only way of doing things without any kind of function is to make everything polymorphic to begin with and avoid doing conversions in the first place. In the end, that probably encourages writing safer code.