modular / modular/modular

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

Open
#6,458 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement neo-driver old-driver
Dominant language
Mojo
Stars
29.8k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the public DeviceContext interface and trace the internal handle representation referenced in the request. Define the public comparison behavior so instances compare by the same underlying runtime context rather than device ID; done means the API exposes this distinction without relying on private members.

Written by the indexing model from the issue text.

Assessment

Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.