Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}
share|improve this question
6  
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. –  Leonid Shifrin Feb 19 at 2:00
4  
@LeonidShifrin I think you should post that as an answer. I doubt we can get a more insightful one. –  belisarius Feb 19 at 2:29
    
@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. –  Leonid Shifrin Feb 19 at 2:33
    
@LeonidShifrin Ok,just bookmark it for later if nothing better comes in. We should keep out unanswered pyre low :) –  belisarius Feb 19 at 2:35
1  
@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. –  Ajasja Feb 19 at 9:56

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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