22
$\begingroup$

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)?

$\endgroup$
1

2 Answers 2

23
$\begingroup$

Yes, at least in one place.

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

All right there, but

y := {1, 2, 3}    
y[[2]] = 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.

$\endgroup$
13
  • 1
    $\begingroup$ I knew you were coming @LeonidShifrin $\endgroup$
    – Rojo
    Commented Jun 2, 2012 at 18:25
  • 1
    $\begingroup$ That's really interesting. Are there any other cases where there's a difference, esp. one involving DownValues? The obvious idea x[1]={1,2,3};y[1]:={1,2,3} doesn't work because x[1][[2]] already cannot be assigned. $\endgroup$
    – celtschk
    Commented Jun 2, 2012 at 19:00
  • 3
    $\begingroup$ @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. $\endgroup$ Commented Jun 2, 2012 at 20:19
  • 3
    $\begingroup$ @celtschk Not so simple: Internal`InheritedBlock[{RuleDelayed}, SetAttributes[RuleDelayed, HoldAllComplete]; Hold[f[1]] /. 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. $\endgroup$ Commented Jun 2, 2012 at 20:48
  • 2
    $\begingroup$ I was wondering how this information (:= vs =) is transferred to subkernels when doing parallel computations. AFAIK definitions are transferred using the (assignable) Language`ExtendedFullDefinitions 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. $\endgroup$
    – Szabolcs
    Commented Jun 4, 2012 at 9:14
6
$\begingroup$

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[2]/.a_Integer-> RuleCondition[a*2]
Hold[2]/.a_Integer:>  RuleCondition[a*2]
Hold[2 2]
Hold[4]

Another difference

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

$\endgroup$
4
  • $\begingroup$ This had to be said, nice catch +1 $\endgroup$
    – Rojo
    Commented Nov 5, 2014 at 15:43
  • $\begingroup$ @Rojo thanks again, I fixed the mistake I mentioned now :). $\endgroup$ Commented Nov 5, 2014 at 17:54
  • $\begingroup$ I think that one problem may be that tag[[1]]=list with lots of parts is not the same as tag[[1]]=list with one element; one part. I have found that the complaint about tag[[1]]=one part can be eliminated by indexing tag[1]=one part. $\endgroup$
    – Carl
    Commented Jul 25, 2021 at 2:29
  • $\begingroup$ Hey Carl, I'm sorry I don't quite understand your comment in the context of this question. Was it meant as a comment on the other answer by Rojo in this Q&A? Alternatively, I was thinking it seemed like you were maybe referring to this answer. $\endgroup$ Commented Jul 26, 2021 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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