本页尚未翻译,因此以英文显示。 参与翻译

pipe

Runs a value through a list of functions, left to right — dynamically typed.

dynamic pipe(dynamic a, List<Function> fns) dynamic Function(dynamic a) pipeLazy(List<Function> fns)

Lecture

In FxTS, pipe(x, f, g, h) is a curried, fully-typed pipeline: TypeScript has enough overloads and generics tricks to infer the type flowing out of each step. Dart cannot do this. There is no variadic-generic overload trick available, so FxDart's pipe is honest about the trade-off: it takes a plain List<Function> and threads a dynamic value through each one in order. Each function receives whatever the previous one returned, with no static type checking in between.

That means pipe still works, and still reads nicely for a short, throwaway transformation — but a step that expects the wrong type will only fail at run time, and the whole pipeline's result type is just dynamic. If a step in the list returns a Future, pipe automatically awaits it before feeding the value to the next step, so sync and async functions can sit in the same list.

For anything you'll keep and maintain, prefer the fx() chain instead: fx(x).map(f).filter(g) is fully typed, gets autocomplete, and catches mismatched types at compile time — it's the typed alternative to exactly this kind of pipeline. Reach for pipe when the steps are dynamic by nature (e.g. built from a runtime list of functions) or when you're prototyping quickly. pipeLazy is the same idea, deferred: it returns a function you can call later instead of running immediately.

Demo 1 · Basics

A short pipeline built from FxDart's data-first functions:

Demo 2 · The honest downside

A step that expects the wrong type compiles fine and only blows up when it actually runs — this is exactly what fx() chains are designed to prevent:

Try it yourself

Exercise: pipe a list of numbers through a filter step and a sum step.

Related: fx — the typed chain alternative · pipe1 — a single pipe step, sync/async aware · toList — common final step in a pipe