2
$\begingroup$

Mathematica 12.1 introduces Splice that replaces the old way of using Sequence@@ to "melt" a list of entries. However, the argument matching seems to be not working as intended.

Suppose we have data = {a, b} and the following polymorphic function definitions:

data = {a, b};

f[x_] = 1;
f[x_, y_] = 2;

Using Sequence@@ would result in the following:

f[Sequence @@ data]
(* 2 *)

But if we use Splice

f[Splice[data]]
(* 1 *)

Is this intentional or a Bug? If this is intentional, could anyone explain why such behavior is favored over the Sequence @@ method?

$\endgroup$
  • 1
    $\begingroup$ Using Sequence[] is of course the more general approach. But it is because of this generality that one might want things to only work in e.g. lists, and that's where Splice[] comes in. $\endgroup$ – J. M.'s ennui May 13 '20 at 1:00
  • $\begingroup$ @J.M. Thank your for your explanation! $\endgroup$ – L.Yu May 15 '20 at 3:45
7
$\begingroup$

The docs say that

Splice[{e1,e2,…}] is equivalent to Sequence[e1,e2,…] when the outer wrapping head is List.

In your example the outer wrapping head is f. In the Basic Examples section it gives one instance,

head[a, b, c, Splice[{1, 2, 3}], d, e]

which returns with the Splice unevaluated. So your f sees one argument, Splice[{a,b}], and returns 1 according to its definition.

$\endgroup$
  • $\begingroup$ Thanks for pointing that out! $\endgroup$ – L.Yu May 15 '20 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.