Git Product home page Git Product logo

Comments (6)

jgpc42 avatar jgpc42 commented on May 24, 2024

I see Clojure's built-in munge doesn't handle dot. Considering most other valid keyword characters like 🐦 are also left intact by it, the only way that would allow for arbitrary (Clojure) names for Java identifiers would be something like:

(defn identifier-name [s]
  (clojure.string/replace
   (-> s name munge)
   #"^\d|[^\p{L}\p{N}_]" ;; etc.
   (fn [m]
     (->> (.codePointAt m 0)
          (Character/toChars)
          (map (comp (partial format "_u%04x") int))
          (apply str)))))

(identifier-name :manifold.speederino.bus-test)
;; => "manifold_u002espeederino_u002ebus_test"

(identifier-name :42🐦fredπ)
;; => "_u00342_ud83d_udc26fredπ"

Although, with that, it would sometimes make it hard to correlate generated names back to the original Clojure. Alternatively, I could just raise an error.

from jmh-clojure.

KingMob avatar KingMob commented on May 24, 2024

Well, I can say that personally, I would prefer names that are correlatable back, since I'm working on something where I won't always be able to control the original names.

from jmh-clojure.

KingMob avatar KingMob commented on May 24, 2024

(Sorry for the delay btw, I'm on vacation.)

from jmh-clojure.

jgpc42 avatar jgpc42 commented on May 24, 2024

(Sorry for the delay btw, I'm on vacation.)

No problem, and thanks for the additional input.

I was curious as to what Clojure does when it comes to generating class and method names. For example, in a test project:

$ mkdir -p classes demo
$ echo '{:paths ["." "classes"]}' > deps.edn
$ clj
(in-ns 'user)
(System/getProperty "java.version")
;; => "14.0.2"
*clojure-version*
;; => {:major 1, :minor 10, :incremental 3, :qualifier nil}

(munge '&)
;; => _AMPERSAND_

Somewhat surprisingly, Clojure can generate classes with names that are invalid according to javac. For example:

(spit "./demo/&.clj" (pr-str '(do (ns demo.& (:gen-class))
                                  (defn -main [& _] (prn :wow)))))
(compile 'demo.&)
;; => demo.&

(import [demo &])

(&.)
;; => #object[demo.& 0x178270b2 "demo.&@178270b2"]

If we look with javap, we see the same name:

$ javap 'classes/demo/&.class' | head -n1
public class demo.& {

But if we try to create a class with the same name with javac it (expectedly) fails:

$ echo 'public class & {}' > 'demo/&.java'
$ javac 'demo/&.java'
demo/&.java:1: error: <identifier> expected
public class & {}
            ^
1 error

But perhaps even more surprising, while javac can't create them, java can still run the ones generated by Clojure:

$ java -cp "$(clj -Spath)" 'demo.&'
:wow

However, while it can generate ostensibly invalid class names, it won't allow the same for methods.

(spit "./demo/test2.clj"
      (pr-str '(do (ns demo.test2 (:gen-class :methods [[& [] String]]))
                   (defn -& [] "???")
                   (defn -main [& _] (prn (-&))))))
(compile 'demo.test2)
;; Syntax error macroexpanding clojure.core/gen-class at (demo/test2.clj:1:5).
;; Not a valid method name: &

But, of course, it works fine from Clojure:

(require 'demo.test2 :reload)
(demo.test2/-main)
;; "???"

Anyway, I hope you don't mind the small brain dump.

So, I think raising an error if the munged name is still an invalid Java method name is the right course of action. This will be less restrictive than Clojure's gen-class, which doesn't allow even munge-able characters like -, but it still will disallow dot as a valid character for benchmark names.

If you don't have any more input or objections, I'll likely get to enforcing this in the code some time next week.

from jmh-clojure.

jgpc42 avatar jgpc42 commented on May 24, 2024

Added in ca59c72.

from jmh-clojure.

KingMob avatar KingMob commented on May 24, 2024

from jmh-clojure.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.