2016-02-05 50 views
2

爲了註釋clojang,我在調用重載Java方法的協議方法上遇到了一些麻煩。我從'clojang.jinterface.otp.nodes縮小問題到以下最低棘手的情況:如何正確註釋重載的Java方法?

(ns clojang.jinterface.otp.nodes 
    (:require [clojure.core.typed :as t :refer [ann ann-protocol]] 
      [clojure.core.typed.utils :refer [defprotocol]]) 
    (:import [com.ericsson.otp.erlang OtpTransport] 
      [java.net InetAddress]) 
    (:refer-clojure :exclude [defprotocol])) 

(ann-protocol AbstractNodeObject 
    create-transport 
    (t/IFn [AbstractNodeObject InetAddress int -> (t/Nilable OtpTransport)] 
     [AbstractNodeObject String int -> (t/Nilable OtpTransport)])) 
(defprotocol AbstractNodeObject 
    (create-transport [this addr port-num] 
    "Create instance of ``OtpTransport``.")) 

(extend-type AbstractNode 
    AbstractNodeObject 
    (create-transport [this addr port-num] 
    (.createTransport this addr port-num))) 

每JInterface的Javadoc,AbstractNode.createTransport可以採取java.lang.Stringjava.net.InetAddressaddr。雖然我相信我已在ann-protocol表單中正確註釋了這一點,但core.typed抱怨實施(在示例的最後一行)。我懷疑我可以用t/inst來解決這個問題,但還沒有找出確切的咒語。

我知道可以在型提示,^String^InetAddress從相應t/IFn形式添加到arglist中(在第二至示例的最後一行),並刪除其他條款之上,但如何可以告訴core.typed,要麼類型是可行的?

回答

2

與安布羅斯在IRC一個簡短的討論之後,我們達成的解決辦法是在(string? addr)分支如下:

(create-transport [this addr port-num] 
    (if (string? addr) 
    (.createTransport this ^String addr port-num) 
    (.createTransport this ^InetAddress addr port-num))) 

,並更換IFn註釋:

[AbstractNodeObject (t/U String InetAddress) int -> (t/Nilable OtpTransport)]