Le dimanche 02 juillet 2023 à 14:28 +0200, Volodymyr Prokopyuk a écrit :

> I wonder if there is a **way to define a music function with default 
> parameters** and be able to selectively specify some parameters using the 
> **parameters names** leaving all other parameters with their default values? 
> Probably it is asking too much :)



It is indeed asking too much from music functions (as in many languages where 
functions are called without parentheses or some other delimiter). There are a 
couple of ways around this restriction, though. One would be to just specify 
your arguments in a Scheme alist, like


```
shapedSlur =
#(define-music-function (params item) (alist? ly:music?)
   (let* ((dir (assoc-get 'dir params))
          (bs (assoc-get 'bs params 2.0))
          ...)
     (tweak 'control-points `(...) item)))
```

Another one is to define a Scheme function. If you use `define` instead of 
`define*`, it will recognize special objects in its argument list (called 
"keywords", written with "#:") which define named parameters.

```
#(define* (shaped-slur dir #:key (bs 2.0)
                                 (sh 0.5)
                                 (wd 1.0)
                                 (ht 1.0)
                                 (dt 0.0))
   (let ((control-points ...))
     #{ \tweak control-points #control-points \etc #}))

{
  c'$(shaped-slur 'your-dir #:bs 3.0 #:sh 0.1)( c')
}
```

Finally, you could abuse context mods a bit ("context mods" is the technical 
name for \with blocks), like this:

```
shapedSlur =
#(define-music-function (params item) (ly:context-mod? ly:music?)
   (let* ((properties (filter-map (match-lambda (('assign prop val) (cons prop 
val))
                                                (else #f))
                                  (ly:get-context-mods params)))
          (dir (assoc-get 'dir properties))
          (bs (assoc-get 'bs properties 2.0))
          ...
          (control-points `(...)))
     (tweak 'control-points control-points item)))

{
  c'\shapedSlur \with { dir=#'foo bs=5.0 } ( c')
}
```

Best,

Jean

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to