Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values for struct fields #2936

Open
ogoffart opened this issue Jun 20, 2023 · 3 comments
Open

Default values for struct fields #2936

ogoffart opened this issue Jun 20, 2023 · 3 comments
Labels
a:language-slint Compiler for the .slint language (mO,bF) rfc Request for comments: proposals for changes

Comments

@ogoffart
Copy link
Member

ogoffart commented Jun 20, 2023

It is often requested to have default values for struct fields

It could look like this:

struct Player {
     score: int = -1,
     name: string = "John Smith",
}

Then, when initialized, or converted from a struct with less fields, the other fields are initialized with thei default value:

component X {
   // initialized to default values
   property <Player> player1;
   // score will be -1
   property <Player> player2: { name: "Alex" };
}

The Default implementation of the rust structure or C++ default constructor must initialize the fields accordingly.

@ogoffart ogoffart added the rfc Request for comments: proposals for changes label Jun 20, 2023
@tronical
Copy link
Member

Straightforward syntax, and very sensible. I'm much in favor.

@hunger
Copy link
Member

hunger commented Jun 21, 2023

I see myself mixing up : and = as we use : for assignments pretty much everywhere else.

Could we maybe do something like

struct Player {
     <int> score: -1,
     <string> name: "John Smith",
}

That is of course breaking the existing struct definition syntax... which I can remember well as "assigning a type to the field name":-)

@ogoffart ogoffart added the a:language-slint Compiler for the .slint language (mO,bF) label Jul 13, 2023
@Tmpod
Copy link
Contributor

Tmpod commented Apr 8, 2024

Yeah, there's a bit of a syntax conflict here. Using = in this case seems a bit out of place, but : clashes with the type specifier.
I wouldn't be against changing the struct field declaration to be more akin to property declaration, like @hunger suggested. It would make Slint's syntax more consistent overall, lowering the learning curve a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:language-slint Compiler for the .slint language (mO,bF) rfc Request for comments: proposals for changes
Projects
None yet
Development

No branches or pull requests

4 participants