# Does Set vs. SetDelayed have any effect after the definition was done?

I was always assuming that the only difference between Set (=) and SetDelayed (:=) is that SetDelayed holds the right argument, so that a := b is effectively the same as a = Unevaluated[b]. Especially I assumed that after the assignment is done, there's no further difference for variables or functions assigned with Set and variables assigned with SetDelayed. Looking at OwnValues resp. DownValues seems to support that assumption.

However I now noticed that when writing ?a, Mathematica displays the type of assignment used for the definition, which means it has to store it somewhere. And I somehow doubt that it only stores it in order to show it with ?.

Therefore my question is: Is there any difference in the behaviour of values assigned with = and with := (apart from the different output of ?), assuming the actual assigned expression is the same (i,e, OwnValues/DownValues have the same value after both assignments)?

• – Mr.Wizard Jun 2 '12 at 22:14

Yes, at least in one place.

x = {1, 2, 3}
x[] = 8;


All right there, but

y := {1, 2, 3}
y[] = 8


gives Set::noval: Symbol y in part assignment does not have an immediate value

Credit to this old comment by Leonid. Also note the point on memory usage:

[...] I'd guess that delayed definitions may use some intermediate internal variables, while immediate ones point straight to the memory where the data is stored.

• I knew you were coming @LeonidShifrin – Rojo Jun 2 '12 at 18:25
• That's really interesting. Are there any other cases where there's a difference, esp. one involving DownValues? The obvious idea x={1,2,3};y:={1,2,3} doesn't work because x[] already cannot be assigned. – celtschk Jun 2 '12 at 19:00
• @celtschk While this is not directly related, you may find this discussion also interesting. In particular, RuleDelayed inside DownValues is not totally inert (contrary to a popular belief, it does evaluate the r.h.s. of the rule, albeit in a special way). Consider ClearAll[f]; f[x_] := Unevaluated@Unevaluated[x];, and contrast DownValues[f] with Block[{RuleDelayed = HoldComplete}, DownValues[f]], for instance. – Leonid Shifrin Jun 2 '12 at 20:19
• @celtschk Not so simple: InternalInheritedBlock[{RuleDelayed}, SetAttributes[RuleDelayed, HoldAllComplete]; Hold[f] /. DownValues[f]]. As I said, the whole point is not that DownValues are not used (which is in some sense true but not really the cause here), but that RuleDelayed in DownValues evaluates it's r.h.s. and strips any number of Unevaluated wrappers. In the above code, I prevented that, thus the result. – Leonid Shifrin Jun 2 '12 at 20:48
• I was wondering how this information (:= vs =) is transferred to subkernels when doing parallel computations. AFAIK definitions are transferred using the (assignable) LanguageExtendedFullDefinitions function. It turns out that when using = we have OwnValues -> HoldPattern[x] :> {4, 5, 6} in the definition list while when using := we have OwnValues -> {HoldPattern[x] :> {4, 5, 6}}. Note that braces. Just an interesting bit to add and to confirm that this info is indeed transferred properly to subkernels. – Szabolcs Jun 4 '12 at 9:14

Here is another difference

test[a_] = Unevaluated[1 /; a];
test2[a_] := 1 /; a
test[True]
test2[True]

1/;True
1


I think this has to do with different handling of RuleDelayed and Rule. I actually found this difference between Set and SetDelayed after finding the following difference between those.

True/.a_->  2/;a
True/.a_:>  2/;a
True/.a_:>  Evaluate[2/;a]

2/;True
2
2


Silly note on syntax highlighting

Note that the syntax coloring, which does not connect the as in the case of Rule, can be misleading. Of course (in the case where a does not have a value) the symbols a do correspond and we do not simply replace by the symbol a, but rather by what a_ matches.

True/.a_->  !a,
True/.a_:>  !a

False (*of course this is the right output, and not !a*)
False


So this has nothing to do with the difference.

Root of the difference

The code

True/.a_:> !a


is evaluated using the undocumented function RuleCondition (WReach explains that function nicely here). This can be seen by looking at the trace

Trace[True/.a_:>2/;a]


But in

Trace[True/.a_->2/;a]


no RuleCondition appears. A simple replacement is made, where the pattern is simply replaced by the expression with head Condition. So no "special handling" occurs here and whether we replace or not does not depend on the condition.

Another difference between Rule and RuleDelayed

First I thought the difference was responsible for the difference between Set and SetDelayed. I felt it was worth mentioning anyway.

Hold/.a_Integer-> RuleCondition[a*2]
Hold/.a_Integer:>  RuleCondition[a*2]

Hold[2 2]
Hold


Another difference

Even another difference between Set and SetDelayed, also involving Condition, is given here

• This had to be said, nice catch +1 – Rojo Nov 5 '14 at 15:43
• @Rojo thanks again, I fixed the mistake I mentioned now :). – Jacob Akkerboom Nov 5 '14 at 17:54