3
$\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$
2
  • 2
    $\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$ May 13, 2020 at 1:00
  • $\begingroup$ @J.M. Thank your for your explanation! $\endgroup$
    – Louis Yu
    May 15, 2020 at 3:45

1 Answer 1

8
$\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$
1
  • $\begingroup$ Thanks for pointing that out! $\endgroup$
    – Louis Yu
    May 15, 2020 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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