Skip to content

typecheck

Checks whether two arguments have the same Csound type.

Use typecheck when debugging an orchestra or a user-defined opcode to check the types of its inputs. The result is a Boolean that you can use in an if statement.

Syntax

sameType:b = typecheck(xarg1, xarg2)
sameType:b typecheck xarg1, xarg2

Initialization

xarg1 and xarg2 are variables or expressions of any Csound type. sameType is an initialization-time Boolean, true when both arguments have the same internal type and false otherwise.

Values do not affect the result. Two i-rate numbers with values of 60 and 72 match. An i-rate number and a k-rate number differ even when both hold 60, since rate is part of the type.

Numeric literals have the constant type c. Comparing an i-rate variable with the literal 60 therefore returns false. Compare with another i-rate variable when you want to check for type i.

All arrays share the internal array type. typecheck returns true for two arrays even if their element types, dimensions or lengths differ. Check those properties separately when they matter to your code.

The opcode runs at initialization only. Passing k-rate values or audio signals does not make it run during performance. It prints no messages and does not require --run-unit-tests.

Examples

This example checks the types of note values, then compares a numeric array with a string array. It uses typecheck.csd.

Compare types of note values and arrays
<CsoundSynthesizer>
<CsOptions>
-n -d -m0
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

instr 1
  iNote = 60
  iOtherNote = 72
  kNote init 60
  iNotes[] fillarray 60, 64, 67
  SNames[] fillarray "C", "E", "G"

  ; Different values can have the same type.
  sameType:b = typecheck(iNote, iOtherNote)
  if sameType then
    prints "Both note values are i-rate\n"
  endif

  ; Equal values can have different types.
  if typecheck(iNote, kNote) then
    prints "The note types match\n"
  else
    prints "The i-rate and k-rate notes have different types\n"
  endif

  ; Array element types are not part of this comparison.
  if typecheck(iNotes, SNames) then
    prints "Both arguments are arrays\n"
  endif
endin
</CsInstruments>
<CsScore>
i 1 0 0.1
e
</CsScore>
</CsoundSynthesizer>

The output includes these lines.

Both note values are i-rate
The i-rate and k-rate notes have different types
Both arguments are arrays

The last result only tells us that both arguments are arrays. It does not mean that an opcode expecting a numeric array will accept the string array.

See also

Conditional Values, if

Credits

Opcode by Victor Lazzarini, July 2025.

New in Csound 7.