Idiomatic way to handle name shadowing?...

2011-03-10 Thread stu
Hi,

This is a newb question so apologies in advance if I haven't read
enough Clojure code yet to see the answer.

Suppose I need mathematical 2D vector functions:


(ns geometry.vector)

(defstruct vector :x :y)

(defn +
Add vectors v and w, yielding a vector
[ v w ]
(...))

Which leads to the core vector function being shadowed by my struct
and the core + function shadowed by my function.  What is the
idiomatic Clojure approach for handling this situation?  Is it through
careful use of :require and :import parts of a (ns ...) form?

I've also seen instances of naming changes to avoid the problem, e.g.
(defstruct vector ...) and (defn vector+ ...)
but not sure if that's making best use of Clojure namespaces?

Any advice much appreciated,

Stu

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Idiomatic way to handle name shadowing?...

2011-03-10 Thread Alan
On Mar 10, 12:48 pm, stu stuart.hungerf...@gmail.com wrote:
 Hi,

 This is a newb question so apologies in advance if I haven't read
 enough Clojure code yet to see the answer.

 Suppose I need mathematical 2D vector functions:

 (ns geometry.vector)

 (defstruct vector :x :y)

 (defn +
         Add vectors v and w, yielding a vector
         [ v w ]
         (...))

 Which leads to the core vector function being shadowed by my struct
 and the core + function shadowed by my function.  What is the
 idiomatic Clojure approach for handling this situation?  Is it through
 careful use of :require and :import parts of a (ns ...) form?

 I've also seen instances of naming changes to avoid the problem, e.g.
 (defstruct vector ...) and (defn vector+ ...)
 but not sure if that's making best use of Clojure namespaces?

 Any advice much appreciated,

 Stu


user= (ns math (:refer-clojure :rename {vector cvector}))
nil
math= vector
java.lang.Exception: Unable to resolve symbol: vector in this context
(NO_SOURCE_FILE:0)
math= (def vector identity)
#'math/vector
math= (vector 10)
10
math= (cvector 10)
[10]

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Idiomatic way to handle name shadowing?...

2011-03-10 Thread stu
On Mar 11, 8:35 am, Alan a...@malloys.org wrote:

  Suppose I need mathematical 2D vector functions:

  (ns geometry.vector)

  (defstruct vector :x :y)

  (defn +
          Add vectors v and w, yielding a vector
          [ v w ]
          (...))

  Which leads to the core vector function being shadowed by my struct
  and the core + function shadowed by my function.  What is the
  idiomatic Clojure approach for handling this situation?  Is it through
  careful use of :require and :import parts of a (ns ...) form?

 user= (ns math (:refer-clojure :rename {vector cvector}))
 nil
 math= vector
 java.lang.Exception: Unable to resolve symbol: vector in this context
 (NO_SOURCE_FILE:0)
 math= (def vector identity)
 #'math/vector
 math= (vector 10)
 10
 math= (cvector 10)
 [10]

Thanks for that -- please bear with me on this: does that mean that
given the flexible options available in the (ns...) form as shown
here, Clojure developers don't need to choose non-shadowed names like
vector or (defn vector+), but rather use the more natural forms and
use (ns...) clauses to make everything clear as needed?

Thanks,

Stu

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Idiomatic way to handle name shadowing?...

2011-03-10 Thread Alan
On Mar 10, 8:04 pm, stu stuart.hungerf...@gmail.com wrote:
 On Mar 11, 8:35 am, Alan a...@malloys.org wrote:
   Suppose I need mathematical 2D vector functions:

   (ns geometry.vector)

   (defstruct vector :x :y)

   (defn +
           Add vectors v and w, yielding a vector
           [ v w ]
           (...))

   Which leads to the core vector function being shadowed by my struct
   and the core + function shadowed by my function.  What is the
   idiomatic Clojure approach for handling this situation?  Is it through
   careful use of :require and :import parts of a (ns ...) form?
  user= (ns math (:refer-clojure :rename {vector cvector}))
  nil
  math= vector
  java.lang.Exception: Unable to resolve symbol: vector in this context
  (NO_SOURCE_FILE:0)
  math= (def vector identity)
  #'math/vector
  math= (vector 10)
  10
  math= (cvector 10)
  [10]

 Thanks for that -- please bear with me on this: does that mean that
 given the flexible options available in the (ns...) form as shown
 here, Clojure developers don't need to choose non-shadowed names like
 vector or (defn vector+), but rather use the more natural forms and
 use (ns...) clauses to make everything clear as needed?

 Thanks,

 Stu

You can write whichever will make your library easier to use and
understand. But do realize if you define something called +, and
someone needs to use your library as well as clojure.core, you are
forcing every user of your library to make a decision about how to
handle the renaming. Arguably this is better than naming it mathy-
vector; that would mean clients don't have to deal with renames, but
it also means it would be harder for them to use your library, or read
code someone *else* wrote using your library with different renames.
It's all a tradeoff, just like everything: the best we can do is tell
you what tools and options you have and let you decide.

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en