Skip to content

TP0203

Error: optional used without narrowing (expected T, got T?)

  • help the change to a <T> on line <n> could have changed it; copy it to a 'let' first (an element, M40)
  • <T>? can only be matched by '_' or a binding, with help 'None' patterns aren't supported yet; bind it and test it with 'is some' (a pattern, M29)
  • expected <T>, found <T>?
  • <T>? must be narrowed before use, with help narrow it with 'is some', or give a default with '??', or one saying why a name or path isn’t narrowed there (§3.9, M27)

From the golden case tests/cases/m11/tp0203_functions.tpn:

tp0203_functions.tpn
var f: Callable[[], int]? = None
print(f())

The compiler reports:

tp0203_functions.tpn:2:7: error TP0203: Callable[[], int]? must be narrowed before use
help: narrow it with 'is some', or give a default with '??'

The specification lists every code.