gleamgen/expression/block

Types

Blocks are used to group expressions together and are needed to define local variables.

{
  use x <- block.with_let_declaration("x", expression.int(4))
  use y <- block.with_let_declaration(
    "y",
    expression.math_operator(x, expression.Add, expression.int(5)),
  )
  block.ending_block(y)
}
|> block.build()
// type of Expression(Int)
|> expression.render(render.default_context())

This will generate the following code:

{
  let x = 4
  let y = x + 5
  y
}

Blocks also can be created without the use syntax through new and new_unchecked

pub opaque type BlockBuilder(type_)

Functions

pub fn build(builder: BlockBuilder(a)) -> Expression(a)
pub fn ending_block(expr: Expression(a)) -> BlockBuilder(a)

Used as the final expression in a block. If you want a dynamic final expression see ending_unchecked.

This will set the type of the block to the type of the expression passed in.

pub fn ending_unchecked(
  statements: List(Statement),
) -> BlockBuilder(a)
pub fn new(
  statements: List(Statement),
  return: GeneratedType(a),
) -> Expression(a)
pub fn new_unchecked(
  statements: List(Statement),
) -> Expression(a)
pub fn with_expression(
  expression: Expression(a),
  handler: fn() -> BlockBuilder(b),
) -> BlockBuilder(b)
pub fn with_let_declaration(
  variable: String,
  value: Expression(a),
  handler: fn(Expression(a)) -> BlockBuilder(b),
) -> BlockBuilder(b)
pub fn with_statements_unchecked(
  statements: List(Statement),
  handler: fn() -> BlockBuilder(a),
) -> BlockBuilder(a)
Search Document