Poor Man's Type Checking in Arguments and Returns#35
Poor Man's Type Checking in Arguments and Returns#35
Conversation
14fcfb8 to
b1a771c
Compare
|
Not a review, but to answer some of your questions:
My idea originally was for it to be opt-in, since it has a runtime performance hit (not sure how much, but it's doing more work than before), and not everyone wants/likes strong typing.
I thought you'd do a |
9df0a6e to
29b670f
Compare
891a454 to
72440b9
Compare
|
@iovis WDYT? |
There was a problem hiding this comment.
Hey Julio!!
It took me some time but I finally did it.
To answer your questions. I prefer checking for :each. It's more ruby-ish than chekcing for Enumerable. So I'd keep it as you did in the lastest changes.
I'd keep the type checking optional. It would be a bit unnatural for me to use a library in ruby that forces me to use types. Also agree with iovis comment about the impact on performance, it would be something to double check if types become a non-optional thing.
Finally, I'm thinking of a use case that might be needed: what If I need the argument to be of different types?
class MyClass
include Injectable
argument :user, type: [User, AdminUser]
endI guess the answer would be to add two arguments and a validation to check that at least one exist:
class MyClass
include Injectable
argument :user, type: User, default: nil
argument :admin_user, type: AdminUser, default nil
validate :user_exists
endMaybe something to add to the readme if you don't want to implement this use case.
It looks good to me. Left a couple of comments about naming, but they are not blocking.
I completely forgot about this till I got the notification from @aalbagarcia's comment, lol. I haven't checked the new changes yet, but I remember seeing this project over the holidays: https://github.com/low-rb/low_type I haven't checked what kind of black magic they're doing behind the scenes yet, but the syntax they use for defining types was much closer to the "ideal DSL" I had in mind when I thought of this feature. I wonder if we can borrow some ideas from their implementation! |
|
Hey @aalbagarcia and @iovis thanks for the comments. I will:
Cheers! |
b9b32d9 to
9ae35a4
Compare
argument :jarl, type: [String, Symbol] call(jarl: 'fistro') # => ok call(jarl: :fistro) # => ok
dce2c42 to
a60f930
Compare
Please, disregard the branch name 😮💨
Changes
Add optional type checking to
argumentdeclarationsAdd optional type checking to
returnedvaluesNotes
Made them optional, since they're not the main purpose of the library, but happy to cut a 3.0.0 branch with breaking changes.
See each commit's README entries for more examples
About Collection validations
I've got a bit of a mess there. Not really sure I should check for Enumerable, or just
responds_to?(:each)is enough?Maybe having an optional
enumerates_with:key is cool to indicate which method is used to iterate the collection (default:each)?