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

Partial override fields of a struct #5201

Open
FloVanGH opened this issue May 8, 2024 · 2 comments
Open

Partial override fields of a struct #5201

FloVanGH opened this issue May 8, 2024 · 2 comments
Labels
a:language-slint Compiler for the .slint language (mO,bF) rfc Request for comments: proposals for changes

Comments

@FloVanGH
Copy link
Member

FloVanGH commented May 8, 2024

Check the following example:

export struct Style {
      background: brush,
      border-brush: brush
}

export component MyBase {
      in property <Style> style: {
              background: #000000,
              border-brush: #0000000
      };
      
      Rectangle {
          background: root.style.background;
          border-color: root.style.border-brush;
          border-width: 1px;
      }
}

export component MyWidget inherits MyBase {
      style: {
             background: #fffffff,
      };
}

In the example what I want for MyWidget is to set background to white and leave border-brush to black, as it is defined in MyBase. But currently it doesn't work. What happens instead is that background is set to black and all not set field of the structs are set to the defaults. So in case of border-brush to transparent.

@FloVanGH FloVanGH added rfc Request for comments: proposals for changes a:language-slint Compiler for the .slint language (mO,bF) labels May 8, 2024
@ogoffart
Copy link
Member

ogoffart commented May 8, 2024

The change as proposed is a breaking change.
We could come up with a new syntax for doing that. Like a "merge" operator.

So one option would be:

export component MyWidget inherits MyBase {
   // Option 1: have a . syntax to override only sub-field of a struct
   style.background: #fff;

   //Option 2: use a different symbol to merge
   style ~: { background: #fff } 

   // Also make sense with callback and stuff
   clicked => { root.style ~= { background: #fff } } 
}

I've used ~: and ~= as an example, but other symbols are possible (for example :~)

Note that with the option 2, the right hand side of this is NOT of type Style, but rather a struct type that has less fields. In particular, this would not work:

export component MyWidget {
   in property <Style> style;
   MyBase { 
      // This is like a normal binding since the right hand side type already have all the fields
      style ~: root.style; 
   }
}

export component App {
   MyWidget { 
      // This is not working as expected since the MyWidget's style don't have a binding
      style ~: { background: #fff }
   }
}

For that reason I think it's best to use option 1, which might already be enough.

@FloVanGH
Copy link
Member Author

I really like option 1: style.background: #ffffff.
It is easy to write, easy to memorize and consistent to

Rectangle {
      background: root.style.background;
}

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

2 participants