Type Aliases experimental
In this guide, we will cover type aliases in Vein.
Type aliases allow you to create new names for existing types, which can make your code more readable and easier to maintain.
Local Type Aliases
Local type aliases are visible only within the module where they are declared.
Syntax
alias typeAlias <| Int32;
Example
alias Age <| Int32;
class Person {
age: Age;
new(age: Age) {
this.age = age;
}
}
auto person = Person(25);
Out.println("Age: " + person.age); // Output: Age: 25
Global Type Aliases
Global type aliases are visible across different modules.
Syntax
global alias typeAlias <| Int32;
Example
global alias Age <| Int32;
class Person {
age: Age;
new(age: Age) {
this.age = age;
}
}
auto person = Person(30);
Out.println("Age: " + person.age); // Output: Age: 30
Attention!
Currently global type alias is disabled for using in user modules (only in system modules)
Function Aliases
Function aliases allow you to create new names for function signatures.
Local Function Aliases
Local function aliases are visible only within the module where they are declared.
Syntax
alias fnAlias <| (i: i32): void;
Example
alias PrintFunction <| (i: i32): void;
fnAlias printNumber;
printNumber = (i: i32): void -> {
Out.println("Number: " + i);
};
printNumber(42); // Output: Number: 42
Global Function Aliases
Global function aliases are visible across different modules.
Syntax
global alias fnAlias <| (i: i32): void;
Example
global alias PrintFunction <| (i: i32): void;
fnAlias printNumber;
printNumber = (i: i32): void -> {
Out.println("Number: " + i);
};
printNumber(42); // Output: Number: 42
Conclusion
Type aliases in Vein provide a powerful way to create more readable and maintainable code. Whether you are creating local or global aliases for types or functions, understanding how to use these aliases can help you better organize your codebase.