Variables

fastn has support for rich data modelling, and it supports declaring variables.

A variable is a named storage that programs can manipulate.
-- integer x: 20
Lang:
ftd
Variables have types.

Types

The data type is mandatory while declaring a variable in fastn.

Type of a variable can be one of the built-in types, a record, or an or-type.

Immutable

By default, variables are immutable − read only in fastn. In other words, the variable’s value cannot be changed once a value is bound to a variable name.
-- integer x: 10

-- ftd.integer: $x
$on-click$: $ftd.increment($a = $x)
Lang:
ftd
The output will be as shown below:
Cannot have mutable reference of immutable variable `x`
Lang:
txt
The error message indicates the cause of the error - Cannot have mutable reference of immutable variable x. This is one of the many ways fastn allows programmers to write code and takes advantage of the safety.

Mutable

Variables are immutable by default. Prefix the variable name with $ to make it mutable. The value of a mutable variable can be changed.

The syntax for declaring a mutable variable is as shown below −
-- integer $x: 10

-- ftd.integer: $x
$on-click$: $ftd.increment($a = $x)
Lang:
ftd
The output will be as shown below: Click on 10.
Output
10

Referring To Another Variable

A variable can be defined as referring to another variable using $ as a prefix of referenced variable, $<referenced variable>. This means if the referenced variable get changed the referer will change too.

When you define a variable in fastn, you can make it refer to another variable by adding a $ sign before the referenced variable’s name, like $<referenced variable>. This basically means that if the referenced variable gets updated or changed, the referring variable will also change accordingly.

For example, let’s say you have an integer variable called x with a value of 10, and you define another integer variable called y with $x as its value. Now, y is the referring variable and x is the referenced variable. So, if you update or change the value of x, the value of y will also change.
-- integer $x: 10

-- integer y: $x

-- ftd.integer: $x

-- ftd.integer: $y

-- ftd.text: I change x
$on-click$: $ftd.increment($a = $x)
Lang:
ftd
Give it a try and click on “I change x” to see how x and y change together!
Output
x:
10
y:
10
I change x :)

Clone the value of a Variable

A value of the variable can be cloned by de-referencing the variable reference. This means that cloning creates a duplicate value and if the cloned value changes, the object, that clones it, will not change.
-- integer x: 10

-- integer y: *$x

-- ftd.text: I change x :)
$on-click$: $ftd.increment($a = $x)
Lang:
ftd

Here, if x changes, y doesn’t changes.

The output will be as shown below: Click on I change x :) to see the result.
Output
x:
10
y:
10
I change x :)

Updating a Variable

Once a mutable variable has been defined it can be updated too. Any variable can be made mutable by prefixing it with $.

Note: By default, fastn variables are immutable (can’t be changed once initialized).
-- integer $x: 10

-- $x: 20
Lang:
ftd
The type of the variable can not be updated.

$processor$: dynamic variables

fastn documents are processed in the context of a “platform”, and platform can provide access to dynamic variables.

Say platform has provided a dynamic variable os, which is the operating system on which this document is getting rendered, you can use that like this:
-- string name-of-os:
$processor$: os
Lang:
ftd

type is mandatory when using $processor$. Available processors would be documented as part of platform documentation.

Processors can also look at data passed, so its possible to create a processor:
-- string greeting: hello, world
$processor$: uppercase
Lang:
ftd
Say the platform has provided a processor uppercase, which takes the current value, hello, world and returns its upper case value. In this case the variable greeting will hold the value: HELLO, WORLD.

Foreign variables

Like $processor$, the platform provides foreign variables against a module. The fastn stuck itself to fetch value of foreign value, which is, then, provided by platform.

The most common foreign variable is assets.
-- import: module/assets

-- ftd.text: Check bar content
link: $assets.files.some-folder.bar.ftd
Lang:
ftd

The files field in assets variable gives url to files present in package. So, $assets.files.some-folder.bar.ftd returns url of <path-to-package>/some-folder/bar.ftd as value.

It’s not so for image file.
-- import: module/assets

-- ftd.image:
src: $assets.files.images.my-image.png
Lang:
ftd

$assets.files.images.my-image.png gives url of <path-to-package>/images/my-image.png as value (for light field).

If an image with the same name but with -dark suffix exists in the package, i.e. <path-to-package>/images/my-image-dark.png, then dark field gets the url of this file.

Checkout src property and ftd.image-src type for more details.

Support fastn!

Enjoying fastn? Please consider giving us a star ⭐️ on GitHub to show your support!

Getting Help

Have a question or need help?

Visit our GitHub Q&A discussion to get answers and subscribe to it to stay tuned.

Join our Discord channel and share your thoughts, suggestion, question etc.

Connect with our community!

Found an issue?

If you find some issue, please visit our GitHub issues to tell us about it.

Join us

We welcome you to join our Discord community today.

We are trying to create the language for human beings and we do not believe it would be possible without your support. We would love to hear from you.
Copyright © 2023 - FifthTry.com