lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Copy variable by value in a current-module.


From: Jean Abou Samra
Subject: Re: Copy variable by value in a current-module.
Date: Sat, 16 Oct 2021 11:18:53 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.2

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]