2

I can use setq-default to set a symbol's default value, as follows:

(with-temp-buffer

  (make-local-variable 'bar)
  (setq-default bar "xyz")
  (symbol-value 'bar))

When I first executed the above code, I got an error:

Symbol’s value as variable is void: bar

But when I tried executing it again, I got the expected return value: "xyz".

Why did I got an error the first time but succeeded the second time?

Drew
  • 75,699
  • 9
  • 109
  • 225
Searene
  • 479
  • 2
  • 14

1 Answers1

2

Put the make-local-variable after the setq-default:

(with-temp-buffer
  (setq-default bar "xyz")
  (make-local-variable 'bar)
  (symbol-value 'bar))

setq-default sets the global value. Doing what you did says there is a local value for bar, but it doesn't set that local value. And neither does setq-default set that local value.

Doing it with what I wrote first sets the global value, then says that there's a local value. And the local value is just the same as the global value.

As the doc string of make-local-variable tells you, you can also use (set (make-local-variable 'bar "abc")). (But that doesn't set the global value.)

Drew
  • 75,699
  • 9
  • 109
  • 225