fun, PascalCase names: fun Compute(a: Int): Int { ... }pure fun Hash(o: Object): Int { ... }
IHashable.class Name implements IFace1, IFace2 { ... }
Fields use val (immutable) or var (mutable):
private val Prefix: Stringpublic var Count: IntMethods must declare access and can be override/pure:
public override fun Run(): Int { ... }static var is unsafe.Destructor: optional, overrides the implicit virtual destructor from Object.
public destructor(): Void { ... } (no parameters, no return).interface IGreeter { fun Greet(name: String): String; }
typealias UserId = Intfun ProcessUser(id: UserId): Void:: (e.g., sys::Print), declared with namespace keyword.#import, #define, #ifdef, #ifndef, #else, #undef.#define commands can be used to define the same symbol, as well as #undef to undefine a symbol.#import does not include the file at the location of the #import command. It adds a dependency for the current file, and after all dependencies are collected, they are included in the final compilation. Order is defined by the topological sort of the dependency graph and lexicographic order of the files to avoid cyclical dependencies and double inclusion.Note:
#definecannot be used to really define something, it is a way to control what code will be used.
call)call member that makes instances callable like functions.call, it may have other members.call member (maybe not one, with all interface rules applying).fun(...) : T { ... } can be coerced to a interface type that exposes a compatible call(...) : T (and only this).Example:
interface CustomFunctional {
call(a: Int?, b: Int?): Int
}
class DefinedFunctional {
public var Multiplier: Int
public fun DefinedFunctional(multiplier: Int): DefinedFunctional {
this.Multiplier = multiplier
return this
}
// Defines the callable behavior; pure body allowed
public call(secondMultiplier: Int): Int = fun(secondMultiplier: Int): Int {
return Multiplier * secondMultiplier
}
}
val AddNullable: CustomFunctional = pure fun(a: Int?, b: Int?): Int {
val aVal: int = a ?: 0 // Conversion from Int? to int
val bVal: int = b ?: 0
return aVal + bVal
}
fun Main(args: StringArray): Int {
// Constructor call then functional call via `call`
return AddNullable(2, DefinedFunctional(-1)(2)) // Implicit conversion from literals
}