Enhanced type-safety #584
Replies: 3 comments
-
I really like this. One thing to keep in mind with wrapping the IDs is that they could potentially be a UUID, or some custom String. Like our IDs look like Being able to type the URLs is awesome! I know that would solve a lot of issues we have in our rails app currently. We deal with a lot of really bad data, so constantly having to make sure things look right before using them is a huge pain. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to use a non int32 type today for the id field? Totally fine with monkey patching for the time being if a solution exists |
Beta Was this translation helpful? Give feedback.
-
@richrines sure, and it could be a suitable use-case, for example, if you grab from many source (ETL engine for example) |
Beta Was this translation helpful? Give feedback.
-
This is a work in progress document with some rough ideas. It might not be implemented until after 1.0, depending on time. I want to get it out there though in case people are interested or have ideas
Custom types for IDs in models
Instead of returning an
Int
for the model id, it would return a class that wraps the integer:This will enable better error messages and preventing more bugs because LuckyRecord can restrict querying to the correct types. This will be especially valuable when paired with better types in Lucky routing
Routing
Once ids are wrapped in types we can do cool stuff like this:
Of course, Lucky would also be able to infer this route and the types automatically:
You can override type restrictions when inferring:
Wrapper types for simple values in models
This will create a wrapper class called
User::Name
that wraps the string. Use it like this:Why do this?
It allows for more specific type signatures, which prevent errors and help make it incredibly easy to figure out what the method expects:
The wrapper types implement
to_s
that just delegates to the wrapped value. So:That way it is easy to work with in HTML pages and when interpolating into strings
Allow wrapping return value of any method
It might be helpful to allow wrapping values from any class.
More to come
I'll add more ideas here as they come. Feel free to comment with ideas for enhancing type-safety in other ways.
Beta Was this translation helpful? Give feedback.
All reactions