-
Notifications
You must be signed in to change notification settings - Fork 4
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
Feature request: multiplication syntax #73
Comments
This proposal is really interesting! I believe this feature will inspire library developers as you say. I have never thought of such a feature, but it is in line with the original purpose of Silverchain (= reducing developers' effort). However, two problems need to be solved before starting its implementation: Syntax: While I like the parameter/expression multiplication feature, it was a little hard for me to understand their notation. I don't like to disagree without an alternative, but I haven't come up with a concrete idea yet. (I am thinking of using Current code quality: To be honest, the code quality is not good (not easy to safely extend 😱). I assume that more features will be proposed/implemented in the future so that developers can quickly create rich fluent APIs. To support those features smoothly, I want to refactor the code first. So, I'd like to continue looking for a better syntax of the multiplication feature for a while (but not so long), while refactoring Silverchain. How about this plan? If the priority of the multiplication feature is high, I will add ad-hoc implementation quickly :) |
Thanks a lot! I'm glad that you like the idea. 😃
Sounds great! Compared to the other features so far, multiplication is not a priority. It certainly doesn't require rushing things (maybe nothing ever does, TBH). Feel free to concentrate on refactoring and other improvements first!
I think multiplication is the most complicated feature so far, so it's fitting that it is tough to get the syntax right: it needs to be flexible, but still very easy to understand. A for-loop-like approach (or relying on keywords in general) could really help with that. I will think about this for a while and post my thoughts. (To avoid ambiguities, I'll probably include examples which consist of proposed AG syntax together with current AG code that achieves the same effect.) |
I think I have come up with a syntax and specification that could work. To explain it, I have split it into two distinct features. Feature 1: MultiplicationThis feature does not "loop" through anything, it just duplicates elements in the AG and changes how the action class is invoked. Here is the pseudo grammar: <parameter-or-method> "multiply" <number_literal-or-variable>
Feature 2: Template BlocksWhile the multiplication feature above already saves boilerplate on its own, it gets even better when used together with this feature. Template Blocks could be used on their own without multiplication, but I don't have an example for that, so I'll show the combination of both features. However, it's important to note that Template Blocks do not change anything regarding the action method calls and can be thought of to operate purely as a preprocessor. Here's the pseudo grammar: "for" "(" <variable> ":" <range> ")" "{" <template> "}"
Design Notes
Future PossibilitiesAn early indication that the syntax proposed above might be a good choice is that several other features (which I would consider outside the scope of a "minimum viable product" of this feature) seem to fit nicely. Disclaimer: most examples below are totally made up, so don't reject a feature just because its example usage feels unrealistic or uncommon.
|
Part 1 of 4
Sometimes, a fluent API has methods where the number of parameters of a method matches those of a another method called earlier in the chain:
In the action class, one can then direct the overloads of each method to the same private varargs method:
Note: with its 2 to 3 parameters, this example is a shortened version of my original use case, an API that offers overloads with 2 to 8 parameters.
While the pattern works fine, it is quite repetitious both inside the AG and in Java, and the average Silverchain user will probably not come up with it on their own.
Obviously, Silverchain could offer a more compact way to achieve the same result. I imagine a parameter multiplication syntax which may look roughly as follows:
(Note the use of Unicode
×
instead of ASCII*
. Although the parser could probably use*
as well and distinguish the two meanings, I fear that humans might have trouble if it did.)Silverchain would create the same chain interfaces/classes as in the manual version above, but all overloads of each method (
properties()
orwithValues()
) would use the same varargs method in the action class:The beauty of this is obviously that I can change the
$N=[2,3]
to$N=[2,8]
or even$N=[2,20]
without having to add any boilerplate AG or Java code - it all stays the same.Part 2 of 4
While writing this, I suddenly thought "why limit this feature to method calls that each have N parameters? Why don't we also allow N successive method calls?" Granted, I don't have a real-world use case for this (my
AggregateBuilder
quite intentionally always works via a two-method chain) - but maybe it is something that's worth pursuing.So we could generalize the idea of parameter multiplication syntax to expression multiplication syntax, and also allow using it for method calls as follows:
Part 3 of 4
If you didn't guess it yet, I see no reason why Silverchain should restrict each chain to have exactly one multiplier. Maybe there are use cases for having a chain with
$I=[1,6] $J=[2,3]
.Part 4 of 4
The variants from Part 1 and 2 (multiplying parameters & method calls) could even be mixed in the same chain: when
setColumns()
was called with N parameters, one has to callsetCell()
N times.Finals
So, what do you think about this, do you like it? Does it look useful to you, as well? And maybe the most important question: can all this be implemented (and with reasonable effort)?
I'm the first to admit that especially Parts 2 to 4 look a bit over the top. But then again, Silverchain offering powerful features like this may be exactly the thing that inspires developers to even attempt to create much richer fluent APIs than usual, with less effort.
The text was updated successfully, but these errors were encountered: