inline
/** @inline */ asks for code to be rewritten at build time, and it can't be: a statement that can't become a loop, or a function that can't be inlined. A chain on std's InlineArray asks the same as @inline on its statement.
Why: @inline asks for speed, usually on a hot path. Falling back without a word would hide that the speed you asked for isn't there. Guides: Loops, Inlining functions.
On a statement: the loop rewrite
❌ Rejected: the callback isn't an arrow, and it is copied before the loop, so it must fit on one line
✕ Rejectedinline
ts
type Order = { readonly amount: number };
export function labels(orders: readonly Order[], format: (o: Order) => string): string[] {
/** @inline */
const result = orders.map(function (o) {
return format(o);
});
return result;
}✅ Accepted: an arrow written in place (its body goes into the loop), or the function by name
✓ Accepted0 diagnostics
ts
type Order = { readonly amount: number };
export function labels(orders: readonly Order[], format: (o: Order) => string): string[] {
/** @inline */
const result = orders.map((o) => format(o));
return result;
}ts
type Order = { readonly amount: number };
export function labels(orders: readonly Order[], format: (o: Order) => string): string[] {
/** @inline */
const __la1 = orders; const __lr1: any[] = new Array(__la1.length); for (let __li1 = 0, __ln1 = __la1.length; __li1 < __ln1; __li1++) { if (!(__li1 in __la1)) continue; const __le1 = __la1[__li1]!; { let o = __le1; __lr1[__li1] = (format(o)); }} const result = __lr1;
return result;
}The message says what stands in the way: a callback that isn't an arrow and spans lines, an argument spread into a step (map(...fns)), an optional call (orders?.map(...)), await in a chain that has to be called in place (ready ? (await load()).map(f) : []: give it a statement of its own), q(...) inside a callback, or a statement with no array method chain at all.
Everything else becomes a loop: a callback by name or with a thisArg, an index after a filter (it counts what the filter kept), reduce without an initial value, flatMap, slice, findLast, includes, and methods that have no loop of their own (toSorted, concat, ...), which run on what the loop before them built.
What @inline vouches for: that the receiver is an array (the rewrite would otherwise need to see its type), and that the callbacks may run element by element rather than step by step. It does not check either: on a Map the loop would be wrong.
On an InlineArray
A chain on std's InlineArray must become loops, like a statement with @inline, and the same things stop it. The error is on the chain.
❌ Rejected: await inside a chain that must be called in place
✕ Rejectedinline
ts
import type { ReadonlyInlineArray } from "@evobug_com/safe-ts-std/array";
export async function names(
users: ReadonlyInlineArray<string>,
more: () => Promise<readonly string[]>,
all: boolean,
): Promise<readonly string[]> {
return all ? users.concat(await more()).map((u) => u.trim()) : [];
}✅ Accepted: the await gets a statement of its own
✓ Accepted0 diagnostics
ts
import type { ReadonlyInlineArray } from "@evobug_com/safe-ts-std/array";
export async function names(
users: ReadonlyInlineArray<string>,
more: () => Promise<readonly string[]>,
all: boolean,
): Promise<readonly string[]> {
if (!all) return [];
const extra = await more();
return users.concat(extra).map((u) => u.trim());
}ts
import type { ReadonlyInlineArray } from "@evobug_com/safe-ts-std/array";
export async function names(
users: ReadonlyInlineArray<string>,
more: () => Promise<readonly string[]>,
all: boolean,
): Promise<readonly string[]> {
if (!all) return [];
const extra = await more();
{ const __la1 = users; const __ls1_0 = __la1.concat(extra); const __lr1: any[] = new Array(__ls1_0.length); for (let __li1 = 0, __ln1 = __ls1_0.length; __li1 < __ln1; __li1++) { if (!(__li1 in __ls1_0)) continue; const __le1 = __ls1_0[__li1]!; { let u = __le1; __lr1[__li1] = (u.trim()); }} return (__lr1 as any); }
}On a function: inlining its calls
❌ Rejected: the body uses a constant of its module, which doesn't exist where it is inlined
✕ Rejectedinline
ts
const LIMIT = 100;
/** @inline */
export const capped = (n: number): number => (n > LIMIT ? LIMIT : n);✅ Accepted: pass it in, or write the value
✓ Accepted0 diagnostics
ts
/** @inline */
export const capped = (n: number, limit: number): number => (n > limit ? limit : n);The function must be declared at the top level of its module, have one expression as its body, plain parameters (a rest parameter only spread as ...name, or with versions to pick), and use only its parameters and globals. The message names the first thing in the way.