How to check if a variable has any value?

Note: This is NOT a duplicate of “How to check if a variable exists”

Hi!
I have a project, where a function accepts inputs of fixed types, which may or may not be passed. Within the function I would like to have different behaviour based on whether a value has been passed or not.

I already know that when a variable has been declared but not defined, I can check for that with none. However, if the variable has already been given a type, but no content/value, comparing to none will return false.

Example:

#let a
#if a == none [Yes] else [No] // Yes

#set a = 5
#if a == none [Yes] else [No] // No
// So far so good

// But:
#let some_function (
  b: int
) = {
  if b == none [Yes] else [No] // No
}

Checking for none does not help here, since none is a type and b does indeed already have a type. What it doesn’t have is any value assigned to it. How can I check for that?
What I’m looking for is a function like empty that returns true after e.g. a: int but false after e.g. a = 5 (or the other way around).

I’ve read that in addition to the type none there is also the value none but it is unclear to me how I would check for that in an if statement.

Any help is greatly appreciated, thanks in advance!

typst doesn’t have type hints yet. the syntax b: int means “named parameter b with default value int” (types are also values in typst, as functions that convert between types). so if you check, you’ll see that b == int is true. but clearly that won’t do what you want; instead, default it to b: none and then you’ll be able to check b == none

2 Likes