How to make parameter "body" of function optional without causing error?

Hello everyone,

I have a problem with a typst function i want to declare where the behavior of said function vary depending on the body :

#let bis(n: 2, body) = { // note the argument body
  if body == none {
    get_number(n)
  } else {
   // code
  }
}

Such code lead to this kind of error

#bis()[
Some text \
Some text2 \
] // -> ok

Some text #bis() // -> missing argument body
Some text #bis()[] // -> ok

When changing the code to have optional argument for body such as :

#let bis(n: 2, body : none) = { // note the argument body
  if body == none {
    get_number(n)
  } else {
   // code
  }
}

I get this behavior :
Such code lead to this kind of error

#bis()[
Some text \
Some text2
] // -> unexpected argument

Some text #bis() // -> ok
Some text #bis()[] // -> unexpected argument

I want to be able to run this kind of code with no error :

#bis()[
Some text \
Some text2 \
] // -> ok

Some text #bis() // -> ok

How can i change the bis function to get what i want.
Thanks in advance for your answer

You need to use an argument sink:

#let bis(n: 2, ..args) = {
  // check args.pos() here
  // (maybe also args.named() to make sure the caller didn't pass an invalid argument)
}
3 Likes