Set custom initialization for record members #77
-
Why can't we set the default values ourselves upon need? local Person = @record{
name: string = "stefanos82",
age: integer
}
local person: auto = Person{}
print('Name:', person.name) -- should print Name: stefanos82
print('Age:', person.age) -- should print 0 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Ah because we can do it like this -- typed initialization
local a: Person = {name = "Mark", age = 20}
print(a.name, a.age) So, is |
Beta Was this translation helpful? Give feedback.
-
local Person = @record{
name: string = "stefanos82",
age: integer
} This is a type declaration, not a runtime variable, and types in Nelua cannot have default initialization, this is a design decision. To initialize a variable with custom values you should use a function or initialize like you mentioned later: local a: Person = {name = "Mark", age = 20} I suggest to add local Person: type = @record{
name: string = "stefanos82",
age: integer
}
No, a |
Beta Was this translation helpful? Give feedback.
This is a type declaration, not a runtime variable, and types in Nelua cannot have default initialization, this is a design decision. To initialize a variable with custom values you should use a function or initialize like you mentioned later:
I suggest to add
: type
in type declarations to be more explicit that it's a type declaration and not a runtime variable declaration, thus avoiding confusion:No, a
record
is more likestruct
in…