So, for a random project, I found myself using a single protocol extended to a bunch of record types. I did this using the support in `defrecord` itself, but what I dislike about it is that the definitions for each method of the protocol are spread out amongst multiple sections of code.
What I'd really like to do would be to define an implementation for many types within a single definition. Conceptually, this is the same thing you might do in OCaml or other staticly typed languages:
let bar x = match x with | String y -> ... | Character y -> ...
So, I came up with this (below), only it doesn't work. I haven't dug deep yet to figure out why exactly, or if this could even technically be done without manipulating something in core. (I'm going to assume for now that I missed something simple, and it could be fixed trivially.)
(defp Foo bar [x] String (concat x x) Character (list x x))
(defp Foo baz [x] String (rest (seq x)) Character nil)
So, is there a technical reason why this is a bad idea, other than "the interface might change in the future"? And, If people are actually into this idea, what adjustments should be made to the syntax?
On Mon, Feb 7, 2011 at 2:16 PM, Ken Wesson <kwess...@gmail.com> wrote: > Can't you just use extend-protocol to group your protocol > implementations for each record type in one place?
That certainly works, but it's really not much different than:
It seems to me that the only reason that specifying the entire implementation together is to avoid situations where the implementation is not completely defined.
See, I'm less concerned about implementing the Protocol than I am about creating "functions" that are dispatched by type, and since they have to have the same argument list, I see no reason why that boilerplate can't be eliminated.
Perhaps one solution for me is to write a macro that constructs a protocol (and extends it to types) out of the definitions provided.. sort of a hybrid of extend-protocol and defp from before.
(defwhatever Foo (bar [_] String (body) Character (body)) (baz [_] String (body) Character (body)))
Which gets me to where I ultimately want to be (methods defined together per type extended too), but not quite what I'm really looking for.
> You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your first post. > To unsubscribe from this group, send email to > clojure+unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en
On Mon, Feb 7, 2011 at 2:49 PM, Andrew Gwozdziewycz <apg...@gmail.com> wrote: > On Mon, Feb 7, 2011 at 2:16 PM, Ken Wesson <kwess...@gmail.com> wrote: >> Can't you just use extend-protocol to group your protocol >> implementations for each record type in one place?
> That certainly works, but it's really not much different than:
> It seems to me that the only reason that specifying the entire > implementation together is to avoid situations where the > implementation is not completely defined.
> See, I'm less concerned about implementing the Protocol than I am > about creating "functions" that are dispatched by type, and since they > have to have the same argument list, I see no reason why that > boilerplate can't be eliminated.
> Perhaps one solution for me is to write a macro that constructs a > protocol (and extends it to types) out of the definitions provided.. > sort of a hybrid of extend-protocol and defp from before.
On Mon, Feb 7, 2011 at 3:30 PM, Ken Wesson <kwess...@gmail.com> wrote: > On Mon, Feb 7, 2011 at 2:49 PM, Andrew Gwozdziewycz <apg...@gmail.com> wrote: >> On Mon, Feb 7, 2011 at 2:16 PM, Ken Wesson <kwess...@gmail.com> wrote: >>> Can't you just use extend-protocol to group your protocol >>> implementations for each record type in one place?
>> That certainly works, but it's really not much different than:
>> It seems to me that the only reason that specifying the entire >> implementation together is to avoid situations where the >> implementation is not completely defined.
>> See, I'm less concerned about implementing the Protocol than I am >> about creating "functions" that are dispatched by type, and since they >> have to have the same argument list, I see no reason why that >> boilerplate can't be eliminated.
>> Perhaps one solution for me is to write a macro that constructs a >> protocol (and extends it to types) out of the definitions provided.. >> sort of a hybrid of extend-protocol and defp from before.
>> Which gets me to where I ultimately want to be (methods defined >> together per type extended too), but not quite what I'm really looking >> for.
> Now it sounds like you might want a plain old multimethod.
It'd be more easily done with multimethods, sure, but since they all implement both methods, there's no reason to not use protocols. See the dilemma?
> -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your first post. > To unsubscribe from this group, send email to > clojure+unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en
On Mon, Feb 7, 2011 at 5:21 PM, Andrew Gwozdziewycz <apg...@gmail.com> wrote: > On Mon, Feb 7, 2011 at 3:30 PM, Ken Wesson <kwess...@gmail.com> wrote: >> On Mon, Feb 7, 2011 at 2:49 PM, Andrew Gwozdziewycz <apg...@gmail.com> wrote: >>> On Mon, Feb 7, 2011 at 2:16 PM, Ken Wesson <kwess...@gmail.com> wrote: >>>> Can't you just use extend-protocol to group your protocol >>>> implementations for each record type in one place?
>>> That certainly works, but it's really not much different than:
>>> It seems to me that the only reason that specifying the entire >>> implementation together is to avoid situations where the >>> implementation is not completely defined.
>>> See, I'm less concerned about implementing the Protocol than I am >>> about creating "functions" that are dispatched by type, and since they >>> have to have the same argument list, I see no reason why that >>> boilerplate can't be eliminated.
>>> Perhaps one solution for me is to write a macro that constructs a >>> protocol (and extends it to types) out of the definitions provided.. >>> sort of a hybrid of extend-protocol and defp from before.
>>> Which gets me to where I ultimately want to be (methods defined >>> together per type extended too), but not quite what I'm really looking >>> for.
>> Now it sounds like you might want a plain old multimethod.
> It'd be more easily done with multimethods, sure, but since they all > implement both methods, there's no reason to not use protocols. See > the dilemma?
>> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscribe@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en