开发者

Adding a type hint to a Clojure gen-class state access

开发者 https://www.devze.com 2023-03-19 01:52 出处:网络
I am writing a Clojure class using gen-class. I have a \"state\" field that I am trying to access in the following code:

I am writing a Clojure class using gen-class. I have a "state" field that I am trying to access in the following code:

(ns com.example.sparetime.DateButton
  (:gen-class
    :extends javax.swing.JToggleButton
    :init initialize
    :state state
    :constructors {[Integer] [String]})
  (:import
    [java.awt Insets]))

(if *compile-files*
  (set! *warn-on-reflection* true))

(def insets (Insets. 1 1 1 1))

(defn -initialize [day-number]
  [[(Integer/toString day-number)] day-number])

(defn get-day-number [this]
  (.state this))

(defn -getInsets [this]
  insets)

I get a reflection warning on the line containing (.state this). I tried adding a type hint with (.state ^D开发者_运维百科ateButton this), but then I get a compilation error that the compiler is unable to resolve DateButton (chicken and egg? Datebutton does not exist until the compiler finishes compiling this file).

Never mind the wisdom of type hinting this code. How do I do it? Do I need to fully qualify the DateButton package?


You have to import your class or fully qualify its name. Then the ^DateButton hint will work and eliminate the reflection.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号