Hey,
I’d like to make a function with two mutually exclusive arguments, like the slice method on arrays, which takes begin and end or count as arguments. I’d like to make a function to define two boundary dates with either two datetime or a datetime and a duration. The last datetime would be mutually exclusive with the duration.
Is there a built-in way to define mutually exclusive arguments ?
Thanks,
In your case you always have two parameters, and since Typst doesn’t have static typing anyway, I’d just use a parameter that can accept both:
#let my-function(begin, end-or-duration) = {
let (end, duration) = if type(end-or-duration) == datetime {
(end-or-duration, end-or-duration - begin)
} else {
(begin + end-or-duration, end-or-duration)
}
// ...
}
… or something like that, I didn’t check closely for syntax errors
computing both “variants” of the second parameter is of course only a suggestion.
If you had a variable number of parameters, I’d recommend ..args
for those. You’d likewise use some if
s inside the function to normalize the parameters, but the checking would probably get a bit more complex.
1 Like
Thanks, that’s a fairly simple solution !