On 2021-01-24 8:17 pm, Vaughan McAlley wrote:
Hi,

I am trying to write a function that will accept a symbol and a value, and
assign that value to the symbol *if the variable has not already been
defined *(such as in an include file).

At the moment, it runs without complaining, but the result is always "bar",
the default value. Can anyone spot the problem?

Did you mean to attach or include a code snippet? Hard to spot any problem without it. :)

Regardless, review the following:

%%%%
\version "2.22.0"

valueOrDefault =
#(define-scheme-function
  (module symbol default)
  ((module? (current-module)) symbol? scheme?)
  (module-ref module symbol default))

defineOnce =
#(define-void-function
  (module symbol value)
  ((module? (current-module)) symbol? scheme?)
  (or (module-defined? module symbol)
      (module-define! module symbol value)))

{
  \valueOrDefault foo { b'1 }
  \defineOnce foo { b'4 4 2 }
  \valueOrDefault foo { b'1 }
  \defineOnce foo { b'8 8 2. }
  \foo
}
%%%%

I included the optional module parameter in the event that users might need to work outside the current scope.


-- Aaron Hill

Reply via email to