-
Notifications
You must be signed in to change notification settings - Fork 169
Description
Component(s)
No response
Is your feature request related to a problem? Please describe.
I noticed that ArrayBasedContext compares ContextKey instances using == instead of equals. This causes issues where keys with the same logical name cannot retrieve their corresponding values unless they are the same object reference. For example:
ContextKey<String> key1 = ContextKey.named("key");
ContextKey<String> key2 = ContextKey.named("key");
Context context = Context.root().with(key1, "value");
context.get(key2); // This returns null because key1 != key2.Additionally, when working with Context.current(), I encountered a scenario where a ContextKey created with ContextKey.named failed to retrieve its associated value when accessed in another part of the application:
// ContextKey created in one part of the application
ContextKey<String> key = ContextKey.named("key");
Context context = Context.current().with(key, "value");
// Later, attempting to retrieve the same key in another part of the application
String value = Context.current().get(ContextKey.named("key")); // This cannot find the valueIt seems this behavior stems from using == for key comparisons instead of equals.
Describe the solution you'd like
I would appreciate clarification on whether this behavior is an intentional design choice. If it is, could you provide insights into the reasoning behind this approach? Alternatively, if this is not intentional, I suggest updating the implementation to use equals for key comparisons, allowing logically equivalent keys to retrieve their values regardless of reference equality.
Describe alternatives you've considered
If the current behavior is intentional, an alternative would be to clearly document this design choice and provide guidance on how to ensure ContextKey instances are reused consistently to avoid issues caused by reference mismatches. However, considering the implementation uses ArrayBasedContext, switching to equals-based comparison could simplify usage and align with typical key-value store semantics, as ArrayBasedContext iterates through its entries rather than relying on reference equality for performance.
Additional context
This behavior was observed while attempting to verify and process logic based on whether a value was present in the Context using ContextKey.named at a specific location in the application. I encountered an issue where a ContextKey with the same name, but newly created in a different part of the application, was unable to retrieve the associated value from the Context.
While it is possible that I may be misusing the API, this issue appears to stem from the implementation of ArrayBasedContext, which compares ContextKey instances using == instead of equals. I would greatly appreciate it if you could clarify whether the use of == for ContextKey comparison is an intentional part of the design. If it is, a brief explanation of the reasoning behind this choice would be incredibly helpful.
Additionally, if this behavior was not intentional, I would like to inquire whether there are plans to update the implementation to use equals for comparisons or provide guidance on how to properly reuse ContextKey instances across an application to avoid such issues. Your input on this matter would be greatly appreciated.