First stab at Err#typeError in api#8168
Merged
enebo merged 13 commits intojruby:9.5-devfrom Apr 25, 2024
Merged
Conversation
I realized having throw be in the error methods had too many tradeoffs.
Beyond Java not being able to realize those methods unconditionally
throws (making some return null; // not reached lines). It also lacks
the ablity to compose:
```java
withException(context, typeError(context, a, "Array"), e);
```
Doing with this typeError raising is not possible and you would have
to either not have composable methods or make a combinatorial set of
methods which do the same thing. This commit also has some oddities
for the new castTo but I think there are ok.
The first one is castTo is effectively replacing:
```java
if (a instanceof RubyArray) throw TypeError(context, a, "Array");
RubyArray b = (RubyArray) a;
```
with:
```java
RubyArray b = castToArray(context, a);
```
Looks nice but:
```java
RubyArray b = castToArray(getRuntime.getCurrentContext(), a);
```
Notice we pay price of looking up context when we may not do that in
the original snippet? I think worst-case you can still do the original
snippet but in normal cases we continue to work on propagating ThreadContext
everywhere and this again will be no cost.
The second oddity is:
```java
if (a instanceof RubyArray) throw TypeError(context, a, str(runtime, ...));
RubyArray b = (RubyArray) a;
```
This form is accepted for castTo as well:
```java
RubyArray b = castToArray(context, a, str(runtime, ...));
```
The problem here is that str is executed every time. So this form of
castToXXX has a warning in javadocs but it is a potential foot gun. There
are enough simple string forms where I think this is worth having but it
is undesirable.
I have been considering whether we could pass a lambda for the String part
but that makes me think it would generate a lot more code.
One last observation is that Java instanceof pattern variables encourages
a new idiom of writing code and I am unsure how I feel about it. Consider
this snippet:
```java
void put(ThreadContext context, AbstractMemory ptr, long offset, IRubyObject value) {
if (!(value instanceof Struct)) throw typeError(context, "expected a struct");
Struct s = (Struct) value;
byte[] tmp = new byte[Struct.getStructSize(context.runtime, s)];
s.getMemoryIO().get(0, tmp, 0, tmp.length);
ptr.getMemoryIO().put(offset, tmp, 0, tmp.length);
}
```
With pattern var:
```java
void put(ThreadContext context, AbstractMemory ptr, long offset, IRubyObject value) {
if (value instanceof Struct s) {
byte[] tmp = new byte[Struct.getStructSize(context.runtime, s)];
s.getMemoryIO().get(0, tmp, 0, tmp.length);
ptr.getMemoryIO().put(offset, tmp, 0, tmp.length);
return;
}
throw typeError(context, "expected a struct");
}
```
Arguably they are both reasonable ways to write but the pattern var gives
the sense that we are performing a safe cast in a way the compiler will
catch. The other way is rare to get wrong and it is less weird to see
an error check before the normal path vs the error path appearing to be the
non-special path. At the end I realized this pattern var will transform
idiomatic Java code to be written using this newer syntactical feature.
We may as well embrace it (sans nice castToXXX methods to hide our casting).
This commit also does a fair amount of style tweaks. A big part of this new
API work is to see how it "feels". That lead to some refactoring to help
learn whether the API feels good to me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First stab at Err#typeError in api
The basic idea is that there is a series of static methods for raising errors in a single class. They will be:
The first one I hit was TypeError and specifically call sites which were manually building up a normal string. typeError
will:
This last point is probably the most contentious as Java type system gets confused by unconditional throwing methods and if this happens to be last statement in places it will argue a return is missing. My belief is this is an ok tradeoff. We have had dozens of errors in this project where someone made the error but did not actually throw it. The second point is we do not tend to ever make an exception and not immediately throw.
If this is good then I will convertall other TypeError callers from Ruby and deprecate those methods. Additionally, I think we should backport this to 9.4 (and possibly 9.3). The benefit is we can update native extensions at some future point to use these and then force a version requirement at a point forward. This should give us confidence that we can remove the original methods in a later release.