pub struct Function<Prim: PrimitiveType = Num> { /* private fields */ }
Expand description

Functional type.

Notation

Functional types are denoted as follows:

for<len! M; 'T: Lin> (['T; N], 'T) -> ['T; M]

Here:

  • len! M and 'T: Lin are constraints on length params and type params, respectively. Length and/or type params constraints may be empty. Unconstrained type / length params (such as length N in the example) do not need to be mentioned.
  • len! M means that M is a static length.
  • Lin is a constraint on the type param.
  • N, M and 'T are parameter names. The args and the return type may reference these parameters.
  • ['T; N] and 'T are types of the function arguments.
  • ['T; M] is the return type.

The for constraints can only be present on top-level functions, but not in functions mentioned in args / return types of other functions.

The -> _ part is mandatory, even if the function returns Type::void().

A function may accept variable number of arguments of the same type along with other args. (This construction is known as varargs.) This is denoted similarly to middles in Tuples. For example, (...[Num; N]) -> Num denotes a function that accepts any number of Num args and returns a Num value.

Construction

Functional types can be constructed via Self::builder() or parsed from a string.

With Self::builder(), type / length params are implicit; they are computed automatically when a function or FnWithConstraints is supplied to a TypeEnvironment. Computations include both the function itself, and any child functions.

Examples

let fn_type: Function = FunctionAst::try_from("([Num; N]) -> Num")?
    .try_convert()?;
assert_eq!(*fn_type.return_type(), Type::NUM);
assert_matches!(
    fn_type.args().parts(),
    ([Type::Tuple(t)], None, [])
        if t.as_slice().map(Slice::element) == Some(&Type::NUM)
);

Implementations§

source§

impl<Prim: PrimitiveType> Function<Prim>

source

pub fn builder() -> FunctionBuilder<Prim>

Returns a builder for Functions.

source

pub fn args(&self) -> &Tuple<Prim>

Gets the argument types of this function.

source

pub fn return_type(&self) -> &Type<Prim>

Gets the return type of this function.

source

pub fn is_concrete(&self) -> bool

Returns true iff this type does not contain type / length variables.

See TypeEnvironment for caveats of dealing with non-concrete types.

source

pub fn with_constraints<C: Constraint<Prim>>( self, indexes: &[usize], constraint: C ) -> FnWithConstraints<Prim>

Marks type params with the specified indexes to have constraints.

Panics
  • Panics if parameters were already computed for the function.
source

pub fn with_static_lengths(self, indexes: &[usize]) -> FnWithConstraints<Prim>

Marks lengths with the specified indexes as static.

Panics
  • Panics if parameters were already computed for the function.

Trait Implementations§

source§

impl<Prim: Clone + PrimitiveType> Clone for Function<Prim>

source§

fn clone(&self) -> Function<Prim>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Prim: Debug + PrimitiveType> Debug for Function<Prim>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Prim: PrimitiveType> Display for Function<Prim>

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Prim: PrimitiveType> From<FnWithConstraints<Prim>> for Function<Prim>

source§

fn from(value: FnWithConstraints<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> From<Function<Prim>> for Type<Prim>

source§

fn from(fn_type: Function<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PartialEq + PrimitiveType> PartialEq<Function<Prim>> for Function<Prim>

source§

fn eq(&self, other: &Function<Prim>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Prim: PrimitiveType> StructuralPartialEq for Function<Prim>

Auto Trait Implementations§

§

impl<Prim = Num> !RefUnwindSafe for Function<Prim>

§

impl<Prim> Send for Function<Prim>

§

impl<Prim> Sync for Function<Prim>

§

impl<Prim> Unpin for Function<Prim>where Prim: Unpin,

§

impl<Prim = Num> !UnwindSafe for Function<Prim>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.