New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: What is an Int? #742

Closed
expipiplus1 opened this Issue Oct 30, 2016 · 4 comments

Comments

Projects
None yet
5 participants
@expipiplus1

expipiplus1 commented Oct 30, 2016

I've been unable to determine what is representable in a value of type Int in Elm.

This page seems to suggest that Int is a 32bit two's complement integer http://learnyouanelm.github.io/pages/03-types.html

Playing around in the REPL suggests that Int is actually represented as a double precision float. 2^53 is the largest value such that every positive integer below it is representable.

@jvoigtlaender jvoigtlaender added the docs label Oct 30, 2016

@sgraf812

This comment has been minimized.

Show comment
Hide comment
@sgraf812

sgraf812 Oct 31, 2016

Also, the usual weird JavaScript hacks exist if you are concerned about precision. I'm making use of this in intdict, requiring the user to make sure they are actually passing valid integers (yuck!).

Perhaps something like this is worthwhile to have in core?

sgraf812 commented Oct 31, 2016

Also, the usual weird JavaScript hacks exist if you are concerned about precision. I'm making use of this in intdict, requiring the user to make sure they are actually passing valid integers (yuck!).

Perhaps something like this is worthwhile to have in core?

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Nov 1, 2016

Member

Because Elm targets JavaScript, there are certain problems with integers that we cannot fix without very severe performance costs. An Elm Int should be a 32-bit integer, but there is no such thing in JavaScript. So you can think of this as the compiler being non-compliant. The problem is that compliance is basically impossible until we are targeting WebAssembly directly, or some other platform.

One of my goals with Elm is that we should never willingly inherit silly JavaScript problems. This is a case where the language is designed aspirationally, so when the day comes that we can have real integers (through whatever means) Elm does not end up being dumb.

Does that answer your question? Also, I don't know where this should be documented. If someone can find a nice place, maybe it makes sense to do.

Member

evancz commented Nov 1, 2016

Because Elm targets JavaScript, there are certain problems with integers that we cannot fix without very severe performance costs. An Elm Int should be a 32-bit integer, but there is no such thing in JavaScript. So you can think of this as the compiler being non-compliant. The problem is that compliance is basically impossible until we are targeting WebAssembly directly, or some other platform.

One of my goals with Elm is that we should never willingly inherit silly JavaScript problems. This is a case where the language is designed aspirationally, so when the day comes that we can have real integers (through whatever means) Elm does not end up being dumb.

Does that answer your question? Also, I don't know where this should be documented. If someone can find a nice place, maybe it makes sense to do.

@evancz evancz closed this Nov 1, 2016

@expipiplus1

This comment has been minimized.

Show comment
Hide comment
@expipiplus1

expipiplus1 Nov 5, 2016

Would it be possible to keep this issue open until this information finds a place in the documentation?

expipiplus1 commented Nov 5, 2016

Would it be possible to keep this issue open until this information finds a place in the documentation?

expipiplus1 referenced this issue in expipiplus1/elm-export Nov 5, 2016

Remove ElmType instances of non-32bit int types
Elm's `Int` type is a 32bit two's complement integer. The exact size of
Haskell's `Int` type is unspecified. Having instances for integral types
which don't have an isomorphism to Elm types allows for lossy
conversions when moving data between the two languages. For example
sending an Elm Int to a Haskell function with type `Int8 -> Int8` will
only work with 8 bit values and raise an error for any out of range
values. By only providing instances for Int32 the user is forced into
being more careful with how they marshall integer types between Haskell
and Elm.

See https://github.com/elm-lang/core/issues/742#issuecomment-257484290
for information on the specifics of Elm's Int type.
@lukewestby

This comment has been minimized.

Show comment
Hide comment
@lukewestby

lukewestby Nov 5, 2016

Member

This sounds like a good addition to http://faq.elm-community.org

Member

lukewestby commented Nov 5, 2016

This sounds like a good addition to http://faq.elm-community.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment