platformer / typst-algorithms

MIT License
141 stars 4 forks source link

Build error: no body? (there is one) #11

Closed Proliecan closed 1 year ago

Proliecan commented 1 year ago
#let pseudocode(content:[], title: "", parameters: "", additionalKeywords: none, caption: none, _label: "") = {
  [
    #figure(
      align(left, 
        algo(
          body: content,
          title: title,
          parameters: parameters,
          keywords: _algo-default-keywords + ("assert", "clone") + additionalKeywords,
          comment-prefix: [#sym.triangle.stroked.r ],
          comment-styles: (fill: rgb(100%, 0%, 0%)),
        )
      ),
      caption: caption,
      supplement: [Pseudocode],
      kind: "pseudocode",    
    ) #label(_label)
  ]
  v(0.2cm)
}

fails to build when used as function.

error: missing argument: body
    ┌─ /assets/template/template.typ:400:12
    │  
400 │           algo(
    │ ╭─────────────^
401 │ │           body: content,
402 │ │           title: title,
403 │ │           parameters: parameters,
    · │
406 │ │           comment-styles: (fill: rgb(100%, 0%, 0%)),
407 │ │         )
    │ ╰─────────^
platformer commented 1 year ago

The reason this fails is because body has no default value in algo, and thus it must be passed as a positional argument:

#let pseudocode(content:[], title: "", parameters: (), additionalKeywords: none, caption: none, _label: "") = {
  [
    #figure(
      align(left, 
        algo(
          content, // <-- HERE
          title: title,
          parameters: parameters,
          keywords: _algo-default-keywords + ("assert", "clone") + additionalKeywords,
          comment-prefix: [#sym.triangle.stroked.r ],
          comment-styles: (fill: rgb(100%, 0%, 0%)),
        )
      ),
      caption: caption,
      supplement: [Pseudocode],
      kind: "pseudocode",    
    ) #label(_label)
  ]
  v(0.2cm)
}

This is just an oddity with the way Typst handles parameters in comparison to other scripting languages like Python. The Typst developers are considering alternative schemes that would allow function calls to be more flexible, but for now this is just how it is.

Side note: I also changed pseudocode's default value for parameters to () because algo always expects it to be an array.

Proliecan commented 1 year ago

Thanks a lot!