For a language that has taken extreme measures to exclude generics because they are deemed to complex, this proposal is absolutely surprising to me. And I'm still not sure what practical benefit comes from it.
Proposal: expression to create pointer to simple types
21–30 of 110 posts
Re: Proposal: expression to create pointer to simple types
#22Re: Proposal: expression to create pointer to simple types
#23Can I create a pointer to a pointer?
Yes, however the utility of doing so in Go is fairly limited. Go has "pointers" but doesn't have pointer arithmetic, and the big use case of pointer-to-pointer in C is to iterate over an array of pointers via pointer arithmetic. Personally I would call them "references" since I consider pointer arithmetic to be the thing that makes pointers pointers and not just references, but that's a personal opinion, not a univer…
Re: Proposal: expression to create pointer to simple types
#24I was surprised that you can't apply & to any value. I thought it was gut an ordinary operator and it would ensure that the value it was applied to would be put onto the heap. s := S{} sp := &s // Works _ = sp _ = &S{} // Works i := int32(1) ip := &i // Works! _ = ip _ = &int32(1) // Doesn't work! https://play.golang.org/p/fdgvbEwJWgh It seems odd that you can't apply & to a function's return value. I think the best…
Offtopic: Surprisingly I was asking myself this question but if possible in C... Is it?
Re: Proposal: expression to create pointer to simple types
#25How exemplary that he filled in the full template questionnaire for language changes, including questions such as "Would you consider yourself a novice, intermediate, or experienced Go programmer?" (he replied "I have some experience").
Re: Proposal: expression to create pointer to simple types
#26How exemplary that he filled in the full template questionnaire for language changes, including questions such as "Would you consider yourself a novice, intermediate, or experienced Go programmer?" (he replied "I have some experience").
> What would change in the language spec?
> The new operator would get an optional second argument, and/or conversions would become addressible.
> [...]
> How would the language spec change?
> Answered above. Why is this question here twice?
Re: Proposal: expression to create pointer to simple types
#27I like the second option (&int(3)) the most personally, as I find myself occasionally defining a bunch of variables before I can use them as pointers in structs. It looks and feels a lot cleaner to use this vs having new everywhere.
I tend to not declare variables when the pointer is used deep into a struct because I find the back-and-forth in the editor to be bad. I usually resort to a pointer to an inline anonymous function, e.g.:
a := SomeStruct{
Field: func() *int64 { x := int64(13); return &x }(),
}
It's ugly and verbose but after seeing it 2 or 3 times you immediately know what it's about the next time.Re: Proposal: expression to create pointer to simple types
#28I was surprised that you can't apply & to any value. I thought it was gut an ordinary operator and it would ensure that the value it was applied to would be put onto the heap. s := S{} sp := &s // Works _ = sp _ = &S{} // Works i := int32(1) ip := &i // Works! _ = ip _ = &int32(1) // Doesn't work! https://play.golang.org/p/fdgvbEwJWgh It seems odd that you can't apply & to a function's return value. I think the best…
One of the things I think the Go tutorials don't make a big enough deal of is that Go is relatively explicit about allocations. := isn't just a shortcut for declaring variables, it's an allocation, and an error to use it when it doesn't allocate. var X Sometype isn't just a declaration, it's an allocation. := kinda smears the clarity by not allocating if you have a variable on the left that is already allocated, and…
> := [...] an error to use it when it doesn't allocate.
> := [...] not allocating if you have a variable on the left that is already allocated,
This appears to be a contradiction.
I suppose you mean something like "error to use it when there's no possible context where that line of code would allocate"; what's an example of that?
Re: Proposal: expression to create pointer to simple types
#29> Fortran, C, Forth, Basic, C, C++, Java, Python, and probably more. Just not JavaScript
Forth is a "top-of-mind" language for Rob Pike. That's unexpected and incredibly cool :)
Re: Proposal: expression to create pointer to simple types
#30Why 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)
> Numeric constants represent exact values of arbitrary precision and do not overflow.