72 lines
3.1 KiB
Plaintext
72 lines
3.1 KiB
Plaintext
#! @file functions.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the traits of functions: `invokable`, which a type implements to be
|
|
#! called as a function is, `f(args)`, and which every function is, and
|
|
#! `signature`, which the compiler gives every function type, and which names
|
|
#! what a function takes and what it gives.
|
|
|
|
module core;
|
|
|
|
|
|
#! What is called as a function of a signature: a function value, the address of
|
|
#! a C function, or a value of a type with an `invoke` taking the arguments.
|
|
#!
|
|
#! The trait is given the signature whole, as `invokable<def(i64, bool): u8>`,
|
|
#! whose parameter types it names `Ts` and whose result `R`, so `invoke` takes a
|
|
#! parameter of each of the types, as `def adder::invoke(&mut this, a: i64, b:
|
|
#! bool): u8` does. A variadic signature, as `def(i64...): i64`, asks for a
|
|
#! variadic `invoke`. Generic code over any signature names it through a type
|
|
#! parameter bounded by `signature`, as `invokable<F>`.
|
|
#!
|
|
#! A call of a value whose type is no function, `f(1, true)`, calls its `invoke`,
|
|
#! as `f.invoke(1, true)`, by the rules a method's call has: the overloads are
|
|
#! chosen between as they are, and one taking `&mut this` is called on what can be
|
|
#! written. Any type with an `invoke` is called so, whether or not it names this
|
|
#! trait, which bounds generic code taking functions and callable values alike:
|
|
#!
|
|
#! ```sakura
|
|
#! def twice<F>(f: &mut F, x: i64): i64 where F: invokable<def(i64): i64> {
|
|
#! return f(f(x));
|
|
#! }
|
|
#! ```
|
|
#!
|
|
#! A function value, and the address of a C function, have an `invoke` the
|
|
#! compiler gives them, which calls them, so each is `invokable` of its own type.
|
|
#! An `invoke` taking `&this` meets the trait too, since what can be written can
|
|
#! be read.
|
|
#! @tparam Ts The types of the arguments it is called with.
|
|
#! @tparam R The type it gives.
|
|
pub trait invokable<def(Ts...): R> {
|
|
#! Calls it with the arguments, as `f(args)` does.
|
|
#! @param this The value called.
|
|
#! @param args The arguments, one of each of the signature's parameter types.
|
|
#! @returns What the call gives.
|
|
def invoke(&mut this, args: Ts...): R;
|
|
}
|
|
|
|
#! A function type, as `def(i64, bool): u8`, a variadic one, as `def(i64...):
|
|
#! i64`, or the address of a C function, as `cfn<def(i32): void>`.
|
|
#!
|
|
#! Generic code bounded by it names the parts of the type it is given: `F::result`
|
|
#! is what a function of it gives, and `F::params` the types it takes, as a pack,
|
|
#! so `args: F::params...` takes the arguments it does. A variadic function's
|
|
#! pack ends in the type of the values its last parameter takes, so a parameter
|
|
#! of the pack is variadic too.
|
|
#!
|
|
#! A pattern parameter, as `def(Ts...): R` of `invokable`, is given a signature
|
|
#! whole: `invokable<F>` where `F: signature` names `F::params` and `F::result`
|
|
#! as `Ts` and `R`.
|
|
#!
|
|
#! The compiler decides what is a signature, so no type implements it.
|
|
pub trait signature {
|
|
#! What a function of the type gives.
|
|
type result;
|
|
|
|
#! The types a function of the type takes, as a pack, which only the compiler
|
|
#! gives.
|
|
type params...;
|
|
}
|