Skip to content

Set custom initialization for record members #77

Answered by edubart
stefanos82 asked this question in Q&A
Discussion options

You must be logged in to vote
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 : type in type declarations to be more explicit that it's a type declaration and not a runtime variable declaration, thus avoiding confusion:

local Person: type = @record{
    name: string = "stefanos82",
    age: integer
}

So, is record behaving like Java's interface in a way?

No, a record is more like struct in…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@stefanos82
Comment options

Answer selected by edubart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants