-
Notifications
You must be signed in to change notification settings - Fork 35
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 a syntax for on-the-fly coercion #68
Comments
See, I would create a RowID type that I can name, test, document and My main issues are that it encourages people to cram a lot of detail OTOH, I guess there's no reason a signature can't span multiple lines. OT3H, I believe people are more likely to write out the coercion long OT4H, I can't reference the instance object in a regular coercion. With that in mind, how are you doing to pull off |
But the coercion isn't possible in isolation, because it needs the row-data mapping stored in the object.
Oh, sure, I use multiple lines for sigs all the time. For instance, method billing_rate_id
(
Company::Company :$company,
Company::Property :$property,
Str :$query_date,
Str :$pick_nm = '0',
Str :$source_tp,
CodeRef :$source_picker
) which is actual production code.
'Tis a valid question. I suppose the |
That seems to me the way to do it. It will require embedding logic to check
|
Yes. I'm thinking something like the following. Currently our injected code looks something like so (pseudocode):
We could change that to:
which is arguably more sensible (although that's of dubious value since it's generated code), and I don't think it's any less efficient. That makes it pretty easy to generate something like:
Or, if it's not desireable to move the Something along those lines. |
In #65 @schwern proposes a coercion trait. This is good.
However, just as we have both type checking and on-demand typing via
where
, I think we could benefit from having a way to specify a coercion directly in the signature, which would apply only to that parameter. Let me provide an example of where this could be useful.I've been putting together some code to deal with simply formatted data in a Google Spreadsheets tab. I want to be able to access the data by column number (or letter) and row number, but I also want to be able to access the data by specifying a column header, or the "key" data of a row. I wrote the following method:
where
_row
is a method that turns a row's key value into the numeric row number. But what I really wanted to write was this:That seems cleaner and neater, and should be more obvious to anyone reading my code. The coercion neatly eliminates an entire line of parameter setup code, which is what signatures excel at. (In a way, this is sort of a poor man's multimethod dispatch.)
However, I would never create an entire subtype with coercion just for this single parameter. So on-the-fly coercion seems the rational way to go.
I'm curious to hear if others would find this useful.
The text was updated successfully, but these errors were encountered: