Le 16/10/2021 à 10:47, Robert Kubosz a écrit :
I want to make a copy of a variable defined in a separate file, but
run in the same current-module.
The copy I make is a copy-by-reference, and in result any
modifications I apply to the copy also appear in the original.
How do I make a copy-by-value in a current-module?
More detailed example is attached to this email.
Thanks in advance!
Robert
Hi,
module-ref and module-set! are useful for dealing
with variables of which the name is not known in
advance. Your code
#(let* ((copy-of-music (module-ref (current-module) 'trumpet-first))
(transposed-music (apply transpose `(,@(event-chord-pitches
#{c' des'#}) ,copy-of-music))))
(module-set! (current-module) 'trumpet-second transposed-music))
is strictly equivalent to
#(define trumpet-second
(let* ((copy-of-music trumpet-first))
(apply transpose `(,@(event-chord-pitches #{c' des'#})
,copy-of-music))))
where it may become clear to you that module-ref
does not do any kind of copy at all (I don't know
what you mean by “copy by reference” exactly -- isn't
passing by reference the contrary of a copy in languages
that have this concept?).
The method for making a copy of a value all depends on
what kind of object this is. For music, LilyPond provides
the function ly:music-deep-copy (which works recursively,
as its name suggests).
#(define trumpet-second
(let* ((copy-of-music (ly:music-deep-copy trumpet-first)))
(apply transpose `(,@(music-pitches #{c' des'#}) ,copy-of-music))))
Note that it's a little strange to apply a function
intended for chords to sequential music, so the
above has music-pitches instead of event-chord-pitches.
It assumes that the #{ c' des' #} part comes from
something programmatically, since otherwise you do
not need to bother and can just do
(transpose #{ c' #} #{ des' #} copy-of-music)
Hope that helps,
Jean