Skip to content
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

math: make the constants typed #21228

Closed
chmllr opened this issue Jul 31, 2017 · 10 comments
Closed

math: make the constants typed #21228

chmllr opened this issue Jul 31, 2017 · 10 comments

Comments

@chmllr
Copy link

chmllr commented Jul 31, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8.3 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="~/.gocode"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6v/mjqvzyld5dn00gt1bj78tsrw0000gn/T/go-build139891339=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

I naively expected, that math.MaxInt64 is of type int64, when I do:

x := math.MaxInt64

More details: https://play.golang.org/p/UGmUgctFjn

What did you expect to see?

That this constant is of type int64 and this type would propagate to the variable I initialize by assignment.

What did you see instead?

x is of type int

@cznic
Copy link
Contributor

cznic commented Jul 31, 2017

Making the constants typed would render them useless/hard to work with more often than not. See also blog: Constants.

@ALTree ALTree changed the title Make the constants in the math package typed math: make the constants typed Jul 31, 2017
@ALTree
Copy link
Member

ALTree commented Jul 31, 2017

This will break the go1 promise of compatibility, so it's not something that can be changed before an hypothetical go2. As explained in the blog post linked above, having all the math constants live in the same space (the space of untyped numbers) has many advantages.

Closing this, since it was brought up many times in the past and I don't think making the constants typed was ever seriously considered.

@ALTree ALTree closed this as completed Jul 31, 2017
@chmllr
Copy link
Author

chmllr commented Jul 31, 2017

@ALTree thanks for the explanation!

@cznic you say that making smth like MaxInt64 of type int64 would render it useless or hard to work more often than not. That means, you have multiple examples in mind. Could you please bring at least one, where the type would make this constant hard to be used? A genuine question. Thank you!

@cznic
Copy link
Contributor

cznic commented Jul 31, 2017

@chmllr
Copy link
Author

chmllr commented Jul 31, 2017

Thanks, but you misunderstood. I was referring to exactly the constant i mentioned in my issue:

Could you please bring at least one, where the type would make this constant hard to be used?

@ALTree
Copy link
Member

ALTree commented Jul 31, 2017

Well, MaxInt64 being typed, for example, would make it harder to explain why some constants are untyped while others are not : )

@chmllr
Copy link
Author

chmllr commented Jul 31, 2017

I understand. Still, the example of @cznic shows that an untyped constant allows to be less explicit and some magic starts to creep into code — exactly the sort of things Go tries to avoid usually. But yes, you cannot please everyone all the time...

@cznic
Copy link
Contributor

cznic commented Jul 31, 2017

I was referring to exactly the constant i mentioned in my issue:

The issue title is "make the constants typed". Anyway: https://play.golang.org/p/UoEmbimvv3

@cznic
Copy link
Contributor

cznic commented Jul 31, 2017

BTW: The example does not make sense, but the point is comparing typed constants no longer works across types.

@chmllr
Copy link
Author

chmllr commented Jul 31, 2017

@cznic yes this is what I suspected: I see no point in untyped math constans in real world usage. Even the example with uint8 is something I'd prefer not to work like that. So I'm fully ok with this not being fixed ever. I'm just still not convinced that it is a "feature" and tried to understand where's the flaw in my thinking.

Thanks everyone!

@golang golang locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants