Skip to content

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* handle, which you can obtain with fopen and pass to fclose. Whether the C type is an opaque pointer such as typedef void *MyType or a pointer to a struct whose fields Crystal does not need to access, it can be represented as Void* because the C library allocates and manages the underlying memory and Crystal only passes the pointer around:

lib LibC
  type FILE = Void*

  fun fopen(filename : Char*, mode : Char*) : FILE
  fun fclose(stream : FILE) : Int32
end

file = LibC.fopen("test.txt", "r")
LibC.fclose(file)

ptr = Pointer(Void).malloc(1)
LibC.fclose(ptr) # Error: argument 'stream' of 'LibC#fclose' must be LibC::FILE, not Pointer(Void)

Using type instead of alias restricts the pointer type in Crystal's type system. An alias would make FILE interchangeable with Void* and accept any Void* pointer.

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