Lyricdev
GitHub

LYR-SEM0193

a splice outside a quote or of the wrong kind

error · spec 11-metaprogramming

A splice #{…} stands outside a quote, or splices a kind of syntax its position in the quote does not take, such as Stmt[] where an expression stands.

Explanation

A splice inserts a tree into the tree a quote builds, so it exists only inside a quote, and what it inserts must fit where it stands: a name position takes an Ident, a type position a Type, an expression position an Expr, Ident, Literal or Block, a statement of its own also Stmt or Stmt[].

Splice a tree of the kind the position takes.

Wrong

import std.syntax { Expr, Stmt };

macro plusOne(s: Stmt[]): Expr {
    return quote { #{s} + 1 };
}

fn main(): void {
}
import std.syntax { Expr };

macro plusOne(e: Expr): Expr {
    return quote { #{e} + 1 };
}

fn main(): void {
}