Well you're talking about method receivers, not struct fields. It's a 
similar shortcut, but sorry about going off topic with the example.

Matt

On Monday, December 18, 2017 at 5:05:19 PM UTC-6, matthe...@gmail.com wrote:
>
> Here's a specific example of how this works for me. I have a chess board 
> represented as a 64 element array of points:
>
> type Point struct {
>  *Piece // nil for no piece
>  AbsPoint
> }
> // Absolute Point represents a specific point on the board.
> type AbsPoint struct {
>  File uint8
>  Rank uint8
> }
> type Piece struct {
>  Kind
>  Orientation
>  Base  Kind
>  Moved bool `json:"-"`
> }
> type Kind int
>
> const (
>  King Kind = iota + 1
>  Queen
>  Rook
>  Bishop
>  ...
>
> Often I'll be iterating over a subset of points on the board and want to 
> check what kind of piece is there. The pointer shortcut (and struct 
> embedding) definitely cleans up my code.
>
> for _, point := range set {
>   if point.Piece != nil {
>     if (point.Kind == King) && (point.Moved == false) && (point.
> Orientation == mover) {
>       // do something
>       break
>     }
>   }
> }
> // instead of
> for _, point := range set {
>   if point.Piece != nil {
>     if (*(point.Piece).Kind == King) && (*(point.Piece).Moved == false) && 
> (*(point.Piece).Orientation == mover) {
>     // or if (point.Piece->Kind == King) && (point.Piece->Moved == false) 
> && (point.Piece->Orientation == mover) {
>       // do something
>       break
>     }
>   }
> }
>
> Given more pieces than regular chess this kind of logic happens many times 
> and I appreciate the reduced word count. For a newcomer it may be 
> surprising and require a type lookup, but newcomers aren't maintaining the 
> code.
>
> Matt
>
> On Monday, December 18, 2017 at 3:05:54 PM UTC-6, jlfo...@berkeley.edu 
> wrote:
>>
>>
>>
>> On Monday, December 18, 2017 at 12:12:22 PM UTC-8, Dave Cheney wrote:
>>>
>>> It's true it is an exception, it's one of the few cases where the 
>>> language adds a pinch of syntactic sugar to make the experience more 
>>> pleasurable.
>>>
>>
>> I'd describe this more as removing a pinch of syntactic sugar. 
>>
>> I can imagine without this the number one oft repeated feature request 
>>> would be to _not_ have to write (&t).m() all the time when you just wanted 
>>> to write t.m(). 
>>>
>>
>> Maybe so, but you know where that leads. Soon those people will start 
>> complaining about the requirement for explicit type conversions too.
>>
>> Anyway, thanks for confirming my reaction.
>>
>> Jon
>>
>>  
>>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to