Pular para o conteúdo

Generics

Este conteúdo não está disponível em sua língua ainda.

Flecha implements compile-time specialization of functions, structures, and callable signatures. Type parameters are ordinary identifiers declared between < and >; T is not a keyword or a wildcard type. Generic types and callable aliases use explicit concrete type arguments. Direct function and method calls may infer them from arguments or an expected result type.

T identity<T>(T value) { return value; }
(A, B) pair<A, B>(A first, B second) { return (first, second); }
structure Box<T> { T value; };
T Box<T>::get(&const self) { return self->value; }
void Box<T>::set(&self, T value) { self->value = value; }
func Mapper<T>(T value) => (T);
i32 main() {
box := Box<i32>::init();
Mapper<i32> mapper = identity<i32>;
box.set(identity(42));
return box.get();
}

Explicit specialization works both at a call (identity<i32>(42)) and when a generic function is retained as a callable value (mapper = identity<i32>). At a direct function or method call, type arguments may be omitted when every type parameter has one consistent inference:

T identity<T>(T value) { return value; }
T parse<T>(stringv value) { return none; }
structure Generator {};
T Generator::integer<T: integer>(&self) { return none; }
(T, error) Generator::integerIn<T: integer>(&self, T low, T high) {
return (low, none);
}
i32 copied = identity(42); // T is i32 from the argument.
i32 parsed = parse("42"); // T is i32 from the declared result type.
generator := Generator::init();
i32 generated = generator.integer();
i32 bounded;
bounded, err := generator.integerIn(8, 32);

Supplied arguments and an available expected result type are combined. If they disagree, or any type parameter remains unknown, the call is rejected and must use explicit arguments. Omitted default arguments provide no inference evidence. An inferred declaration such as value := parse("42") has no expected result type and therefore cannot infer T. Numeric and floating literals use their ordinary default types, i32 and f64, when they are the evidence.

Known tuple-destructuring destinations contribute their corresponding expected result elements. New inferred destinations and _ provide no evidence. Export, quiver, module-private, and private-member visibility affect lookup and access, not inference. Generic function values, generic types, and callable aliases keep explicit arguments. Type-set constraints are checked after inference exactly as for an explicit specialization.

A method repeats its owner’s parameter names in the same order. It may declare additional parameters: U Box<T>::convert<U>(&const self). Calls may use box.convert<i32>() explicitly or omit <i32> when arguments or the expected result determine U. Lifecycle methods cannot add type parameters of their own. Method declarations remain outside the structure body.

A generic callable alias specializes to the same structural callable type that could be written as a nongeneric alias. Substitution applies to parameter and result types, including nested types, and preserves unsafe, own, lend, and borrowed-result contracts.

func Mapper<T>(T value) => (T);
func Fold<T, U>([]const T values, U initial) => (U);
func Borrower<T>(&T value) => (&T) <- value;
Mapper<i32> increment = (value) => { return value + 1; };

Specialized callable aliases work in function parameters and results, aggregate fields and containers, imported module APIs, and other generic declarations. They also provide the expected type for inferred anonymous functions. Callable aliases are structural, so independently declared aliases with identical specialized signatures are compatible. Type arguments are never inferred for the alias itself: missing, extra, and unresolved arguments are diagnosed.

Each generic declaration and ordered argument list identifies one specialization. Box<i32> and Box<f64> are different types; repeated Box<i32> uses share a type, including across imports. Nested applications such as Box<Box<i32>> work. Concrete type arguments can include pointers, arrays, slices, tuples, and named types, subject to the ordinary rules at each substituted use.

Generic functions can take &T and &const T parameters. Reference storage rules apply after specialization. A Box<&const i32> whose field is T carries a shared borrow, is nonzeroable, and may be copied when its other fields permit. A Box<&i32> carries an exclusive borrow, is nonzeroable and noncopyable, but remains movable. Its constructor or factory must return with <- source> and initialize the specialized reference-bearing field first in build. References take neither own nor lend. Raw pointers alone still carry no ownership contract; generic own *T and lend *T declarations undergo the existing ownership analysis.

const controls mutation. It does not make a resource copyable. Structures with unsafe tear remain immovable and noncopyable, and tear remains callable only by the compiler. Instantiation does not bypass borrowing, allocation checking, unsafe permissions, visibility, or lifecycle rules.

Templates are parsed when loaded. Parameter-name collisions, malformed generic declarations, and private types exposed by exported generic signatures are rejected even without an instantiation. Concrete signatures and bodies are checked when instantiated. Operations must be valid for the supplied types. A type parameter may additionally name a closed type-set constraint, such as T: integer. The constraint is checked before specialization, and ordinary body checking still follows. Type sets restrict identity; they do not grant operations or provide dynamic dispatch. Instantiating a structure also checks its ordinary methods. Methods with additional type parameters are checked when explicitly or inferentially instantiated on their actual receiver.

Export and import rules match ordinary declarations. A specialization resolves helpers in the defining module, while argument types retain their identity from the caller’s module. Importing a structure includes its public methods and preserves private-member restrictions.

Ordinary recursive calls to the same specialization are supported. Expanding recursion is bounded: specialization permits at most 64 levels of instantiation ancestry, 2,048 generated declarations, and 1,000,000 generated tokens per compilation. Exceeding a budget is an error, not permission to emit unchecked code. Invalid generic applications use FLECHA-S0054; errors in instantiated code include instantiation context.

In expressions, a syntactically valid <types> followed by ( or, for a type name, :: is a type application. Parenthesize comparisons when their token sequence would otherwise have that form. Ordinary field comparisons such as box.value < limit remain comparisons.

The parser retains template tokens and identifies type parameter substitution sites through the AST. A pre-semantic expansion pass materializes concrete AST declarations, caches specializations, and preserves source/module identity. The normal semantic, allocation, lowering, and native code generation passes then process those declarations. Generic templates themselves emit no code.

There is no partial explicit/inferred type-argument list, default type argument, variadic generic parameter pack, constant generic parameter, user specialization override, trait system, or separate compiled generic interface yet. The language’s untyped forwarding packs are a separate compile-time call-forwarding facility and cannot participate in generic substitution. The standard library provides RawArray<T>, implemented in Flecha. Its runtime storage currently supports numeric, boolean, and character elements; pointers and structures are not yet supported. Element restrictions are enforced when the relevant generic operations are instantiated.

An if or else if with T.isof<own []Element> can bind a fresh element-type name inside its selected body. The name is arbitrary; an existing type name or type parameter keeps its meaning and is compared instead of rebound. The match requires an owning slice and preserves the exact element type, including const qualification and nested ownership. For example:

usize depth<T>() {
if T.isof<own []Element> {
return 1 + depth<Element>();
} else {
return 0;
}
}

Specialization selects direct T.isof<...> branches before checking bodies, including exact types, interfaces, and type sets. Ordinary runtime conditions retain normal checking. A new pattern binding is scoped to the matching body; it is unavailable in alternatives, also, and following statements. Pattern binding in compound Boolean conditions is not supported. Recursive helpers use the existing specialization budgets and ordinary ownership checks.