-
-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support clj instance field access without hyphen #714
Comments
Is this only a problem in the tests? Is there a reason why those tests didn't use |
I'm sorry - I should've made the actual location of the accesses clearer... The actual function implementations use the dot accessor in this way: and then the tests use (for example) the So the tests don't directly do the problematic access, but they call the functions that do the problematic access. This would mean that the library would be "incompatible" (used very loosely), because something calling As for the reason, I can only imagine that the code in question has been stable for a long time, maybe even before the hyphen for field access was added (that's purely speculative, of course), and just hasn't been updated. |
@bobisageek I "recently" (not sure how long ago but not too long ago) added support for looking up fields in classes. I decided to purely do this based on syntactic grounds to dispatch on whether to look up a field or a method. But there might be a way to support both without sacrificing too much performance. There is a path to do so here: I would use that path in sci.impl.interop if it weren't complicated by GraalVM reflection and type hints. But since SCI has its own version of clojure.lang.Reflector, named sci.impl.Reflector, we could maybe add the target class in there. Getting late here to dive into details, but these are some pointers. |
I'll at least take a look and see how lost I get. :) |
The reason we use a different path than Clojure for implementing reflection is that the reflection information in a native image can be limited or even absent. So we're using type hints (or a function called |
@bobisageek One thought I've been having lately is that SCI could support |
A thing I was playing with (and thinking about if there was a better approach) was to have sort of a special case in Lines 38 to 39 in e8aa6cf
to something like: (let [methods (Reflector/getMethods target-class (count args) method false)]
(if (and (empty? args) (empty? methods))
(invoke-instance-field obj target-class method)
(Reflector/invokeMatchingMethod method methods obj (object-array args)))) In my mind, this would be somewhat analogous to the functionality implemented in The plus is that dot access on instance members would "just work" like clojure. My primary concern with that bit is how to handle if I did go digging around into the |
That's certainly interesting! Minor thing: instead of |
PR welcome to explore that option. |
I'm experimenting with some tests for this (wrapping my head around the |
Is your feature request related to a problem? Please describe.
While working on adding
math.numeric-tower
lib tests to babashka (babashka/babashka#1237), a handful of tests failed becausenumerator
could not be found onclojure.lang.Ratio
. Upon further investigation, it appears that the root causes of the failure are a) theRatio
class doesn't have its methods/fields exposed in bb (orthogonal to this issue), and b) the 'dot' behavior differs slightly between Clojure/JVM and sci. Specifically related to this example, in Clojure/JVM, instance fields can be accessed without the beginning hyphen, and this isn't (as far as I can tell) doable with sci.Describe the solution you'd like
Based on my interpretation of the Clojure reference (https://clojure.org/reference/java_interop#dot), I'd say that, at least when hosted on Clojure/JVM, a two-argument call to the
.
special form, where the second argument doesn't begin with a hyphen, should invoke the method named by the second argument if there's a matching 0-arg method, or return the value of a public field if there's a matching field, or throw if there's no matching 0-arg method or public field.Describe alternatives you've considered
An alternative that addresses the proximate desire of including
math.numeric-tower
into babashka's lib tests would be to submit an issue/patch to the library itself to use field access syntax, e.g. to change(quot (. n numerator) (. n denominator))
to
(quot (. n -numerator) (. n -denominator))
in all the cases of instance field access.
Additional context
A small example to illustrate the behavioral difference, using the
numerator
public field onclojure.lang.Ratio
, with the current head revision of Sci:Clojure/JVM:
Sci (running on Clojure/JVM):
The text was updated successfully, but these errors were encountered: