Skip to content

support C++17 std::optional in Embind #11139

Closed

Description

@skonstant

I would find it useful to allow class properties and method parameters to be optional, like in WebIDL.

I use std::optional a lot and that would seem like a natural thing do. As this is taken care of in WebIDL

something like this:

template
struct OptionalAccess {
static emscripten::val value(
const OptionalType& v) {
return emscripten::val(v.value());
}

static bool has_value(OptionalType& v) {
    return v.has_value();
}

};

template
emscripten::class_<std::optional> register_optional(const char* name) {
typedef std::optional OptionalType;

return emscripten::class_<std::optional<V>>(name)
        .function("value", &OptionalAccess<OptionalType>::value)
        .function("has_value", &OptionalAccess<OptionalType>::has_value);

}

but it's cumbersome to register a new optional type, can we not have something generic?

Activity

  1. skonstant commented on Jan 19, 2021

    @skonstant
    Author

    Any progress on this?

    I have done Typescript "bindings" for this now:

    export abstract class Optional<T> { has_value(): boolean; value(): T; delete(); }

    and

    export class OptionalString extends Optional<string> {}

    But again it is pretty poor, it would be much better to have undefined or null or the value, this way we could use it as falsy.

  2. stale commented on Apr 16, 2022

    @stale

    This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

  3. mrroohian commented on Aug 21, 2023

    @mrroohian
    Contributor

    For anyone still interested I've a workaround here: #20082 (comment)

  4. brendandahl commented on Jan 24, 2024

    @brendandahl
    Collaborator

    Fixed by #21076

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions