Thank you Jason and Axel. I know that "nil" is predeclared identifier from watching talk on YT "Understending nil" (or something like that) and know that it can be redefined. But, due to my background I still struggle with such things in Go. Old habits die hard, nothing more.
Thank you Axel, you points are very helpful to me. They remained me title of ones of Rob Pikes talks about Go "Simplicity is complicated". Best, Kamil piątek, 11 lutego 2022 o 21:21:03 UTC+1 axel.wa...@googlemail.com napisał(a): > FWIW two useful aspects of this design are > 1. It makes the language simpler, as we have fewer keywords and the > grammar doesn't need to mention `int`/… everywhere > 2. It makes it possible to extend the set of predeclared identifiers, > without worrying about breakages. For example, we are adding a new `any` > predeclared identifier in Go 1.18. This won't break any (hehe) code, as > code which uses that identifier will just refer to the one it already > declares. > > On Fri, Feb 11, 2022 at 8:38 PM Jason Phillips <jasonrya...@gmail.com> > wrote: > >> In C and C++[1][2] "int" and "float" are reserved keywords and thus can't >> be used as identifiers. In Go[3], "int" and "float64" are "predeclared >> identifiers"[4] and can be redefined, just like any other identifier. >> >> [1] - https://en.cppreference.com/w/c/keyword >> [2] - https://en.cppreference.com/w/cpp/keyword >> [3] - https://go.dev/ref/spec#Keywords >> [4] - https://go.dev/ref/spec#Predeclared_identifiers >> >> On Friday, February 11, 2022 at 1:20:37 PM UTC-5 kziem...@gmail.com >> wrote: >> >>> Hello, >>> >>> I have so background in C and C++ so was used to that you CAN'T define >>> variable with the name of predefined types. But, in Go 1.17 (go version >>> go1.17.7 linux/amd64) >>> > package main >>> > >>> > import "fmt" >>> > >>> > func main() { >>> > var int float64 = 2.5 >>> > >>> > fmt.Printf("int: %v, %T\n", int, int) >>> > >>> > // var varOne int = 1 >>> > >>> > // fmt.Printf("varOne: %v", varOne) >>> > } >>> this code compile and produce result >>> > int: 2.5, float64 >>> >>> When you uncomment two lines in this example you will get error >>> > int is not a type >>> >>> In Go 1.18beta2 (go version go1.18beta2 linux/amd64) >>> > package main >>> > >>> > import ( >>> > "fmt" >>> > // "math" >>> > ) >>> > >>> > type Constraint interface { >>> > float64 >>> > } >>> > >>> > func someFunction[uint8 Constraint](x uint8) uint8 { >>> > x += uint8(1) >>> > return x >>> > } >>> > >>> > func main() { >>> > var x float64 = 1 >>> > >>> > fmt.Printf("someFunction(%v): %v\n", x, someFunction(x)) >>> > fmt.Printf("x type: %T\n", x) >>> > fmt.Printf("someFunction(x) type: %T\n", someFunction(x)) >>> > } >>> this code compile and produce result >>> > someFunction(1): 2 >>> > x type: float64 >>> > someFunction(x) type: float64 >>> >>> Can anyone tell me why this behavior is possible in Go? In the past I >>> never write "var int int" because it is wicked and possible sinful, but >>> syntax for generic functions make easier for me to produce such evil things. >>> >>> This is a bit too much shocking things for me for one day, I will be >>> back tomorrow. >>> >>> Best, >>> Kamil >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/78714732-cd31-49cf-a941-2ed228eb2b7fn%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/78714732-cd31-49cf-a941-2ed228eb2b7fn%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/176cce79-e869-4afc-8c50-5b5b177f5704n%40googlegroups.com.