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?

1 Answer 1

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

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.