Var

Var( name )
Var( name )
Var( name )
Var( name )
let name
Var (  name )

Description

The Var statement evaluates and returns the value stored in a named variable. The Var statement can only be used inside other statements, such as Let or Lambda.

When _ (underscore) is used to define an item in a parameter array for a Lambda , it is not a variable; it is a placeholder for a parameter that should be ignored. You cannot access an ignored parameter with Var('_'). For example, this is invalid:

Map(
  [
    ['a', 1],
    ['b', 2]
  ],
  Lambda(
    ["name", "_"],
    {
      name: Var("name"),
      number: Var("_")
    }
  )
)
Error: invalid expression
{
  errors: [
    {
      position: [
        'map',
        'expr',
        'object',
        'number'
      ],
      code: 'invalid expression',
      description: "Variable '_' is not defined."
    }
  ]
}

You can use _ as a field name in a document or object, such as in a Let binding. For example, this is valid:

Let({ _: 'bar' }, Var('_'))
'bar'

Parameters

Argument Type Definition and Requirements

name

The name of the variable whose value should be returned.

Returns

The Value stored in the variable identified by name.

Examples

The following query defines two variables in a Let statement. The first variable "x" contains the value 1 and the second variable "y" contains the value 2. When the simple "in" expression is executed, it returns the value stored in the variable "x".

try
{
    Value result = await client.Query(
        Let("x", 1, "y", 2).In(Var("x"))
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
LongV(1)
result, err := client.Query(
	f.Let().Bind(
		"x", 1).Bind(
		"y", 2).In(
		f.Var("x")))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
1
System.out.println(
    client.query(
        Let("x", Value(1), "y", Value(2)).in(Var("x"))
    ).get());
1
client.query(
  q.Let({ x: 1, y: 2 }, q.Var('x'))
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
1
result = client.query(
  q.let({"x": 1, "y": 2}, q.var("x"))
)
print(result)
1
try {
  println(Await.result(
    client.query(
  Let(Seq("x" -> 1, "y" -> 2), Var("x"))
),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
1

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!