-
Notifications
You must be signed in to change notification settings - Fork 110
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
Optional Hackpipes proposal #198
Comments
This seems much more complex than something like: x |> (^ == null ? no(^) : yes(^)) |
I could see a use case for bailing out of pipeline early. // pseudo-syntax
let value = a()
|> ^ == null ? BAIL_KEYWORD : b(^)
|> c(^) I don't know if anything like that exists in other languages around pipelines or similar constructs. |
The closest we have to a "bail-out option" is optional-chaining itself, where null/undefined serve that purpose and short-circuit the rest of the property/method chain. With optional-chaining existing for property access (both dot and bracket) and function calls, it makes sense to me to spread that syntax to pipes, so that a null/undefined topic would immediately bail on the rest of the pipeline and just return null/undefined, identical to The new thing introduced by this thread is the idea of a pipe operator that just skips a single pipeline step if the topic is null/undefined and continues to the next. I'm moderately opposed to this, I think:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This entire side thread is off-topic. Please keep your comments in this issue to commenting on the issue. |
Quick question: Would that inline the topic twice, or assign it to an engine-internal temp variable? Assuming the latter? |
@sdegutis that'd be up to the engine needs unobservable by the user. |
I just realized that the |
The semantics of it are pretty important. If topic is simply expanded, and the computation is expensive (e.g. an API call), then inlining topic multiple times would be a serious problem and a show-stopper in many cases. If it's necessary to guarantee single execution of LHS regardless of topic instances in RHS, this should be in the spec. |
@sdegutis obviously it wouldn’t be permitted to run any computation more than once, in any scenario. It’s not a macro. |
Got it, thanks. To me it wasn't exactly obvious, so clarification was appreciated. |
Yeah, the LHS is evaluated, and the result is then bound to the placeholder. The explainer does indeed not make this quite clear; I'll fix. The proposed spec text is clear, tho. |
This is a good proposal in at least being able to short-circuit a pipeline which returns null or undefined at some point. I have been using I'll add one thing, since you are short-circuiting primarily on const A = x
|?> f
|?> g
|?> h
|?> i //short-circuit on undefined
const B = x
|.> f
|.> g
|.> h
|.> i //short-circuit on null
const C = x
|?.> f
|?.> g
|?.> h
|?.> i //short-circuit on undefined/null
const E = (x
|.> f
|.> g
|.> h
|.> i) || "foo" //short-circuit on null returning a default
const F = x
|> f
|.> g
|.> h
|.> i
|> j
|> k //interleave short-circuit and normal pipelines
const G = x
|> f
|.> g
|.> h
|.> i
|> either(?, "foo") //given interleaving, return a default of one's choosing Where a short-circuit pipeline meets a normal, it just passes the value thus far (null/undefined) to the next operation in the pipeline. As example I don't care what the characters are, just that the option for short-circuiting exists on null/undefined. It's a totally sensible use case and one I use regularly. I know that the overall proposal is stuck and has been for years. So this is more of an add-on, which I hope will be contemplated, since knowing what's potentially next can help direct the current design. |
The optional hackpipes proposal
|?>
caught my eye.Instead of using
|?>
in any subsequent pipe in order to take care of undefined branch I would propose two constructs:The rules
Expresion would execute only if value us not undefined. If the input value is undefined It would skip the expression and continue to the next pipe.
The second one would abort pipe if undefined value is passed thus leaving the final value of the composed structure undefined.
example:
the above could be extended to get more granular control over pipes
The text was updated successfully, but these errors were encountered: