Recently I had the need to redefine a certain symbol in my init.m so it would be automatically omitted from any lists it appears in. I decided to redefine it to an empty sequence, e.g. Locked = Sequence[]
, but that got me thinking. What if I wanted to return a sequence (not necessarily an empty one) in a :=
definition? Return
doesn't have the SequenceHold
attribute, and adding it in a package might cause problems, so what would I do?
EDIT: I think I've figured out what exactly causes me to have the problem. I've defined it to display a Message
first to let me know whenever a package I'm importing attempts to "attack my computer". (It is trying to cause my computer to behave in a manner not consistent with my wishes, after all.) So I defined it as Locked := (Message[Locked::nope]; Sequence[])
, but strangely it just returns Null. (It doesn't show a return value, but if I do {Locked}
, it returns {Null}
, and if I try to set it as an attribute it says that Null is not a valid attribute and doesn't set any of them.)
Locked
is itselfLocked
in version 9, precisely to prevent people doing what you are trying to do here, which is the oldest trick in the book. ;) About the question, though:Return
is rarely needed and you can anyway return results wrapped in any head that is eitherSequenceHold
orHoldAllComplete
to achieve what you want. I'd suggestUnevaluated
as a reasonable choice, useful also for its habit of disappearing. $\endgroup$Locked
is locked in Mathematica 9, which is precisely why I keep a copy of Mathematica 8 installed on one computer. I believe I've tried using Unevaluated. Do you mean something likeReturn[Unevaluated[Sequence[]]]
? If not, could you please give an example of what you do mean? By the way, take a guess what I used to prevent packages from erasing my definition of Locked. ;-) $\endgroup$Return
. It's really only necessary when you want to bail out of a construct likeWhile
that ordinarily wouldn't have any return value, or which wouldn't terminate except in the case of a manual return. If you encounter such a situation thenReturn@Unevaluated@Sequence[...]
is indeed fine (though you might need the second argument ofReturn
). Otherwise, just useUnevaluated@Sequence[...]
in place of the usual return value (typically the last position of constructs such asCompoundExpression
orModule
). $\endgroup$Unevaluated@Sequence[]
in this case? Incidentally if you consider the use ofLocked
to be malicious then I might suggest that the best course of action is not to use such packages in the first place. We have discussed this issue here before and the community concensus was that it harms us all to openly discuss how to circumventLocked
orEncode
. Given the way this discussion is going, I would suggest removing that context or otherwise the question is likely to be deleted. $\endgroup$Locked := (Message[Locked::nope]; Sequence[])
, as as OleksandrR. mentioned,Locked
isLocked
, but note that in your codeSequence[]
occurs insideCompoundExpression
, which does not haveSequenceHold
, so it is immediately removed, which is probably also not what you intended. $\endgroup$