Skip to main content

Annotations

Annotations in k# are used to influence the compiler and optimizer.

note

If the goal is produce code, refers to macros.

Annotations go before the element who is going to be marked, using @annotationName

@sideEffect
log x = println x

Annotations

sideEffect or impure

Functions marked with this annotation has side effects, so calling this function is not guarantied return the same result

@sideEffect
log x = println x

@impure
random = rand
note

If a function a use a impure function b then a is impure.

tip

The @impure annotation is always calculated, this annotation is for documentation purposes, compiler always ignore the annotation.

pure

Functions marked with this annotation has not side effects, so calling this function is guarantied return the same result

@pure
native sum a b
note

The @pure only applies for native functions. uses in other functions are ignored by the compiler

constant

Functions marked with this annotation are considered constant values

@constant
native pi
note

The @constant only applies for native functions. uses in other functions are ignored by the compiler

name

Used to define the name of the function, depending the target language

@name("round-value", for=["clojure"])
@name("RoundValue", for=["c#", "f#"])
roundValue value decimals = ...
Note

Compiler support different rules for function names depending the language.

doc

Used to define documentation over types and functions

@doc("sum two numbers **using** ...")
specialSum a b = ...
Note

Documentation supports markdown syntax

if

Instruct the compiler the function should compile only for target language

@if(["java", "c#"])
fn = ... //if target language is java or c# compiler compiles this function

Annotation value types supported

The annotation values can be of the following types:

  1. String
  2. Number
  3. Boolean: true, false
  4. Annotation
  5. List of String, Number or Annotations

All values inside annotation should have a label, the first value could not have a label, in this case the label is default

@if(default=["java"])

// the same as

@if(["java"])
warning

If an annotation has the same label for a value, the las define value is used.

@if("java", "c#")

// is the same as

@if("c#")