Protocols in cljs

52 views
Skip to first unread message

Pedro Viegas

unread,
Sep 22, 2014, 4:26:34 PM9/22/14
to clojure-c...@googlegroups.com
Hi! I am trying to annotate a protocol in clojurescript, but I am not being able to.

I am really lost.

(ns myproject.protocols
  (:refer-clojure :exclude [defprotocol])
  (:require [cljs.core.typed :as t :include-macros true]))

(t/defprotocol A
  (fmap [fv g :- (t/All [x y] (t/Fn [x -> y]))]
[fv g :- (t/All [x y] (t/Fn [x -> y])) fvs :- (t/NonEmptyColl A)]))

Can anyone help me?

Thx

Ambrose Bonnaire-Sergeant

unread,
Sep 22, 2014, 5:14:53 PM9/22/14
to core.typed
I don't have time to check if this works in ClojureScript, but this equivalent should work in Clojure.

(defprotocol A
  ([a b] fmap
   [fv g :- [x -> y]] :- Result
   [fv g :- [x -> y] fvs :- (NonEmptyColl x)] :- Result))

Pedro Viegas

unread,
Sep 22, 2014, 5:24:27 PM9/22/14
to clojure-c...@googlegroups.com

Thanks, probably I am requiring the wrong namespace, because I am getting:

Type Error (myproject.protocols:4:1) Found untyped var: cljs.core.typed/defprotocol

in: cljs.core.typed/defprotocol

Ambrose Bonnaire-Sergeant

unread,
Sep 22, 2014, 5:27:44 PM9/22/14
to core.typed
Oh it's much more likely that defprotocol isn't implemented yet for ClojureScript. Hope to fix this soon.

Pedro Viegas

unread,
Sep 22, 2014, 6:09:22 PM9/22/14
to clojure-c...@googlegroups.com
Oh, anyways, I saw that ann-protocol is there, but couldn't find any example using it with multiple arity, like my case.

Would it be possible to help me with an example?

Ambrose Bonnaire-Sergeant

unread,
Sep 22, 2014, 6:15:05 PM9/22/14
to core.typed
Could you explain the semantics of fmap?

Thanks,
Ambrose

Pedro Viegas

unread,
Sep 22, 2014, 6:27:14 PM9/22/14
to clojure-c...@googlegroups.com
Sure, it should behave like a functor.

(ann-protocol Functor
  fmap
  (Fn [Functor [x -> y] -> Functor]
        [Functor [x -> y] (Coll Functor) -> Functor]))

This would be the behaviour on a string:

(defn fmap-string
  ([fv g] (apply str (g fv)))
  ([fv g fvs] (apply str (apply g fv fvs))))

(fmap #(apply str "A" %&) "b" "c" "d") => "Abcd"

Ambrose Bonnaire-Sergeant

unread,
Sep 22, 2014, 6:30:15 PM9/22/14
to core.typed
It gets messier if you want to abstract over the Functor, but does this give you a start?

(ann-protocol Functor [[x :variance :covariant]]
  fmap
  (All [y]
   (Fn [(Functor x) [x -> y] -> (Functor y)]
         [(Functor x) [x -> y] (Coll (Functor y)) -> (Functor y)]))

Pedro Viegas

unread,
Sep 23, 2014, 9:25:32 AM9/23/14
to clojure-c...@googlegroups.com
Yes it does! Thanks a lot for the quick reply.
Reply all
Reply to author
Forward
0 new messages