Auto functions

Common auto functions

Keen will provide an automatic implementation for certain functions.
Just write the function signature without a body and Keen will do the rest.

main void() a point = 1, 2 log a == a log a <=> a log get-hash a # This depends on 'hash' being defined log a # This depends on 'to json' being defined point record x float y float == bool(a point, b point) <=> comparison(a point, b point) hash void(a point, state hash-state) to json(a point)

All of these are implemented by calling a function of the same name on each field and combining the results.

  • == compares each field (by calling the == function for each field type) and returns true if they are all equal.
  • <=> compares each field in declaration order.
  • hash hashes each field in declaration order.
  • to calls to on each field and creates a json object.

Except for to, it is unsafe to define these functions yourself.
(You can still do so if you mark the body as trusted. See Unsafe code.)
maps depend on those functions being implemented correctly, so it's best to let Keen implement them.

Comparison

You should never define !=, <, >, <=, or >=. These are defined as generic functions that depend on == and <=>.
If you define <=>, you should always define == too.