cheekybits / genny

Elegant generics for Go
MIT License
1.71k stars 126 forks source link

Not all instances of the argument type getting rewritten #10

Closed szabba closed 9 years ago

szabba commented 9 years ago

So, I decided to take a look at genny, just poking around, implementing a pretty dumb digraph...

package digraph

import "github.com/cheekybits/genny/generic"

type Node generic.Type

type DigraphNode struct {
    nodes map[Node][]Node
}

func NewDigraphNode() *DigraphNode {
    return &DigraphNode{
        nodes: make(map[Node][]Node),
    }
}

func (dig *DigraphNode) Add(n Node) {
    if _, exists := dig.nodes[n]; exists {
        return
    }

    dig.nodes[n] = nil
}

func (dig *DigraphNode) Connect(a, b Node) {
    dig.Add(a)
    dig.Add(b)

    dig.nodes[a] = append(dig.nodes[a], b)
}

But when I run

$ cat generic.go | genny gen Node=int

I get

// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny

package digraph

type DigraphInt struct {
    nodes map[int][]Node
}

func NewDigraphInt() *DigraphInt {
    return &DigraphInt{
        nodes: make(map[int][]Node),
    }
}

func (dig *DigraphInt) Add(n int) {
    if _, exists := dig.nodes[n]; exists {
        return
    }

    dig.nodes[n] = nil
}

func (dig *DigraphInt) Connect(a, b int) {
    dig.Add(a)
    dig.Add(b)

    dig.nodes[a] = append(dig.nodes[a], b)
}

As you can see, not all instances of the Node type in the code got rewritten.

tylerstillwater commented 9 years ago

Interesting. Thanks for the report. We'll look in to it!

matryer commented 9 years ago

What was it?

tylerstillwater commented 9 years ago

Multiple occurrences of Type in a single line was not handled. I put it in a loop that rewrites them all.

On Tue, Dec 23, 2014 at 9:54 AM, Mat Ryer notifications@github.com wrote:

What was it?

Reply to this email directly or view it on GitHub: https://github.com/cheekybits/genny/issues/10#issuecomment-67972680

matryer commented 9 years ago

Ha. Thanks for the report - that was a biggy :)

On Dec 23, 2014, at 08:55, Tyler notifications@github.com wrote:

Multiple occurrences of Type in a single line was not handled. I put it in a loop that rewrites them all.

On Tue, Dec 23, 2014 at 9:54 AM, Mat Ryer notifications@github.com wrote:

What was it?

Reply to this email directly or view it on GitHub: https://github.com/cheekybits/genny/issues/10#issuecomment-67972680 — Reply to this email directly or view it on GitHub.