Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic Integer type #17

Open
rwl opened this issue Apr 12, 2016 · 8 comments
Open

Add generic Integer type #17

rwl opened this issue Apr 12, 2016 · 8 comments

Comments

@rwl
Copy link

rwl commented Apr 12, 2016

I encounter an error when using a generic type to index a slice or as the len argument to make

non-integer len argument in make([]IntType) - IntType
non-integer slice index n

My workaround has been to add a generic.Integer type, in addition to the existing generic.Number type. An alternative might be to change the generic.Number definition to:

type Number int

Another alternative is to add a // +build ignore tag to the template file, but then I think it can't be used for generic testing.

@FlorianUekermann
Copy link

I would like to see "Integer" added instead changing Number to int.

Integers support more operators than floats, which means "type Integer int" is necessary if you want to use those operators.

Similar to how "type Type interface{}" is useful for indicating and ensuring support for a less specific set of types than "type Number float64", the latter is useful for indicating and ensuring support for integers and floats.

@pdrum
Copy link
Collaborator

pdrum commented Sep 3, 2018

I don't understand what generic.Integer is good for. As far as I understand this type can only be replaced with int so how is it different with just using int instead of sth like generic.Integer in templates? Will you elaborate please? @rwl

@rwl
Copy link
Author

rwl commented Sep 5, 2018

IIRC, I was trying to support generating different types of int e.g. int32, int64 etc. In the end I dropped genny and just used text/template directly.

@dmoles
Copy link

dmoles commented Mar 13, 2019

Something like this would be useful for generating code for enum-type constants.

@takeda
Copy link

takeda commented May 14, 2019

@pdrum generic.Number is currently an alias for float64, as mentioned in the ticket float64 can't be used as a slice index or len argument in make().

generic.Integer could be replaced with int, int8, int16, int32, int64, uint, uint8, unit16, uint32, uint64 I don't think it is any less meaningful than generic.Number, having said that I believe (I didn't test it) not using generic.* and just assigning the custom type to int would just work, so I'm thinking that creating generic.Integer would be just for completeness sake, since we already have generic.Number assigned to float64

@dmoles
Copy link

dmoles commented May 14, 2019

@takeda — So I just tried this, and naively assigning the custom type to int (or uint32 or what have you) gets you an extra type declaration that you may not want — type T generic.Type gets deleted, type T int gets transformed to type MyType int.

As a workaround, if what you want is for your templates to compile, behave well in your IDE, etc., you can put the type declarations in a separate file in the same directory. As far as I can tell this just works—which is interesting, because it suggests the whole generic.Type mechanism is unnecessary.

@takeda
Copy link

takeda commented May 14, 2019

Ah I see, I forgot that genny removes it. In that case I agree that Integer type should be included. The workaround you gave is still a workaround and generic.* is more elegant I wouldn't call it unnecessary.

@dmoles
Copy link

dmoles commented May 14, 2019

It does make the templates more readable and more obviously templates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants