Types and properties.

The Intrinsic namespace also holds types, properties a bound can require, the operator interfaces, and one import. None of them is a function: they appear in type positions, bounds and impl heads. Most have an ordinary name in the standard library, a pub type alias such as pub type Copy = Intrinsic.Copy in silk/core/ownership, and every spelling names the same thing, so impl Copy for Point {} and impl Intrinsic.Copy for Point {} are one conformance.

The bare spellings, such as Slot or Allocation, are ordinary names. A program may declare its own Slot, and without a declaration or import a bare Slot names nothing.

Storage types#

Intrinsic.Allocation#

silk
type Allocation

One owner of a block of raw memory, its Layout, and the way to release it. Dropping it releases the block: to the system for a block from systemAllocationAcquire, or through the reclaim function given to allocationAdopt. It holds no values and never drops any.

Intrinsic.RawBuffer#

silk
type RawBuffer<T>

Typed raw storage for a number of T slots, owned independently of the allocator that provided it. It is four words: the base address, the slot count, and the reclaim function and context of the allocation it adopted. It does not record which slots hold a value; the container that owns it keeps that record. Dropping it releases the storage and drops no value in it.

Intrinsic.Slot#

silk
type Slot<'storage, T>

One element of a RawBuffer<T>, borrowed exclusively from the buffer for 'storage. A slot operation consumes it, so one slot value does one thing.

Intrinsic.SharedCore#

silk
type SharedCore<T>

One handle to a value shared by several handles in one thread: the value, a handle count and a busy flag in one allocation. Cloning adds a handle; dropping the last one drops the value and releases the allocation. The value is reached only through sharedWithMut, which refuses a second access while one is running.

Intrinsic.StorageFailure#

silk
type StorageFailure

The failure of systemAllocationAcquire: the system refused the request, or the layout was invalid (an alignment that is not a nonzero power of two, or a size that overflows usize with its padding). It carries no data.

  • Transferable and Concurrent.

  • The system allocator turns it into OutOfMemoryError with catchFailure<Intrinsic.StorageFailure>; the example under systemAllocationAcquire does the same.

Execution types#

Intrinsic.Execution#

silk
type Execution<A>

The owner of one execution package whose body yields A: the body or its stored frames, the readiness endpoint and the control words. Dropping it before the body completes cancels the body, dropping the owners in its frames from the innermost out; the readiness endpoint is not called.

Intrinsic.ExecutionRef#

silk
type ExecutionRef

Counted authority over one package allocation. It keeps the allocation alive, not the body: it can read and write the control words, notify the package and ask whether its Execution handle still exists, but it cannot drive it.

Static descriptors#

These types exist only during static evaluation. A value of one is phase-only: it lives in a const, a static binding, a static for loop variable or a static fn, and a runtime argument of one is SEM0176. The members that read them are on the Static evaluation page.

Intrinsic.Type#

silk
type Type<Owner>

The descriptor of one aggregate type, from reflectType. reflectTypeKind, reflectTypeName, reflectDeclarationName, reflectTypeDoc and reflectPrivateField read it.

Intrinsic.StaticSequence#

silk
type StaticSequence<Element>

An immutable sequence built during static evaluation and walked with static for. Appending returns a new sequence. Made and read by the staticSequence members; the wrapper is StaticSequence in silk/core/static_sequence.

Intrinsic.Field#

silk
type Field<Owner, Value>

The descriptor of one field of Owner whose type is Value, from reflectField or an element of Fields<Owner>. borrowField and borrowFieldMut turn it into a field access at runtime.

Intrinsic.Fields#

silk
type Fields<Owner>

The field descriptors of a struct, or of one variant of a union, in declaration order, from reflectFields and reflectVariantFields. Each element is a Field<Owner, Value> with its own Value, so it is walked with static for.

Intrinsic.FieldName#

silk
type FieldName<Owner>

A checked field name. A parameter of this type accepts a string literal only when it names a field of Owner, so a misspelled name is an error at the literal and the editor links the literal to the field. fieldNameLabel returns its text.

Intrinsic.Variant#

silk
type Variant<Union>

The descriptor of one variant of a nominal union, an element of Variants<Union>. makeVariant, isVariant and borrowVariantField use it at runtime.

Intrinsic.Variants#

silk
type Variants<Union>

The variant descriptors of a nominal union, in declaration order, from reflectVariants.

Intrinsic.VariantName#

silk
type VariantName<Union>

A checked variant name: a parameter of this type accepts a string literal only when it names a variant of Union. variantNameLabel returns its text.

Intrinsic.FunctionRef#

silk
type FunctionRef

A static function value with its contract erased, so one sequence can hold functions of different types. functionRef makes one and functionAs recovers the function, which must have the contract it is asked for.

Intrinsic.Tests#

silk
type Tests

The catalog of the test functions in the discovery root of a silk test build, from tests. Each element is a Test<F>.

Intrinsic.Test#

silk
type Test<F>

The descriptor of one test whose function type is F. testInfo reads its metadata and testFunction returns the function.

Intrinsic.TestInfo#

silk
struct TestInfo {
  identity: string<'static>,
  name: string<'static>,
  module: string<'static>,
  path: string<'static>,
}

One test's metadata, from testInfo. identity is stable across runs, name is the function's name, module its module and path its source file. No field is a source position.

Properties#

A property is a bound or an impl head, never a value type. Ownership and thread properties are derived by the compiler; the two execution properties are proved from the code.

Intrinsic.Copy#

silk
property Copy

Values of the type duplicate without user code, so a use does not move them. Scalars, bool, char, shared references and raw pointers are Copy. A struct or union is Copy when it declares impl Copy for T {} and every stored part is Copy.

  • Alias: Copy in silk/core/ownership.

  • A second conformance through any spelling is SEM0083.

silk
import silk.core.ownership { Copy }

struct Point {
  x: i32,
  y: i32,
}

impl Copy for Point {}

fn sum<T: Intrinsic.Copy>(value: T, read: fn(T) -> i32) -> i32 {
  // A Copy value can be used twice without `move`.
  return read(value) + read(value)
}

fn x(point: Point) -> i32 {
  return point.x
}

pub fn main() -> i32 {
  if sum(Point { x: 21, y: 0 }, x) != 42 { return 1 }
  return 0
}

Intrinsic.Drop#

silk
property Drop

The type runs one cleanup operation when a value is dropped: impl Drop for T { fn drop(self: &mut T) -> () {...} }. It runs exactly once per value, at the end of its scope, at drop, or when a cancelled fiber releases the frame that owns it. A trap runs no drop.

  • Alias: Drop in silk/core/ownership. A Copy type cannot implement it.

silk
import silk.core.ownership { Drop }

struct Guard {
  closed: *mut i32,
}

impl Drop for Guard {
  fn drop(self: &mut Guard) -> () {
    unsafe Intrinsic.pointerWrite<i32>(self.closed, unsafe Intrinsic.pointerRead<i32>(self.closed) + 1)
  }
}

pub fn main() -> i32 {
  let mut closed: i32 = 0
  let guard = Guard { closed: Intrinsic.pointerFromMutRef<i32>(&mut closed) }
  drop guard
  if closed != 1 { return 1 }
  return 0
}

Intrinsic.Transferable#

silk
property Transferable

Ownership of a value may move to another thread. Scalars, string, enums, Allocation and StorageFailure are Transferable; raw pointers, SharedCore, Execution and ExecutionRef are not; an aggregate is when every stored part is, and &mut T is when T is. A type that synchronizes its own storage asserts it with impl Transferable for Lock {}.

  • Alias: Transferable in silk/concurrency/thread. Threads, channels and the thread pool bound their inputs on it; a value that is not is SEM0139 where it is handed off.

silk
import silk.concurrency.thread { Concurrent, Transferable }

struct Job {
  id: i32,
}

fn handOff<T: Transferable>(value: T) -> T {
  return move value
}

fn inspect<T: Concurrent>(value: &T) -> &T {
  return value
}

pub fn main() -> i32 {
  let job = handOff(Job { id: 42 })
  if inspect(&job).id != 42 { return 1 }
  // handOff(Intrinsic.pointerFromRef<i32>(&job.id)) is SEM0139: a raw pointer is not Transferable.
  return 0
}

Intrinsic.Concurrent#

silk
property Concurrent

A shared borrow of a value may be used from several threads at once. The derivation is the one for Transferable, except that &T and &mut T are Concurrent when T is.

  • Alias: Concurrent in silk/concurrency/thread. The example under Transferable uses both.

Intrinsic.NonParking#

silk
property NonParking

A bound on an Effect or callable type: everything it can reach, through its calls and the providers it is given, never parks with relinquish. Nested suspendEffect boundaries are allowed. The compiler proves it from the specialized code; there is no impl.

  • Required of readiness endpoints, drive callbacks, the non-parking finalizers and the observeDiagnostics callback, and of main under the default runtimes.

  • A failure is SEM0139 with the path that parks.

silk
fn invoke<F: fn<'static>() -> i32 + Intrinsic.NonParking + Intrinsic.Detached>(body: F) -> i32 {
  return body()
}

fn answer() -> i32 {
  return 42
}

pub fn main() -> i32 {
  if invoke(answer) != 42 { return 1 }
  return 0
}

Intrinsic.Detached#

silk
property Detached

A bound on an Effect or callable type: it owns what it captures and keeps no loan from the caller's scope or providers, so it can outlive the call that made it. It says nothing about threads.

  • Required of an execution package's body, readiness state and endpoint.

  • A failure is SEM0139 with the capture or provider that holds the loan. The example under NonParking requires both.

Operator interfaces#

Each overloadable operator is bound to one of these interfaces. A type gets the operator by implementing it, through either spelling; silk/core/ops, silk/core/equal and silk/core/order publish the aliases. The left (or only) operand's type is the provider, and the conformance is found in the interface's module, the provider's module or the right operand's module. Scalars have built-in conformances, so a generic bound such as T: Add<T, T> holds for i64 and f64.

Arithmetic, bitwise and logical operators take their operands by value; comparisons take shared references, so an affine operand is written &a < &b. No operator borrows, copies or moves implicitly. No conformance for the operands is SEM0135, and two are SEM0136.

silk
struct Vector {
  x: i32,
  y: i32,
}

impl Intrinsic.Add<Vector, Vector> for Vector {
  fn add(left: Vector, right: Vector) -> Vector {
    return Vector { x: left.x + right.x, y: left.y + right.y }
  }
}

impl Intrinsic.Multiply<i32, Vector> for Vector {
  fn multiply(left: Vector, right: i32) -> Vector {
    return Vector { x: left.x * right, y: left.y * right }
  }
}

impl Intrinsic.Negate<Vector> for Vector {
  fn negate(value: Vector) -> Vector {
    return Vector { x: -value.x, y: -value.y }
  }
}

impl Intrinsic.Equal for Vector {
  fn equals(left: &Vector, right: &Vector) -> bool {
    return left.x == right.x && left.y == right.y
  }
}

pub fn main() -> i32 {
  let moved = -(Vector { x: 1, y: 2 } + Vector { x: 2, y: 2 }) * 2
  let expected = Vector { x: -6, y: -8 }
  if &moved != &expected { return 1 }
  return 0
}
silk
fn total<T: Intrinsic.Add<T, T> + Intrinsic.Copy>(values: &[T], start: T) -> T {
  let mut sum = start
  let mut index: usize = 0
  while index < values.length {
    sum = sum + values[index]
    index = index + 1
  }
  return sum
}

pub fn main() -> i32 {
  let values: [i64; 3] = [40, 1, 1]
  let floats: [f64; 2] = [0.5, 0.25]
  if total(&values, 0) != 42 || total(&floats, 0.25) != 1.0 { return 1 }
  return 0
}

Intrinsic.Add#

silk
interface Add<Right, Output> {
  fn add(left: Self, right: Right) -> Output
}

a + b. Built in for every integer and float as Intrinsic.<numeric>Add, so integer overflow traps. Alias Add.

Intrinsic.Subtract#

silk
interface Subtract<Right, Output> {
  fn subtract(left: Self, right: Right) -> Output
}

a - b. Built in for every integer and float. Alias Subtract.

Intrinsic.Multiply#

silk
interface Multiply<Right, Output> {
  fn multiply(left: Self, right: Right) -> Output
}

a * b. Built in for every integer and float. A type may implement it for several Right types, such as Vector * f64 and Vector * Vector. Alias Multiply.

Intrinsic.Divide#

silk
interface Divide<Right, Output> {
  fn divide(left: Self, right: Right) -> Output
}

a / b. Built in for every integer and float; integer division by zero traps. Alias Divide.

Intrinsic.Remainder#

silk
interface Remainder<Right, Output> {
  fn remainder(left: Self, right: Right) -> Output
}

a % b. Built in for every integer and float. Alias Remainder.

Intrinsic.Negate#

silk
interface Negate<Output> {
  fn negate(value: Self) -> Output
}

-a. Built in for the signed integers and floats. Alias Negate.

Intrinsic.BitAnd#

silk
interface BitAnd<Right, Output> {
  fn bitAnd(left: Self, right: Right) -> Output
}

a & b. Built in for every integer. Alias BitAnd.

Intrinsic.BitOr#

silk
interface BitOr<Right, Output> {
  fn bitOr(left: Self, right: Right) -> Output
}

a | b. Built in for every integer. Alias BitOr.

Intrinsic.BitXor#

silk
interface BitXor<Right, Output> {
  fn bitXor(left: Self, right: Right) -> Output
}

a ^ b. Built in for every integer. Alias BitXor.

Intrinsic.BitNot#

silk
interface BitNot<Output> {
  fn bitNot(value: Self) -> Output
}

~a. Built in for every integer. Alias BitNot.

Intrinsic.Not#

silk
interface Not<Output> {
  fn not(value: Self) -> Output
}

!a. Built in for bool. Alias Not.

Intrinsic.Equal#

silk
interface Equal {
  fn equals(left: &Self, right: &Self) -> bool
}

a == b, and a != b as its negation. Built in for the scalars, bool, char and string (by UTF-8 bytes), and for &[T] element by element when T has it. Alias Equal.

  • As a bound, write the alias: T: Equal after import silk.core.equal { Equal }. The spelling T: Intrinsic.Equal is refused with SEM0083 in v0.1.0-dev.82.

Intrinsic.Order#

silk
interface Order {
  fn compare(left: &Self, right: &Self) -> Ordering
}

<, <=, > and >=. Each compares compare(a, b) with one Ordering member: < tests Less, <= tests not Greater, > tests Greater and >= tests not Less. Built in for the integers, char and string (by UTF-8 bytes, which is Unicode scalar order), &[T] lexicographically, and the floats by the IEEE 754 total order, so NaN has a place. The operators on concrete floats keep IEEE comparison, where every comparison with NaN is false. Alias Order.

  • As a bound, write the alias: T: Order after import silk.core.order { Order }. The spelling T: Intrinsic.Order is refused with SEM0083 in v0.1.0-dev.82.

silk
import silk.core.order { Order, Ordering }

struct Version {
  major: u32,
  minor: u32,
}

impl Order for Version {
  fn compare(left: &Version, right: &Version) -> Ordering {
    if left.major != right.major {
      if left.major < right.major { return Ordering.Less }
      return Ordering.Greater
    }
    if left.minor < right.minor { return Ordering.Less }
    if left.minor > right.minor { return Ordering.Greater }
    return Ordering.Equal
  }
}

fn largest<T: Order>(left: &T, right: &T) -> bool {
  return left < right
}

pub fn main() -> i32 {
  let old = Version { major: 1, minor: 9 }
  let new = Version { major: 2, minor: 0 }
  if !(&old < &new) || !largest(&old, &new) { return 1 }
  return 0
}

Intrinsic.Ordering#

silk
enum Ordering {
  Less,
  Equal,
  Greater,
}

The result of Order.compare. Alias Ordering; name its members through the alias, as in Ordering.Less, because Intrinsic.Ordering.Less is not a value path.

Intrinsic.Rows#

silk
interface Rows<Index, Row> {
  fn borrow<'s, V>(self: &'s Self, index: Index, static field: Intrinsic.Field<Row, V>) -> &'s V
  fn span<'s, V>(self: &'s Self, static field: Intrinsic.Field<Row, V>) -> &'s [V]
}

Reads a field from a collection of rows. For cells[i].count, the compiler selects the count field of Row at compile time and calls borrow with that field and the index. For &cells[..].count, it calls span to borrow the column as a slice. The standard library implements this sealed interface for collections that store each field in a separate column. The slice follows row order and borrows from the collection. ColumnList accepts a usize index or an Id<Row>.

Intrinsic.RowsMut#

silk
interface RowsMut<Index, Row> {
  fn borrowMut<'s, V>(self: &'s mut Self, index: Index, static field: Intrinsic.Field<Row, V>) -> &'s mut V
  fn spanMut<'s, V>(self: &'s mut Self, static field: Intrinsic.Field<Row, V>) -> &'s mut [V]
}

Writes through a row field or a column. Assigning to cells[i].count or taking &mut cells[i].count uses borrowMut. Taking &mut cells[..].count uses spanMut and returns a mutable slice of the column. Different fields can be borrowed at the same time because their columns occupy separate storage. In this example, changing the column changes the value read through the row.

silk
import silk.collections.column_list { ColumnList }
import silk.core.effect { Effect }
import silk.core.ownership { Copy }
import silk.mem.allocator { Allocator, OutOfMemoryError }

struct Cell { pub count: i32 }
impl Copy for Cell {}

pub effect fn main() -> i32 ! OutOfMemoryError {
  let mut allocator = Allocator.systemAllocatorProvider()
  let mut cells = ColumnList<Cell>.make()
  run cells.append(Cell { count: 1 }) |> Effect.provideMut<Allocator>(&mut allocator)
  cells[0].count = 2
  let counts = &mut cells[..].count
  counts[0] = counts[0] + 1
  if cells[0].count != 3 { return 1 }
  let values = &cells[..].count
  if values.length != 1 || values[0] != 3 { return 2 }
  return 0
}

Intrinsic.Omittable#

silk
interface Omittable {
  fn omitted() -> Self
}

The interface behind leaving a field out of a struct or variant literal: a field whose type implements it may be left out, and holds omitted(). Option<T> implements it with None, so every Option field is optional. A left-out field of another type is SEM0024.

silk
import silk.core.option { Option }

enum Level {
  Quiet,
  Normal,
}

impl Intrinsic.Omittable for Level {
  fn omitted() -> Level {
    return Level.Normal
  }
}

struct Config {
  name: string<'static>,
  level: Level,
  port: Option<u16>,
}

pub fn main() -> i32 {
  // level and port are left out: they hold Level.omitted() and Option.None.
  let config = Config { name: "web" }
  if config.level != Level.Normal || config.port.isSome() { return 1 }
  return 0
}

Program entry#

Intrinsic.application#

silk
import Intrinsic.application

The one importable member of Intrinsic: a module binding for the application module of the build, the package whose main the build runs. A runtime module imports it and calls a visible function of it, usually application.main(); the name main has no other privilege, and a custom runtime may call another function or pass arguments. Every other member is written Intrinsic.<name> without an import, and importing one is MOD0004.

  • The default runtimes, silk/platform/native/start, silk/platform/wasm/start and silk/platform/wasi/start, import it, run main under an observer and exit with its status.

  • A missing or private application member is an ordinary error at the import or the call.

View the source of this page