Re:Proposal: expression to create pointer to simple types
by uh_uh on Monday April 19, 2021 @10:24AM
(#26863057)
Why can't &3 work? Rob says 3 does not have a type and that's a problem. Would it be possible to change the Go compiler such that 3 has a type? (I'm guessing no, at least not easily, otherwise he'd be suggesting it, but I'm curious about the reason)
[ Parent
| on HN ]
Re:Proposal: expression to create pointer to simple types
by masklinn on Monday April 19, 2021 @11:21AM
(#26863729)
> Would it be possible to change the Go compiler such that 3 has a type? (I'm guessing no, at least not easily, otherwise he'd be suggesting it, but I'm curious about the reason)
Why not? In fact it already kind-of does: Go has "default types" for most untyped constants. When you write
i := 3
absent an explicit type, Go will fall back to "int".
Re:Proposal: expression to create pointer to simple types
by fred256 on Monday April 19, 2021 @10:32AM
(#26863157)
From the language specification:
> Numeric constants represent exact values of arbitrary precision and do not overflow.
Re:Proposal: expression to create pointer to simple types
by friseurtermin on Monday April 19, 2021 @01:00PM
(#26865071)
Ok I think I'm not understanding this correctly then. Why does this return an error?
package main
import (
"fmt"
)
const tst = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
func main() {
fmt.Println("%v", tst)
}
Error:
./prog.go:16:17: constant 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 overflows int
Re:Proposal: expression to create pointer to simple types
by howeyc on Monday April 19, 2021 @01:22PM
(#26865411)
https://golang.org/ref/spec#Constants
> An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required.
The default type for a number (integer) is int. If you were to add a period in that long string of zeros, the default for floating-point is float64.
Re:Proposal: expression to create pointer to simple types
by HALtheWise on Monday April 19, 2021 @04:57PM
(#26867904)
If you look closely, that error happens at the print line statement, not at the constant declaration. The constant is perfectly legal to exist as written, and you can even do constant operations like mathematics on it, you just can't legally assign it to a variable (implicitly in this case as part of the function call) if it's value is too large for the type of that variable.
Re:Proposal: expression to create pointer to simple types
by friseurtermin on Tuesday April 20, 2021 @04:24AM
(#26872094)
I see, that makes sense. Thanks!
Re:Proposal: expression to create pointer to simple types
by Laremere on Monday April 19, 2021 @01:18PM
(#26865351)
I asked that in the thread, response here:
https://github.com/golang/go/issues/45624#issuecomment-82259...
With my reply (and further exploration) here:
https://github.com/golang/go/issues/45624#issuecomment-82263...
tl;dr: Go uses casts to coerce values to the correct types, which means you couldn't get pointers to number literals for non-default number types.