cheekybits / genny

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

How do I... basetypes? #13

Closed falafflepotatoe closed 8 years ago

falafflepotatoe commented 8 years ago

Hey. I'm having good success with genny but now I have a spazzy-issues.

Code:

func (arr *T) pop () BASETYPE { l := len(*arr)-1; val := (*arr)[l]; *arr = (*arr)[:l]; return val }

Question

One. is there any way to automatically deduce BASETYPE to int from T being []int? I tried:

func (arr *TSlice) pop () T { l := len(*arr)-1; val := (*arr)[l]; *arr = (*arr)[:l]; return val }

but after running go generate Tslice is named "intSlice" so I cannot export it, which is my next question: Two. I'm exporting my package so custom types have to be Uppercased, so the way I was building slices is no longer work. any suggestions on how to get "intSlice" to "IntSlice" if i generate with "int"? (so I can use it as as the base type)

edit: NVM I should just use a second var. I'm still kind of curious as to the 2nd question, or if theres a basetype() API for genny though. Cheers and thanks for the software, most simple generator I've seen for Go.

matryer commented 8 years ago

What about having T as int and use []T in your code when you need slices?

Sent from my iPhone

On 18 Sep 2015, at 16:07, falafflepotatoe notifications@github.com wrote:

Hey. I'm having good success with genny but now I have a spazzy-issues.

Code:

func pop(arr _T) BASETYPE { l := len(_arr)-1; val := (_arr)[l]; *arr = (_arr)[:l]; return val } Question

How can I set basetype to "int" when T is []int? so the return type is the base type... ex: return "int" when my code generates []int., float when T is []float etc I tried: func pop(arr TSlice) T { l := len(arr)-1; val := (arr)[l]; *arr = (arr)[:l]; return val }

but after running go generate Tslice is named "intSlice" so I cannot export it, which is my next question:

I'm exporting my package so custom types have to be Uppercased, so the way I was building slices is no longer work — Reply to this email directly or view it on GitHub.

falafflepotatoe commented 8 years ago

@matryer thats what I was doing initially, however for builtins... go does not allow you to use them as receivers in functions

ex: func (arr []int) pop () {...} is invalid, so I have to use a custom type IntSlice.

falafflepotatoe commented 8 years ago

Question for you guys! I want to release my generated .go file as a package but I want to include the src/template so they can add types and regenerate it. But the template should not be in the pkg (the code/varnames conflict as the T variables are not defined)

I guess you cannot change the extension of a file before using go generate. Any idea how to do this?

matryer commented 8 years ago

You can use build tags to prevent template files from being build as part of the package?

// +build ignore

On 9 Oct 2015, at 10:54, falafflepotatoe notifications@github.com wrote:

Question for you guys! I want to release my generated .go file as a package but I want to include the src/template so they can add types and regenerate it. But the template should not be in the pkg (the code/varnames conflict as the T variables are not defined)

Any idea how to do this?

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

falafflepotatoe commented 8 years ago

yea! thanks a lot sir.

falafflepotatoe commented 8 years ago

One last question: Im using two vars and getting 2x2x2x2 results instead of 2x2 (4) generated functions the vars are BASE_TYPE (=int,float) and SLICE (=IntSlice,FloatSlice) I dont need a type IntSlice []float, and a Floatslice []int

anyway to not get double the results?