Type Inference
Type inference is a powerful feature in Filtrera that allows the system to automatically determine the types of symbols, function/filter parameters, and program parameters. This simplifies your code, making it more readable and easier to write. This article will explain how type inference works in Filtrera, its use cases, and how it can benefit you.
What is Type Inference?
Type inference means that Filtrera can automatically deduce the type of a symbol, parameter, or expression based on its value or usage. Instead of explicitly specifying the type, you can let Filtrera infer it, reducing the amount of code you need to write and maintain.
Example
Consider the following example where a symbol’s type is inferred:
let age = 30
In this case, age
is inferred to be of type number
because it is assigned the value 30
.
Type Inference for Symbols
Symbols in Filtrera can have their types inferred from their initial values. This is useful for variables that are initialized with a specific value.
Example
let name = 'Alice'
Here, the type of name
is inferred to be text
because it is assigned the string "Alice"
.
Function and Filter Parameters
Function and filter parameters can also benefit from type inference. When the types of parameters are not explicitly defined, Filtrera infers them from the context in which they are used.
Example
let largeNumbers (collection) |> collection where n > 20
from [1, 20, 30] largeNumbers
In this example, the parameter collection
in the filter function largeNumbers
is inferred to be a collection of numbers because it is used with the condition n > 20
.
Program Parameters
Program parameters can have their types inferred if they are assigned default values. This makes your program definitions cleaner and more concise.
Example
param age = 25from age * 2
Here, the type of age
is inferred to be number
because it is assigned the value 25
.
If a parameter has a default value and a more general type is desired, you can explicitly specify the type:
param data: text | number = 'default'
In this case, data
can be either text
or number
, and the default value is "default"
.
Record Fields
Filtrera can also infer types for record fields based on the properties accessed within a function. This is particularly useful when working with records that have known structures.
Example
let func(rec) => rec.total
In this example, the parameter rec
is inferred to be an record with a property total
because the function accesses rec.total
.
Benefits of Type Inference
- Reduced Code Complexity: Type inference reduces the need for explicit type declarations, making your code simpler and more concise.
- Improved Readability: With fewer type annotations, your code becomes easier to read and understand.
- Ease of Maintenance: Less code means fewer opportunities for errors and easier maintenance.
- Flexibility: Type inference allows for more flexible and adaptable code, as types are determined based on usage and context.
Best Practices
- Leverage Defaults: Use default values to let Filtrera infer types whenever possible.
- Explicit When Needed: Explicitly define types when the inferred type does not match the desired type, especially for more complex or general types.
- Consistent Naming: Use clear and descriptive names for symbols and parameters to enhance readability and understanding.
Summary
Type inference in Filtrera is a valuable feature that simplifies your code by automatically determining the types of symbols, function/filter parameters, program parameters, and record fields. By understanding and utilizing type inference, you can write cleaner, more readable, and easier-to-maintain code. Whether you’re defining symbols, creating functions, setting up program parameters, or working with records, type inference helps you focus on the logic of your code rather than the details of type declarations.