Why can I not loop over arguments in a function receiving multiple arguments?

Here is my function:

#let add-numbers(...nums) = {
  let sum = 0
  let num-list = (nums) // Convert arguments to a list
  for num in num-list {
    if num.type() == "int" or num.type() == "float" {
      sum = sum + num
    }
  }
  sum
}
 
// Test the function  
#add-numbers(1, 2, "hello", 3, 4, "world", 5) // Expected output: 15

Typst throws an error: "cannot loop over argument" at the for num in num-list line.

1 Like

You need to use nums.pos() to turn the positional arguments into an array, see the docs on the arguments type.

1 Like

Im addition to what @janekfleper said, you should compare your types not with strings. That is deprecated and will be removed very soon (almost was removed already), use type == float directly instead.

2 Likes