Skip to content

Add a Generic Numeric TextModel#79

Open
kaycodes13 wants to merge 4 commits intosilksong-modding:mainfrom
kaycodes13:generic-numeric-textmodel
Open

Add a Generic Numeric TextModel#79
kaycodes13 wants to merge 4 commits intosilksong-modding:mainfrom
kaycodes13:generic-numeric-textmodel

Conversation

@kaycodes13
Copy link
Copy Markdown
Contributor

@kaycodes13 kaycodes13 commented Apr 29, 2026

Summary of Changes

I know that #76 is currently slated to add support for auto-generating elements for the missing numeric value types. But I had an idea for how to reduce the number of public API methods there are that are dedicated to just creating TextModels for numbers and I wanted to propose it.

This replaces TextModels.ForIntegers and ForFloats with a method ForNumbers<T> that works for any of the built-in numeric value types. (Edited): The idea is that we keep a dictionary of numeric value types and their TryParse functions, and if the type parameter is in the dictionary, it creates a parser function using the reflected TryParse and returns a model for that. When passed a non-numeric type, it throws an ArgumentException instead.

The ConfigEntryFactory methods GenerateIntegerElement and GenerateFloatElement have similarly been replaced with a method GenerateNumberElement that catches all numeric value types.

I've tested it with the automatic menu test and haven't encountered any problems with any of the types.

I understand if this gets canned for being too clever/code smelly, lol, but I wanted to offer it anyway.

(Also, version has not been bumped due to the many open PRs at the moment)

Checklist

  • No change is too small for a release, so pick one:
    • I have updated the package version following semantic versioning
    • There is another change actively WIP that will update the version (link issue or PR here)
    • This PR does not update user-facing code/config
    • I'm not sure how to set the version and would like the reviewer's help

In its current state, a breaking change, because it removes the equivalent methods for ints and floats
@flibber-hk
Copy link
Copy Markdown
Member

https://stackoverflow.com/questions/8171412/cannot-implicitly-convert-type-int-to-t/8171547#8171547

Any time you find yourself switching on a type in a generic you are almost certainly doing something wrong.

My feeling is that the advantages of this are probably not worth the code stinkiness

@kaycodes13
Copy link
Copy Markdown
Contributor Author

kaycodes13 commented Apr 29, 2026

The other option for doing the same thing but with slightly less switching is to use reflection.

Edit: I've now committed a version of the idea which uses reflection and a cache of delegates instead of the generic-switch.

@flibber-hk
Copy link
Copy Markdown
Member

I think I personally prefer the existing non-reflection approach - I don’t think doing it with reflection really provides much benefit on a code cleanliness perspective given that the functions involved are pretty simple and it’s only the int and float that matter for the vast majority of cases (given that they fit in the best with the Unity ecosystem) (and obviously reflection isn’t free - even if that doesn’t really matter here). I don’t mind being overruled if more people want it like this though.

Another alternative would be writing a source generator that generates the code for each numeric type, but for this specific use case that would be a lot more work than simply doing each numeric separately (not to mention it makes the code structure a bit worse as you can’t have all the code next to each other)

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

Successfully merging this pull request may close these issues.

2 participants