kulshekhar / fungen

Replace boilerplate code with functional patterns using 'go generate'
MIT License
123 stars 8 forks source link

Functions for pointers #21

Closed slayer closed 8 years ago

slayer commented 8 years ago

Hi

I want to use pointers to my types, like:

//go:generate fungen -types *Node,*Edge

but got error:

2016/05/26 23:42:38 9:18: expected 'IDENT', found '*' (and 3 more errors)
server.go:4: running "fungen": exit status 1
kulshekhar commented 8 years ago

@slayer Did you try using

//go:generate fungen -types Node,Edge

This should work with pointers as well. For instance, if you define a struct like

type Node struct {}

you'll have the methods generated available on a variable of this type and on a pointer to a variable of this type.

kulshekhar commented 8 years ago

@slayer I'm closing this issue. If you think that I've understood your issue incorrectly or if this is still a problem for you, let me know and I'll open this and take a look at it again.

slayer commented 8 years ago

Hi Sorry for delay

Can you please check this example: https://play.golang.org/p/qoRDTTOyNM

Thanks

kulshekhar commented 8 years ago

@slayer Please take a look at the slightly modified version at https://play.golang.org/p/Y1ZiBH3EEi

Note that the generated methods don't operate on the members of a list directly. They always return a copy.

I can see how the name of the Each method can cause some confusion. This method is used when there's a requirement to perform some action for each member of the list, but not ON each member of the list. For example, you might want to loop through a list and print out a message for each of them.

If you want to modify or transform the members of a list in any way, you should use the Map of PMap method.

Does this help you?

kulshekhar commented 8 years ago

Reopening for now

slayer commented 8 years ago

Note that the generated methods don't operate on the members of a list directly. They always return a copy.

Sure, that is why I want a pointer version of functions.

My storage has a plurality of interconnected mutable objects (nodes and edges), and I need to go through they and change state. It is no problem with a pointer version unlike with a copy version Thanks

slayer commented 8 years ago

Currently I have done with a type PItem *Item, but it is a workaround

kulshekhar commented 8 years ago

I see your point. I'll take a closer look at it over the weekend and see how this use case can be best served.

kulshekhar commented 8 years ago

@slayer Could you please check if your original code works with these changes

kulshekhar commented 8 years ago

I've tested the latest changes with various combinations and this seems to be working now.

However, there is an edge case that's not handled - when generating for something like *Node,Node:N, there seems to be a problem.

@slayer If you can confirm that this satisfies your original requirements, I'll close this issue and open another one for the edge case.

slayer commented 8 years ago

Yes, it works fine. Thanks