10
$\begingroup$

Why does $Failed have attribute HoldAll? Are there typical situations where $Failed is returned as a head that must not evaluate further, or is this just a relic from Mathematica's early times?

In[1]:= Attributes[$Failed] Out[1]= {HoldAll, Protected} 
$\endgroup$
7
  • 7
    $\begingroup$ It is not very common, but sometimes, $Failed is used as a head, like f[x___]:= $Failed[x]. This makes it possible to have "return code" returned, rather than just a fact of failure. Basically, when this is used, it is usually in the error-reporting fall-back rule. In some cases, one may want to not evaluate the arguments x (e.g. if f is Hold*). I don't use $Failed in such fashion myself, but I've seen such code, and I don't find any particular reason not to use it in this way. Of course, one could also use $Failed[Hold[x]], but arguably this is a clumsier method. $\endgroup$ Commented Feb 19, 2014 at 2:00
  • 4
    $\begingroup$ @LeonidShifrin I think you should post that as an answer. I doubt we can get a more insightful one. $\endgroup$ Commented Feb 19, 2014 at 2:29
  • $\begingroup$ @belisarius I'd wait for a while. There are much more knowledgable folks than me, who may shed some more light on this. There might be other reasons of which I am not aware. My previous comment has a status of an educated guess backed by some evidence, rather than an authoritative answer, which is why I posted it as a comment. $\endgroup$ Commented Feb 19, 2014 at 2:33
  • $\begingroup$ @LeonidShifrin Ok,just bookmark it for later if nothing better comes in. We should keep out unanswered pyre low :) $\endgroup$ Commented Feb 19, 2014 at 2:35
  • 1
    $\begingroup$ @LeonidShifrin Hehe, I remember that a lot of your posts end with "I will change/improve/revisit this soon":) That's why I agree with beli 100%. I would also recommend to post the comment as an answer. The hypothetical more knowledgable people can either post their own answer or improve your own. $\endgroup$ Commented Feb 19, 2014 at 9:56

1 Answer 1

8
$\begingroup$

It is not very common, but sometimes, $Failed is used as a head, like

f[x___]:= $Failed[x] 

This makes it possible to have "return code" returned, rather than just a fact of failure.

Basically, when this is used, it is usually in the error-reporting fall-back rule. In some cases, one may want to not evaluate the arguments x (e.g. if f is Hold*). I don't use $Failed in such fashion myself, but I've seen such code, and I don't find any particular reason not to use it in this way. Of course, one could also use $Failed[Hold[x]], but arguably this is a clumsier method.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.