Keyword: is
The with
keyword in Filtrera is used to create a clone of an record with one or more fields overridden. This reference guide provides a detailed specification of the with
keyword, focusing on its syntax and usage.
The with
keyword allows you to create a new record based on an existing one, with specified fields updated to new values. This cloning mechanism ensures immutability while providing flexibility to modify record fields. Fields can also change type when using the with
keyword.
Syntax
Basic Syntax
<new_record> = <existing_record> with { <field1> = <new_value1>, <field2> = <new_value2>, ... }
<new_record>
: The new record created by cloning the existing record.<existing_record>
: The record to be cloned.<field>
: The field to be overridden in the new record.<new_value>
: The new value for the overridden field.
Example
let originalPerson = { name = 'Alice', age = 30 }let updatedPerson = originalPerson with { age = 31 }
from updatedPerson
In this example, updatedPerson
is a clone of originalPerson
with the age
field updated to 31
.
Detailed Syntax
Cloning with Overridden Fields
You can clone an record and override one or more fields to create a new record.
let car = { make = 'Toyota', model = 'Corolla', year = 2020 }let newCar = car with { year = 2021 }
from newCar
In this example, newCar
is a clone of car
with the year
field updated to 2021
.
Changing Field Types
Fields in the new record can also change type when using the with
keyword.
let book = { title = 'Filtrera Guide', pages = 300 }let updatedBook = book with { pages = 'Three Hundred' }
from updatedBook
In this example, updatedBook
is a clone of book
with the pages
field changed from a number
to a text
.
Cloning with Multiple Field Changes
You can override multiple fields simultaneously.
let employee = { name = 'John', position = 'Developer', salary = 50000 }let updatedEmployee = employee with { position = 'Senior Developer', salary = 60000 }
from updatedEmployee
In this example, updatedEmployee
is a clone of employee
with both the position
and salary
fields updated.
Practical Usage
Example: Updating Nested Records
The with
keyword can be used to update fields in nested records.
let company = { name = 'Tech Corp', location = 'New York', ceo = { name = 'Alice', age = 45 }}
let updatedCompany = company with { ceo = company.ceo with { age = 46 }}
from updatedCompany
In this example, updatedCompany
is a clone of company
with the age
field of the ceo
record updated to 46
.
Example: Creating Variations of an Record
The with
keyword is useful for creating variations of an record without modifying the original.
let product = { name = 'Laptop', price = 1000, inStock = true }let discountedProduct = product with { price = 900 }let outOfStockProduct = product with { inStock = false }
from discountedProductfrom outOfStockProduct
In this example, discountedProduct
and outOfStockProduct
are variations of product
with different field values.
Summary
The with
keyword in Filtrera provides a powerful mechanism for cloning records with overridden fields, ensuring immutability while allowing flexibility to modify record properties. By understanding its syntax and usage, including changing field types and updating nested records, you can leverage the with
keyword to create robust and flexible programs.