Compile-time Stuff

Compile-time Flags

  • Check base:builtin/builtin.odin .

When

  • when .

  • Certain compile-time expressions.

  • The when  statement is almost identical to the if  statement but with some differences:

    • Each condition must be a constant expression as a when  statement is evaluated at compile time .

    • The statements within a branch do not create a new scope.

    • The compiler checks the semantics and code only  for statements that belong to the first condition that is true .

    • An initial statement is not allowed in a when  statement.

    • when  statements are allowed at file scope.

when ODIN_ARCH == .i386 {
    fmt.println("32 bit")
} else when ODIN_ARCH == .amd64 {
    fmt.println("64 bit")
} else {
    fmt.println("Unsupported architecture")
}

Directive #config

In Code
  • TRACY_IS_ENABLED :: #config(TRACY_ENABLE, false)

    • The name on the left is for code use. The name on the right is for the compiler.

    • They can be the same, it doesn't matter.

Compilation Flag
odin run . -define:TRACY_ENABLE=true
  • The flags require  the pattern value=key ; just using -define:TRACY_ENABLE  will cause a compile-time error.

  • For what I've tested, only one value  is accepted per -define ; I don't think you can concatenate defines.

  • Caio:

    • If a lib defines OPTION :: #config(OPTION, false) , is it possible for me to enable it in my app, without using compiler flags? If I redefine it in my app as OPTION:: #config(OPTION, true) , it doesn't work.

  • Oskar:

    • Only compiler flag.

Procedure Disabled

@(disabled=CONDITION)
  • Disabled the procedure at compile-time if the condition is met.

  • The procedure will not be used  when called.

  • The procedure cannot have a return value.

  • The procedures using this are still type-checked.

    • This differs from Zig. Odin tries to check as much as possible.

  • Modify the compilation details or behavior of declarations.

Static

  • @(static)

Read-Only

  • @(rodata)

Comp-time Loop

  • Barinzaya:

    • There's no compile-time loop, though. I seem to recall Bill saying something about not wanting to add it, IIRC because it's a bit of a slippery slope (e.g. then people will want to be able to iterate over struct fields). I can't find the message I'm thinking of, though.

  • Sobex:

    • Since you unroll you can kinda do a unrolled loop with inlined recursion

    comp_loop :: #force_inline proc(as: []int, $i: int, $end: int) {
        _, _ = args[i].(intrinsics.type_proc_parameter_type(F, i))
        a := as[i]
        fmt.print(a)
        when i + 1 != end do comp_loop(as, i+1, end)
    }
    as := [?]int{5, 4, 3, 2, 1}
    comp_loop(as[:], 0, 5)
```

Build Tags

  • Build Tags .

  • Used to define build platforms.

  • It is recommended to use File Suffixes  anyway.

    • This has a function, not just decorative.

    • "For example, foobar_windows.odin  would only be compiled on Windows, foobar_linux.odin  only on Linux, and foobar_windows_amd64.odin  only on Windows AMD64."

Ignore

#+build ignore

Control

Force Inline ( #force_inline )
  • Doesn't work on -o:none .

Intrinsics

  • Intrinsics .

  • intrinsics.type_is_integer()

    • Caio:

      proc (a: [$T]any) where intrinsics.type_is_integer(T)
      
      Expected a type for 'type_is_integer', got 'T'
          intrinsics.type_is_integer(T)
      
    • Blob:

      • Because the type of T  is an untyped integer, as it's technically a constant, & there's no way to check against an untyped type (I would like there to be honestly). What you'd want to check against is the type of the array itself proc (a: $E/[$T]any) where intrinsics.type_is_array(E) .

  • intrinsics.type_elem_type()

    • Underlying type.

    • Useful for arrays.

Custom Attributes

 -custom-attribute:<string>
                Add a custom attribute which will be ignored if it is unknown.
                This can be used with metaprogramming tools.
                Examples:
                        -custom-attribute:my_tag
                        -custom-attribute:my_tag,the_other_thing
                        -custom-attribute:my_tag -custom-attribute:the_other_thing
  • If you don't use this flag for a custom attribute, there will be a compiler error.

  • I imagine this is best used when in conjunction with core:odin/parser , or something like it?

Example
@(my_custom_attribute)
My_Struct :: struct {

}