Fork me on GitHub
#clojure
<
2024-04-20
>
Carsten Behring22:04:26

I have this java class;

public static Formula of(String response, String ... predictors) {
        return new Formula((Term)new Variable(response), (Term[])Arrays.stream((Object[])predictors).map(predictor -> {
            if (predictor.equals((Object)".")) {
                return new Dot();
            }
            if (predictor.equals((Object)"1")) {
                return new Intercept(true);
            }
            if (predictor.equals((Object)"0")) {
                return new Intercept(false);
            }
            return new Variable(predictor);
        }).toArray(Term[]::new));
    }

    public static Formula of(String response, Term ... predictors) {
        return new Formula((Term)new Variable(response), predictors);
    }

    public static Formula of(Term response, Term ... predictors) {
        return new Formula(response, predictors);
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public static Formula of(String s) {
and cannot call the last function via (Formula/of "my string"), it fails with
No matching method of found taking 1 args for class smile.data.formula.Formula
Is there some trick to call it or is it confused by the existence of an other variadic method: public static Formula of(String response, String ... predictors)

hiredman23:04:32

Variadic methods in java compile to methods that take an array, so to call them from clojure you have to pass an array

seancorfield23:04:18

You're not calling it with a literal tho', right?

(~/clojure)-(!2007)-> clj -Sdeps '{:deps {com.github.haifengl/smile-core {:mvn/version "3.1.0"}}}'
Clojure 1.12.0-alpha9
user=> (import '(smile.data.formula Formula))
smile.data.formula.Formula
user=> (Formula/of "my string")
SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See  for further details.
Execution error (IllegalArgumentException) at smile.data.formula.Formula/of (Formula.java:230).
Invalid formula: my string
user=>
Can you share the actual code you're trying to run?

seancorfield23:04:41

(I tried Clojure 1.9, 1.10, and 1.11, in addition to 1.12 and can't repro your reflection failure @U7CAHM72M)

hiredman00:04:46

The single string method was added in 3.1.0, so maybe make sure the version of the library being used matches the source you are looking at

seancorfield00:04:34

Did you mean 3.0.0? Confirmed that it fails in 2.6.0 but works in 3.0.0:

(~/clojure)-(!2014)-> clj -Sdeps '{:deps {com.github.haifengl/smile-core {:mvn/version "2.6.0"}}}' -M -e "(import '(smile.data.formu
la Formula))(Formula/of (str 'my_string))"
smile.data.formula.Formula
Syntax error (IllegalArgumentException) compiling Formula/of at (REPL:1:39).
No matching method of found taking 1 args for class smile.data.formula.Formula

Full report at:
/tmp/clojure-1523568482288008227.edn

Sat Apr 20 17:27:44
(~/clojure)-(!2015)-> clj -Sdeps '{:deps {com.github.haifengl/smile-core {:mvn/version "3.0.0"}}}' -M -e "(import '(smile.data.formu
la Formula))(Formula/of (str 'my_string))"
smile.data.formula.Formula
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See  for further details.
Execution error (IllegalArgumentException) at smile.data.formula.Formula/of (Formula.java:230).
Invalid formula: my_string

Full report at:
/tmp/clojure-11804444325394978125.edn

Sat Apr 20 17:27:58
(~/clojure)-(!2016)->

oyakushev08:04:52

@U064X3EF3 If I remember correctly, Clojure 1.12 has plans to address Java variadic methods, right? If not, I think it would at least deserve a more helpful hand-crafted error message because such questions pop up quite often.

Alex Miller (Clojure team)11:04:32

We punted varargs work out of 1.12. I’m confused by the thread above what the actual problem is here

1
seancorfield18:04:53

@U064X3EF3 I strongly suspect that @U7CAHM72M has the 2.6.0 version of Smile on their classpath but is looking at the 3.x source and expecting to be able to call of(String s) which doesn't exist in 2.6.0. So the error they're getting is correct for the version they are using.

Carsten Behring20:04:31

Yes, indeed. sorry for confusion. I had 2.6.0 in Classpath, but Emacs/Cider jumped somehow to 3.0.0 source code when expecting the source from Emacs.