W3cubDocs

/Crystal

type

A type declaration inside a lib declares a kind of C typedef, but stronger:

lib X
  type MyInt = Int32
end

Unlike C, Int32 and MyInt are not interchangeable:

lib X
  type MyInt = Int32

  fun some_fun(value : MyInt)
end

X.some_fun 1 # Error: argument 'value' of 'X#some_fun' must be X::MyInt, not Int32

Thus, a type declaration is useful for opaque types that are created by the C library you are wrapping. An example of this is the C FILE type, which you can obtain with fopen.

Refer to the type grammar for the notation used in typedef types.

To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/docs/syntax_and_semantics/c_bindings/type.html