I have a boolean variable that I’m initializing like so (define *chain-welded* #f)
. Later I do (set! **chain-welded** #t)
but I get this error: set!: assignment disallowed;
cannot modify a constant
constant: *chain-welded*
I’m guessing chain-welded is “becoming” the constant #f and cannot be reassigned. How would I track a boolean value if that’s the case? I may be asking the wrong question, if so, I can post my code (99 lines) to http://pasterack.org\|pasterack.org.
@joshua.e.cottrell you can’t use set!
on a definition from a different module
if you need to let other people change *chain-welded*
, I would either make it a box
or provide a function set-chain-welded!
from that module that does the set!
.
Ok, maybe I’m unclear on what comprises a module. I’m just using one file in Dr Racket. Line 20 defines *chain-welded*
and later in a procedure I try to change it. Are those considered two different modules?
Does the top of your file start with #lang racket
or #lang racket/base
?
#lang racket
Maybe posting your code is the best option
Is the problem that I’m “playing” the game in the interactions area of Dr Racket when the definition sets chain-welded?
Yes, that’s the problem
the module is the code in the definitions area, repl code is not part of the module
The set!
is actually inside a macro, so the compiler thinks that defintion is immutable until you end up changing later in a macro expansion.
and that expansion happens in the interactions window
so it isn’t in the module
Ah, very helpful clarification - the macro expanding in the interactions window
This ends up being a little confusing in exactly the case you’ve run into. What you probably want is to define a function that changes chain-welded
and expand to calling that function.
Do I fix that by providing the procedures that can be called from the interactions window?
(oh I see what happened now, nevermind my earlier comment)
Or I could compile it and play it at the terminal?
No, you don’t have to provide them. Add a function (define (set-chain-welded! v) (set! *chain-welded* v))
and change line 85 to use it.
You probably have to do something similar for *bucket-filled*
Perfect! And it makes sense. Thanks for the explanation too.
:slightly_smiling_face:
And I’m back in the same situation if I create a macro to build both my set-chain-welded! and set-bucket-filled! procedures, right?
No, that’s fine as long as you use the macro inside the module
The rule is “all set!
s must be inside the module that defines the name, and you can’t use macros to cheat”
Very good @samth, maybe I’ll try that after I get the first solution ironed out. Thanks again!