Skip to content

Type definitions

You can declare your own types using the type keyword.

type customStringType = str

NC uses nominal typing rather than structural typing, so if you define a custom type with a different name, that is now a distinct type from the original, and the two cannot be used interchangably.

fn someFunc(str arg) {
  // ...
}

customStringType cst = "hello world"
someFunc(cst) // error: expected `str`, found `customStringType`

Note that you can use the syntax of the underlying type to create the variable. In the example above, cst was initialised using the same "string" syntax as if it were a string, but it cannot be used in the same place that a string can.

Type casting

Creating custom types can create situations where the custom type cannot be used with your existing code, or the standard library. In cases like this, you can use the as keyword to cast your variable to another type, only if the underlying types are equivalent.

type customStr = str

fn str(customStr cs) -> str {
  return cs as str
}

customStr cs = "abc"
str s = str(cs)

This means that you cannot cast an int to a uint, for example, since the two are distinct types, and you must use the builtin uint() function to do that conversion instead.