-
Notifications
You must be signed in to change notification settings - Fork 108
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
Letk destructuring of an optional renamed key with schema validation #145
Comments
I don't remember if these are supported currently, I'm not sure if I thought of these combinations (I mostly thought of "renaming" as a way to bind an entire map). I think schema + renaming would be easy to support if we don't already. For renaming + fallback, what would you expect the behavior to be? Would the fallback also be used for the other bindings? i.e. does |
Regarding renaming + fallback having sibling bindings as fallback, I'd reproduce the same behaviour as Example 1 : this works because (let [a 1, b (inc a)] b)
=> 2 Example 2 : this doesn't work because (let [a (inc b), b 2] b)
=> Syntax error compiling. Unable to resolve symbol `b` in this context I'm not sure to understand your syntax proposal though. (letk [ [[:bar :or 777 :as baz]] {:foo 42}] baz)
=> 777 Using (letk [[foo :- schema.core/Number :as bar] {:foo 42}] bar)
=> 42 schema + renaming + fallback (letk [[:bar :- schema.core/Number :or 777 :as baz] {:foo 42}] baz)
=> 777 Please note I'm just brainstorming and I'm happy to see alternatives / what you have in mind. But IMHO, deprecating the With plain vectors, since you don't have any constraints on the vector's size (and can have an odd number of items in the vector) you could fairly easily pick any optional Examples for all combinations : |
Thanks for the detailed response. I'll need to think about these suggestions a bit. Off the top of my head, the first thing I notice is that in |
Good shout. I understand (and had not properly appreciated, I admit) that the nested vector syntax was first designed for sub-maps. It's great, and my suggestions above are somehow equivalent to extending these features (initially for sub-maps) to the top level maps too, in order to enable new use cases. Regarding Alternatively, the syntax could bind a symbol in first position (when there is no renaming) (example : Again, this is just brainstorming |
Thanks, happy to continue brainstorming. I personally think it's a smell to ever bind anything that isn't a symbol in the binding map (e.g., binding to the symbol form of a keyword). The potential for confusion when accidentally shadowing an existing binding or something is just too high. I also think the asymmetry between |
On further reflection, I think the best route might be:
|
Hi,
I've read the docs here : https://github.com/plumatic/plumbing/tree/master/src/plumbing/fnk#fnk-syntax
and here : http://plumatic.github.io/plumbing/plumbing.fnk.schema.html
and I don't understand how to go with some destructuring scenarios.
Basically, there is "vanilla" destructuring (just bind a symbol to a key in a map) + 3 main extra features being :
And I struggle to combine these extra features.
Scenario A : vanilla => destructuring a plain map
Result : ✅
Scenario B : renaming only => destructure the key named
:foo
and bind tobar
Result : ✅
Scenario C : fallback only => destructure a key named
:bar
and fallback to0
when the key is missingResult : ✅
Scenario D : schema only => destructure a key named
:foo
and add a schema hintResult : ✅
Alright, using at most 1 extra feature worked.
Now let's try by combining 2 or 3 extra features.
Scenario E : schema + fallback => destructure a type-hinted missing key named
:bar
and fallback to0
Result : ✅
Where I'm stuck
However, I haven't found a way to combine :
Are these combinations supported ? If not, what would it take to support them ?
PS : Classic
let
bindings support renaming + fallback, which I use often, hence the current issue :The text was updated successfully, but these errors were encountered: