Built-in annotations
Tells the compiler how to link a C library. This is explained in the lib section.
Extern
Marking a Crystal struct with this annotation makes it possible to use it in lib declarations:
You can also make a struct behave like a C union (this can be pretty unsafe):
@[Extern(union: true)]
property int = 0
property char = '\0'
end
s = Int32OrChar.new
s.int # => 65
s.int = 66
s.char # => 'B'
ThreadLocal
The @[ThreadLocal]
annotation can be applied to class variables and C external variables. It makes them be thread local.
Marks a as packed, which prevents the automatic insertion of padding bytes between fields. This is typically only needed if the C library explicitly uses packed structs.
AlwaysInline
Gives a hint to the compiler to always inline a method:
@[AlwaysInline]
def foo
end
NoInline
Tells the compiler to never inline a method call. This has no effect if the method yields, since functions which yield are always inlined.
Marks a method or lib fun as returning twice. The C setjmp
is an example of such a function.
Raises
CallConvention
Indicates the call convention of a . For example:
lib LibFoo
@[CallConvention("X86_StdCall")]
fun foo : Int32
end
The list of valid call conventions is:
- C (the default)
- Fast
- Cold
- WebKit_JS
- AnyReg
- X86_StdCall
- X86_FastCall
They are explained here.
Marks an as a “flags enum”, which changes the behaviour of some of its methods, like to_s
.