Fork me on GitHub
#java
<
2023-10-09
>
Noah Bogart15:10:33

is it possible to monkey-patch classes in java? (I don't actually want to do this, I'm asking for related reasons)

hiredman16:10:31

depends what you mean by monkey patch

hiredman16:10:44

you can access private members and set private fields

hiredman16:10:26

you cannot add new members

Noah Bogart16:10:00

If the clojure compiler knows the type of a given object (symbol or literal), is it possible for there to be fields it can't know from using reflection between emitting the compiled code and run-time?

hiredman17:10:39

the trivial version is if you aot compile clojure with type hints and using version N of some java library, and then when you run that aot'ed code version N+1 where whatever class has changed

👍 1
Noah Bogart17:10:50

I'm thinking about https://ask.clojure.org/index.php/13339/tools-better-detect-errors-coming-existing-methods-fields and wondering if there are cases where the compiler could be wrong in some way. for example, if given a java.lang.String, can the compiler say "you've asked for .a, that doesn't exist on java.lang.String, compiler error (like that of javac itself)"?

hiredman17:10:43

it is very complicated

Noah Bogart17:10:12

ostensibly, at the time of compilation, if the field or method doesn't exist, then it would be an error to run code that accesses that field or method (with the java and class versions used at compilation). in some future, maybe a new version of java or class is used and now that field or method is available, but should we be building code to allow that?

hiredman17:10:18

when you type (.a "") at the repl sure, everything is evident and execution immediately follows compilation

hiredman17:10:18

if you have (defn f [] (.a "")) and aot compile, the (.a "") is compiled but not executed, and who knows, maybe when it is executed String will have an 'a' member

hiredman17:10:21

it is an open world vs. closed world assumption, and am pretty sure rich is way into open worlds

👍 1
Noah Bogart17:10:43

yeah i had a feeling about that

Noah Bogart17:10:58

why i didn't post in the ask lol. trying to learn as much first before i say anything

hiredman17:10:32

the ask is somewhat confusingly stated, but I don't think is really about this at all

hiredman17:10:57

it is about the kind of exception that gets thrown when a reflective call fails to find a method

Noah Bogart17:10:59

my question and thought line is related

Noah Bogart17:10:09

i agree with alex about cases when we don't know the type of the object being accessed, but it felt like if we're given an object with a defined type, we should be able to know up front, such as cases of methods on literals

hiredman17:10:54

it is pretty clear that a decision has been made not to do that

hiredman17:10:06

that is basically the behavior of javac

hiredman17:10:53

if some method isn't there at compile time, javac won't compile it as a unknown reflective access

Noah Bogart17:10:02

Right, but only for objects with known types.

Noah Bogart16:10:00

If the clojure compiler knows the type of a given object (symbol or literal), is it possible for there to be fields it can't know from using reflection between emitting the compiled code and run-time?