You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When writing some code that does not have a specific .NET type, or if the linker removes the .NET type because it is not used, there is no way to check to see if the type is implementing an interface or has a specific base type.
For example, I am using Glide to load a Drawable and then I want to see if it implements the IAnimatable interface. In debug builds this works because the AnimationDrawable type happens to be in the .NET type system. When the linker runs through the code, that type is never referenced so it is removed. This causes the runtime behavior to be wrong for an if block like this:
if(myDrawableFromJavaisIAnimatable)
Similarly, if I have a slim binding (or chose not to bind everything) then the .NET type will never exist at all.
The app still runs because it uses the DrawableInvoker type to do things. This invoker does not have the interface to the .NET type check fails.
To get around this issue, I want to use the JavaCast<T>() extension method, but that now throws an exception if the type is not what I am casting.
Is there an overload or alternative that will allow be to check the types on the Java side to see if an interface or base type is valid and then return the instance that is of that type?
@jpobst: I agree with your thoughts on the "Try" pattern; TryJavaCast<T>() having a non-bool return type feels wrong.
We could alternatively do .JavaAs<T>() for "symmetry" with the as operator.
Usage-wise, I think bool-returning TryJavaCast() might be better?
if(a.TryJavaCast<B>(outvarb)){// use `b`}// vsvarb=a.JavaAs<B>();if(b!=null){// use `b`}
If you're going to need an ifanyway, you might as well go with the bool TryJavaParse<T>() idea. However, if you want to use the null conditional operator or pattern matching, .JavaAs<T>() might be better:
a.JavaAs<B>()?.SomeMethodSpecificToB();if(a.JavaAs<B>()isBb){// … but why?!}
Maybe both Try and As variants?
We can also debate .AsJavaCast<T>() vs. .JavaAs<T>(). .JavaAs<T>() came to mind earlier todays, before I had a chance to elaborate. However, .JavaAs<T>() might be "too cute".
Activity
jonpryor commentedon Jun 27, 2024
I like this idea, and we should do it in dotnet/java-interop as an extension method on
IJavaPeerable. :-)jpobst commentedon Jun 27, 2024
This probably merits some discussion on what the API should look like. To fit the standard "Try" pattern, the API would be:
If we want to simply return
T?, perhaps we should call itAsJavaCastto by symmetric with a C# "as" cast?jonpryor commentedon Jun 28, 2024
@jpobst: I agree with your thoughts on the "Try" pattern;
TryJavaCast<T>()having a non-boolreturn type feels wrong.We could alternatively do
.JavaAs<T>()for "symmetry" with theasoperator.Usage-wise, I think bool-returning
TryJavaCast()might be better?If you're going to need an
ifanyway, you might as well go with thebool TryJavaParse<T>()idea. However, if you want to use the null conditional operator or pattern matching,.JavaAs<T>()might be better:Maybe both
TryandAsvariants?We can also debate
.AsJavaCast<T>()vs..JavaAs<T>()..JavaAs<T>()came to mind earlier todays, before I had a chance to elaborate. However,.JavaAs<T>()might be "too cute".[Java.Interop] Add `.JavaAs()` extension method
[Java.Interop] Add `.JavaAs()` extension method
Bump to dotnet/java-interop@7a058c0e
Bump to dotnet/java-interop@7a058c0e (#9066)
[Java.Interop] Add `IJavaPeerable.JavaAs()` extension method (dotnet#…
[Java.Interop] Add `IJavaPeerable.JavaAs()` extension method (#1234)