Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-02-28 Thread Kaveh Shahbazian
You are right Axel, I should be more clear on when one needs to overwrite that method with the same name as type (with the same name) (method B of type *B). Sample: https://play.golang.org/p/rg5C_5Z5NhR (compiler error) Working example using type aliases: https://play.golang.org/p/PBHBvkSPUxW

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-02-28 Thread 'Axel Wagner' via golang-nuts
Can you elaborate/link to example code? Because it seems to work just fine . On Wed, Feb 28, 2018 at 6:58 PM Kaveh Shahbazian wrote: > BTW I have found an interesting usage for embedding type aliases. Assume a > type A embeds

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-02-28 Thread Kaveh Shahbazian
BTW I have found an interesting usage for embedding type aliases. Assume a type A embeds another type B that has a member named B. That will cause a compilation error since A has two members named B. Now if a type alias be defined as type OrigB = B, then A can embed OrigB without any problems.

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-23 Thread dc0d
Did not try that and changed that instance of this code. On Tuesday, January 23, 2018 at 9:04:36 PM UTC+3:30, rog wrote: > > Have you tried out the behaviour on Go tip (or the go 1.10 beta)? > > On 23 Jan 2018 14:31, "Josh Humphries" > wrote: > > Roger, > I don't believe

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-23 Thread roger peppe
Have you tried out the behaviour on Go tip (or the go 1.10 beta)? On 23 Jan 2018 14:31, "Josh Humphries" wrote: Roger, I don't believe that patch will change behavior. See my last message: the fields appear to be unexported according to reflection. That patch has the

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-23 Thread dc0d
Exactly. That's why I asked before why we are allowed to embed type aliases. It should be either not possible, or be properly handled by the type system (and tools). On Tuesday, January 23, 2018 at 6:02:54 PM UTC+3:30, Josh Humphries wrote: > > Roger, > I don't believe that patch will change

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-23 Thread Josh Humphries
Roger, I don't believe that patch will change behavior. See my last message: the fields appear to be unexported according to reflection. That patch has the encoding/json package checking StructField.PkgPath, which should be blank for exported fields. For these fields, it is "go.builtin", which

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-23 Thread roger peppe
This is a bug that has been fixed on Go tip by https://go-review.googlesource.com/c/go/+/65550. On 23 January 2018 at 00:45, Josh Humphries wrote: > I think have misspoken. Even though the field's name appears exported via > reflection (it has a name that starts with a

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-22 Thread Josh Humphries
I think have misspoken. Even though the field's name appears exported via reflection (it has a name that starts with a capital letter), attempting to use the reflect.Value's SetInt method panics, indicating that the field was obtained using an unexported field. So the encoding/json package is thus

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-22 Thread Dan Kortschak
The discordance is unexpected. On Mon, 2018-01-22 at 19:28 -0500, Josh Humphries wrote: > I think that is expected, and it is the JSON marshaling that is > surprising > (and erroneous). > > If it were expected that the embed field names resolved to the alias > *target > type* name, it would

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-22 Thread Josh Humphries
I think that is expected, and it is the JSON marshaling that is surprising (and erroneous). If it were expected that the embed field names resolved to the alias *target type* name, it would instead be a compiler error since the compiler does not allow embedded types that would result in name

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-22 Thread Dan Kortschak
This is sort of surprising though: https://play.golang.org/p/mjfkzIqAo_b On Mon, 2018-01-22 at 10:20 -0800, C Banning wrote: > From the Language Specification -  > > A field declared with a type but no explicit field name is called an > *embedded  > field*. An embedded field must be specified as

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-22 Thread C Banning
>From the Language Specification - A field declared with a type but no explicit field name is called an *embedded field*. An embedded field must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-22 Thread 'Axel Wagner' via golang-nuts
You are right, I am wrong. I wasn't aware that the struct field would be named after the alias, I assumed it would be named after the type itself. I agree that this looks like a bug in the json-package and that it should look at the field name itself. On Mon, Jan 22, 2018 at 4:47 PM, Josh

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-22 Thread Josh Humphries
I'm not trying to suggest that the example code makes sense or is something that one *should* do. But it is surprising behavior that is not expected, even after reading relevant parts of the language spec and the reflect and json package docs. It looks like this is a bug in the json package.

Re: [go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-22 Thread 'Axel Wagner' via golang-nuts
On Mon, Jan 22, 2018 at 8:50 AM, dc0d wrote: > Then the main question would be why is it possible to embed type aliases? > Because it is possible to embed types and an alias is simply another name for a type. For example, embedding an x/image/draw.Image

[go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-21 Thread dc0d
Then the main question would be why is it possible to embed type aliases? On Sunday, January 21, 2018 at 10:42:28 PM UTC+3:30, Kevin Malachowski wrote: > > Given the last playground post, I'd guess that the aliased type (i.e. > 'int' here) is being used for visibility rather than the alias's

[go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-21 Thread Kevin Malachowski
Given the last playground post, I'd guess that the aliased type (i.e. 'int' here) is being used for visibility rather than the alias's new name. It's also a little weird to use embedded types that dont need to be (there's no method promotion here because 'int' has no methods), or seeing aliases

[go-nuts] Re: JSON and Embedded Types (Aliases)

2018-01-21 Thread C Banning
https://play.golang.org/p/SiWmBrUYUXF On Saturday, January 20, 2018 at 11:16:20 PM UTC-7, dc0d wrote: > > Playground , output: {"Test":100}. > > On Sunday, January 21, 2018 at 9:42:39 AM UTC+3:30, dc0d wrote: >> >> Why embedded type aliases get ignored