This is not an attempt to answer the question exactly as posed, because generally speaking I don't consider it a good idea to subvert Mathematica's evaluation process (e.g. by reaching up the stack and rewriting definitions based on their RHS before they evaluate) just to satisfy arbitrary syntactical preferences.
A better way, if you just want to stop Quiet
from evaluating when the definition is made and before the message is actually going to be produced, is to Block
it:
Block[{Quiet}, myFunction3[x_] = Quiet[1/x, Power::infy]] (* -> 1/x *)
myFunction3[0] (* -> ComplexInfinity [with no messages] *)
Something that works more similarly to what is described in the question, and still without requiring any awful hacks, is the following:
TurnedOffError /: HoldPattern[
lhs_ = TurnedOffError[rhs_, msg : _MessageName | {___MessageName} | PatternSequence[]]
] := lhs = Unevaluated@Quiet[rhs, msg];
myFunction3[x_] = TurnedOffError[1/x, Power::infy] (* -> 1/x *)
myFunction3[0] (* -> ComplexInfinity [with no messages] *)
But this, of course, only works when TurnedOffError
appears directly at level 1 inside Set
.
a=3;myfunction2[x_] = a + Unevaluated@Quiet[1/x]
will evaluatea
on assignment, but will not evaluate1/x
on calling the function, but instead give (when calling with argument0
) the result3+Unevaluated[Quiet[1/0]]
$\endgroup$