Live data from Hacker News

Proposal: expression to create pointer to simple types

github.com

21–30 of 110 posts

Re: Proposal: expression to create pointer to simple types

#21

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.

For a language that has taken extreme measures to exclude generics, Go seems to have an awful lot of accepted design proposals for generics.

Re: Proposal: expression to create pointer to simple types

#23
post #14
post #2

Can 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…

Pointer-pointers are nice to implement linked data structures in C; they make a lot of logic surrounding re-seating the head pointer far simpler and with fewer edge cases.

Re: Proposal: expression to create pointer to simple types

#24

I 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…

>It seems odd that you can't apply & to a function's return value.

Offtopic: Surprisingly I was asking myself this question but if possible in C... Is it?

Re: Proposal: expression to create pointer to simple types

#26
post #3

How 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").

Also good:

> 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

#27

I 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.

That would be my preference too. Great readability, and most users would eventually try this out even before searching for the right way (I have tried it).

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

#28
post #16

I 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…

I think the way to say it is that Go requires you to declare every allocation, but allows over-declaration in the case of copying.

> := [...] 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

#30
post #22

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)

From the language specification:

> Numeric constants represent exact values of arbitrary precision and do not overflow.

Post reply on HN