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

Converting enum to string #6852

Closed
dorukotiv opened this issue Nov 20, 2024 · 4 comments
Closed

Converting enum to string #6852

dorukotiv opened this issue Nov 20, 2024 · 4 comments
Labels
enhancement New feature or request need triaging Issue that the owner of the area still need to triage

Comments

@dorukotiv
Copy link

dorukotiv commented Nov 20, 2024

Feature Description

Currently, an enum that is defined in slint can not be given to the Text component as an input, a callback needs to be implemented in Rust to do the conversion.

Ideally, I would directly use the enum text as an input to the text.

Example:

enum Direction {
Right,
Left
}

Text {
text : Direction.Right;
}

that would be seen as "Right" on the screen.

Product Impact

We are a railway company building Remote Supervision Control Desktop application for a locomotive.

It would reduce the code and time.

@dorukotiv dorukotiv added enhancement New feature or request need triaging Issue that the owner of the area still need to triage labels Nov 20, 2024
@tronical
Copy link
Member

I don't feel very comfortable with this. Other programming languages don't do this typically out of the box with enums and use reflection instead. And to make matters worse: this doesn't play well with translations.

Perhaps we need a way to more easily create mappings? Like a match on an enum that errors when not all variants are covered.

@ogoffart
Copy link
Member

(in case you didn't know, it is converting to string for debugging purpose using the debug(...) function to print value in the console)

You can use @rust-attr to derive from one of the crate that provide enum-to-string conversion in rust for the exported rust enum.

But I agree with @tronical that this is not necessarily a good idea to have that in Slint itself.

@tronical
Copy link
Member

I suggest we don't implement this. But I'd like the ability for user to write something like this:

function direction-string(dir: Direction): string {
    match dir {
        Direction.Left => "left",
        // compiler errors out, because `Right` isn't handled here
    }
}

@ogoffart
Copy link
Member

We already have a tracking issue for a match or switch: #1307

In the mean time, it can be done with something like:

function direction-string(dir: Direction): string {
    return dir == Direction.Left ? "left"
         : dir == Direction.Right ? "right"
         : ""; // unfortunately no errors/warnings at this point
    }
}

or in rust with a callback that does to-string based on @rust-attr(derive(strum_macros::Display))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request need triaging Issue that the owner of the area still need to triage
Projects
None yet
Development

No branches or pull requests

3 participants