From 9407f8dfa42c6c3ccbd8277d4ef98cb45c35178e Mon Sep 17 00:00:00 2001 From: pgyorok Date: Tue, 15 Sep 2026 10:41:37 +0100 Subject: [PATCH] add example of using list/dict with per-element type check --- docs/basics/pattern.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/basics/pattern.md b/docs/basics/pattern.md index 03a12561..cfc8e444 100644 --- a/docs/basics/pattern.md +++ b/docs/basics/pattern.md @@ -241,6 +241,28 @@ q)((a;b):`F):3 4e ^ ``` +Combining type check with a list or dictionary pattern allows element-wise type checking: +```q +q)(a:`i;b:`j):(1i;2j) +q)(a:`i;b:`j):(1i;2h) +'type + [0] (a:`i;b:`j):(1i;2h) + ^ +q)([a:a:`i;b:b:`j]):([a:1i;b:2j]) +q)([a:a:`i;b:b:`j]):([a:1i;b:2h]) +'type + [0] ([a:a:`i;b:b:`j]):([a:1i;b:2h]) + ^ +``` +Note that the form ``([a:`i])`` is a dictionary pattern that matches the value at the key `a` to a symbol, so it doesn't contain a type check pattern. Additionally, using a null pattern in a type check for a dictionary element requires parentheses: +```q +q)([a:(:`i);b:(:`j)]):([a:1i;b:2j]) +q)([a:(:`i);b:(:`j)]):([a:1i;b:2h]) +'type + [0] ([a:(:`i);b:(:`j)]):([a:1i;b:2h]) + ^ +``` + ### Filter function The filter function pattern takes the form ```p:expr``` where `expr` is an expression that returns a callable (such as a lambda, projection or operator). The result of `expr` is called on the value from the assigned value, and the result is matched to `p`.