This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-14
Channels
- # announcements (4)
- # aws (7)
- # babashka (44)
- # beginners (178)
- # calva (15)
- # cider (3)
- # clj-kondo (15)
- # clojure (139)
- # clojure-dev (8)
- # clojure-europe (2)
- # clojure-italy (2)
- # clojure-losangeles (9)
- # clojure-nl (32)
- # clojure-spec (6)
- # clojure-sweden (1)
- # clojure-uk (27)
- # clojurescript (17)
- # core-typed (116)
- # cursive (26)
- # data-science (1)
- # datomic (14)
- # duct (16)
- # emacs (9)
- # events (1)
- # fulcro (47)
- # jobs (3)
- # juxt (6)
- # keechma (2)
- # malli (59)
- # mid-cities-meetup (8)
- # off-topic (32)
- # pathom (5)
- # reagent (2)
- # remote-jobs (4)
- # rewrite-clj (16)
- # shadow-cljs (14)
- # spacemacs (9)
- # sql (27)
- # tools-deps (37)
- # vscode (7)
answered @zilti over here if anyone wants to know https://clojurians.slack.com/archives/C03S1KBA2/p1581690839174800
Actually I am writing annotations right now ^^ Just like when I tried it a few years ago.
And yes, I have two questions...
1. How do I define a subtype just for one declaration? Like [a Any -> a]
where a is a subtype of let's say (U Kw Sym)
only in that one definition?
2. core.typed just told me to add type hints to a Java constructor call. Making the argument be annotated seems to not suffice, is that on purpose, or not implemented yet?
1. let me look up the syntax xD
2. turn on (set! **warn-on-reflection** true)
and fix reflection warnings. it should stop complaining
And one more to vararg functions. I have a function (fn [& selectors] ...)
and the annotation [& (Vec Selector) -> [State -> State]]
is apparently wrong
there's a &
syntax for kwargs. some docs here https://github.com/clojure/core.typed/wiki/Types#rest-parameters
Also, fyi: this will fail with an "Unannotated var: set-key" error: (defn set-key [key :- Kw modificator-fn [State -> Any]] :- [State -> State] ...)
But it works when annotating it separately like this: (ann set-key [Kw [State -> Any] -> [State -> State]])
Yes. Actually I did a :as t :refer :all
and tried both defn
and t/defn
. Seems to be a bit inconsistent though, so I am just using ann
for now. If I find time I'll try to find out how it breaks
oh yea lol t/defn
doesn't add a global annotation https://github.com/clojure/core.typed/blob/master/core/runtime.jvm/src/main/clojure/clojure/core/typed/macros.clj#L275
obviously also a good idea in many situations, but you didn't have the option to choose with t/defn
's behavior
But isn't the point about static type checking that it is allowed to have sorta bad performance?
eliminates many-minute waits for initial check-ns's that don't really do anything, and can be delegated to CI
And it was bleeding into all dependencies. "Oh, you want to use obscure-ns/whatever? Please add an annotation for half the ns", which is something you just can't pull off with an optional type system
if you care, should be easy to make you're own wrapper for t/defn
that also emits an ann
by reusing the machinery around the link above ^
I mean even now I had to add annotations for two clojure.alpha.spec functions because I have s/def
in my code
Now, am I right that in the s/register example above, a
can be Kw
on the argument side and Sym
on the return side in this case, and I'd need to make two annotations in a Union to cover that it returns Kw when given Kw?
I guess what I am thinking about are generics? Like in Java where you'd write "K register(K k, S s)"
ah. I would annotate it [(U Kw Sym) Any -> (U Kw Sym)]
you only need the type variable if you're going to type-check code that uses s/register
and cares exactly which keyword/sym you return.
you can also do (IFn [Kw Any -> Kw] [Sym Any -> Sym] [(U Kw Sym) Any -> (U Kw Sym)])
if you want to maintain the dependency between first arg and return
ok so your case is wondering whether (ann-form (s/register :a ...) Sym)
would type check using that annotation
Ah. So basically, my annotation is already correct in that sense, (ann s/register (All [[a :< (U Kw Sym)]] [a Any -> a]))
does imply that if argument a
is of type Kw
the return type will also be Kw
?
well, I'm not sure. the type information for a
flows in from the type of the first argument and the expected type of the return. which is why I'm not sure if (ann-form (s/register :a ...) Sym)
would be allowed
I mean, in Java it would be clear - K register (K k, S s)
guarantees that the return type K is the same or a subtype of the argument K, so when I give a string, I am guaranteed that the function will return a string or a subtype of string
the equivalent in java fwiw is (forget the syntax) <K super (U Kw Sym)> K register (K k, S s)
And there I wouldn't know either if that would guarante the returned value to be of (sub)type of the argument given
ok. I think you're right. ((t/inst s/register Kw) ...)
is guaranteed to return a keyword
g2g nice chatting about types again! I get emailed about messages in this channel and I usually respond within 24hrs, or try the core.typed google group
@ambrosebs hey! nice that you continue working on core.typed!
do you plan to port https://docs.racket-lang.org/turnstile/index.html to clojure?