Skip to content

Implement opImplicitCast#22753

Open
IDONTUSEGH wants to merge 2 commits intodlang:masterfrom
IDONTUSEGH:opImplicitCast
Open

Implement opImplicitCast#22753
IDONTUSEGH wants to merge 2 commits intodlang:masterfrom
IDONTUSEGH:opImplicitCast

Conversation

@IDONTUSEGH
Copy link
Copy Markdown
Contributor

@IDONTUSEGH IDONTUSEGH commented Mar 18, 2026

This PR implements opImplicitCast, a user defined implicit conversion operator inspired by OpenD. After reviewing past forum discussions, I found this idea has been proposed before.

With opImplicitCast, we can implement #22715 entirely in user code (with some limitations). The same mechanism also enables the Result/Option/Maybe pattern:

(This was suggested here: https://forum.dlang.org/post/gkqthkjhmjngnqkjzwya@forum.dlang.org)

void main()
{
    // Pattern: Check and unwrap in one operation
    Result!int result = Result!int(42);

    // opCast!bool checks if value exists
    // opImplicitCast extracts the value
    if (result) {
        int value = result;  // Implicitly unwraps via opImplicitCast!int
        assert(value == 42);
    }

    // Empty result
    Result!int empty;
    assert(!empty);  // opCast!bool returns false

    // Can also use in conditions with auto
    Result!string strResult = Result!string("hello");
    if (strResult) {
        string s = strResult;
        assert(s == "hello");
    }
}

// Result/Option/Maybe pattern
struct Result(Type)
{
    private {
        Type _value;
        bool _hasValue;
    }

    this(Type value) {
        _value = value;
        _hasValue = true;
    }

    // Check if value exists (for if conditions)
    bool opCast(T)() const if (__traits(isSame, T, bool))
    {
        return _hasValue;
    }

    // Unwrap the value via implicit cast
    T opImplicitCast(T)() if (__traits(isSame, T, Type))
    {
        if (!_hasValue)
            assert(false, "Attempting to unwrap empty Result");
        return _value;
    }

    T opImplicitCast(T)() const if (__traits(isSame, T, Type))
    {
        if (!_hasValue)
            assert(false, "Attempting to unwrap empty Result");
        return _value;
    }
}

@dlang-bot
Copy link
Copy Markdown
Contributor

Thanks for your pull request and interest in making D better, @IDONTUSEGH! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#22753"

@IDONTUSEGH
Copy link
Copy Markdown
Contributor Author

    if (int value = result) {
        assert(value == 42);
    }

This syntax would be better but I couldn't figure out how to implement yet.

@rikkimax
Copy link
Copy Markdown
Contributor

Unfortunately opImplicitCast is a long-standing feature request that Walter has been strongly against, due to experience with C++.

The unwrapping action to pair it with the check is actually pretty simple to implement: https://github.com/dlang/dmd/pull/22570/changes#diff-11fb0212c55c05ed6e743e6776abb3c56996381760c8725255c622f96f5d9d4bR1705

My PR has been changed from the DIP for it, after feedback that people wanted it to be more explicit. It now requires a call to opUnwrapIfTrue to trigger the rewrite.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants