Skip to content

[Feature Request] Add equality comparison for DeviceContext (not based on device ID alone) #6458

@ratulb

Description

@ratulb

Review Mojo's priorities

What is your request?

Problem
Currently, there is no built‑in way to check if two DeviceContext instances represent the same logical context (not just the same device ID).

Example:

var dc1 = DeviceContext(1)
var dc2 = dc1
var dc3 = DeviceContext(1)

dc1 and dc2 → same context (same instance)

dc1 (or dc2) and dc3 → different contexts, even though device ID (1) is identical

Comparing by device ID is insufficient because DeviceContext(1) can be called multiple times, potentially returning distinct objects that are not equivalent.

Workaround (hacky)
One hacky option is:

var same_context = dc1._handle.value() == dc2._handle.value()

This compares the internal handles, but it relies on implementation details and is not recommended for production code.

Request

Please add a proper equality API for DeviceContext in the public interface, such as:

dc1 == dc2 operator overload

dc1.equal(dc2) method

Or explicit def is_same_context(other: DeviceContext) -> Bool

The equality should be defined as same underlying runtime context (i.e., same internal handle), not just same device ID.

This would make the API more robust and avoid reliance on private members.

Additional context
This was identified as a missing feature by a Mojo team member.

Thanks

What is your motivation for this change?

Thanks

Any other details?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssue needs to be routed/triaged to a particular team stillenhancementNew feature or requestmojoIssues that are related to mojo

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions