clojure - How do I properly annotate overloaded Java methods? -
in working annotate clojang, i'm having trouble protocol methods call overloaded java methods. i've narrowed problem down following minimal tricky case 'clojang.jinterface.otp.nodes
:
(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 addr port-num)))
per jinterface javadocs, abstractnode.createtransport
can take java.lang.string
or java.net.inetaddress
addr
. while believe i've annotated correctly in ann-protocol
form, core.typed
complains implementation (on last line in example). suspect can somehow fix using t/inst
, have yet sort out exact incantation.
i know add type hint, ^string
or ^inetaddress
arglist (on second last line of example) , remove other clause corresponding t/ifn
form above, how can tell core.typed
either type viable?
after brief discussion ambrose on irc, solution we've reached branch on (string? addr)
follows:
(create-transport [this addr port-num] (if (string? addr) (.createtransport ^string addr port-num) (.createtransport ^inetaddress addr port-num)))
and replace ifn
annotation with:
[abstractnodeobject (t/u string inetaddress) int -> (t/nilable otptransport)]
Comments
Post a Comment