Parameter Passing Modes

Each parameter in Cilia has a “parameter passing mode” that defines how its argument is passed and used — whether it is input-only, mutable, output, copied, or moved.

The basic idea is to have the most efficient/appropriate parameter passing as the default, and to give more the intent than the technical realization.

Taken from Cpp2 / Herb Sutter (surely inspired by the out parameters of C#, and by Ada).

Default is passing as in-parameter. So if no parameter passing keyword is given, in parameter passing is used. All other parameter passing methods need to be explicitly given.

Function Parameters

Function call parameters are passed as either:

The wording describes how the argument gets into or out of the function body.

Loop Variables

The loop variable of a for ... in loop can be passed as in, inout, copy, or move. Here, the passing mode describes how the loop variable gets into the loop body:

out and forward are not applicable to loop variables.

Exception Handlers

The exception caught by catch is passed as in:

try {
    ...
} catch Exception ex {
    ...
}

copy, inout, move are not recommended for exception handlers, out and forward are not applicable.

Parameter Passing Mode Keywords

Type Trait InParameterType

The type trait InParameterType determines the concrete type to be used for in-passing.

The rule of thumb is:

So, as general default, use const reference,

extension<type T> T {
  using InParameterType = const T&
}

and use a “list of exceptions” for the “const value types”.

extension     Bool { using InParameterType = const Bool }
extension     Int8 { using InParameterType = const Int8 }
extension    Int16 { using InParameterType = const Int16 }
extension    Int32 { using InParameterType = const Int32 }
extension    Int64 { using InParameterType = const Int64 }
extension    UInt8 { using InParameterType = const UInt8 }
extension   UInt16 { using InParameterType = const UInt16 }
extension   UInt32 { using InParameterType = const UInt32 }
extension   UInt64 { using InParameterType = const UInt64 }
extension  Float32 { using InParameterType = const Float32 }
extension  Float64 { using InParameterType = const Float64 }
extension   std::string_view { using InParameterType = const std::string_view }
extension  std::span<type T> { using InParameterType = const std::span<T> }
...

This way developers only need to extend this list if they create a small class (and if they need or want maximum performance). And I expect most custom classes to be larger than 16 bytes (so nothing to do for those).

extension<type T> Complex<T> {
    using InParameterType = Conditional<
      IsReference(T::InParameterType),
      const Complex<T>&,
      const Complex<T>
    >
}
extension  Complex<Float128> { using InParameterType = const Complex<Float128>& }

According to the generic rule, Complex<T> is passed the same way as T. But as sizeOf(Complex<Float128>) is 32 bytes (so pass by reference is desired), despite sizeOf(Float128) is 16 bytes (so pass by value would be the default), the generic rule is corrected.

Special Trick for Types with Views

Applicable only for types X that have an XView counterpart where

Like:

extension String { using InParameterType = const StringView }
extension<type T>  T[0] { using InParameterType = const Span }

So for an in String in fact a const StringView is used as parameter type. Then all functions with a String (AKA in String) parameter would implicitly accept

This way people do not necessarily need to understand the concept of a StringView. They simply write String and still cover all these cases.

Example

concat(String first, String second)

is short for concat(in String first, in String second)
and extends to concat(const StringView first, const StringView second).

Though I don’t see any advantage with respect to the for ... in loop, I would still apply the same rules just for consistency.

String[] stringArray = ["a", "b", "c"]
for str in stringArray {
  // str is a const StringView
}

This is not possible with every view type, as some views do not guarantee contiguous memory access (typically when they do support stride):

Small ...View-classes with a size of up to 16 bytes (such as StringView, ArrayView/Span, and VectorView) will be passed by value:

extension String { using InParameterType = const StringView }
extension  Array { using InParameterType = const ArrayView }
extension Vector { using InParameterType = const VectorView }

Bigger ...View-classes with a size of more than 16 bytes (such as MatrixBasicView, ImageBasicView, and NDArrayBasicView) will be passed by reference:

extension  Matrix { using InParameterType = const MatrixBasicView& }
extension   Image { using InParameterType = const ImageBasicView& }
extension NDArray { using InParameterType = const NDArrayBasicView& }

But you do not have to write that explicitly, because const& simply is the standard for user defined types anyway.

Type Trait CopyParameterType

The type trait CopyParameterType of a type T typically is simply T itself:

extension<type T> T { using CopyParameterType = T }

But for View-types the corresponding “full”/owning type is used instead:

extension       StringView { using CopyParameterType = String }
extension        ArrayView { using CopyParameterType = Array }
extension       VectorView { using CopyParameterType = Vector }
extension       MatrixView { using CopyParameterType = Matrix }
extension  MatrixBasicView { using CopyParameterType = Matrix }
extension        ImageView { using CopyParameterType = Image }
extension   ImageBasicView { using CopyParameterType = Image }
extension      NDArrayView { using CopyParameterType = NDArray }
extension NDArrayBasicView { using CopyParameterType = NDArray }

The idea is to get a mutable copy of the object, even without understanding the concept of a View.

Example

for copy str in ["an", "array", "of", "words"] { ... }

While the literal ["an", "array", "of", "words"] is a StringView[4], str is a String (not a StringView).

This way people do not necessarily need to understand the concept of a StringView literal. They simply write copy to get a String with a copy of the content of the StringView.