A few enhancements

Showing 1-7 of 7 messages
A few enhancements joegallo 12/1/11 10:00 AM
Here are some things I've run across in Clojure that seem asymmetric to me -- I looked through Jira for issues related to these, but I didn't find any (of course, I might have just missed them).  Some of these might be valid enhancements that would make sense to add, some of them might be a case of me having a bad mental model and wanting the wrong thing from the language.  I'm hoping to get some feedback to help me determine which is which, and then I'll write some patches for the ideas that are good ones.  I've tried to avoid making any statements about the world (e.g. x is y), these are all just statements about my personal experience (e.g. x seems like y to me).

1.
user=> (keyword 'foo)
:foo
user=> (keyword "foo")       
:foo                                   
user=> (keyword :foo)      
:foo                                             
user=> (symbol "foo")                              
foo                                    
user=> (symbol 'foo)        
foo                                      
user=> (symbol :foo)             
ClassCastException clojure.lang.Keyword cannot be cast to java.lang.String  clojure.core/symbol (core.clj:522)

I've never wanted to create a symbol from a keyword, but this seems asymmetrical to me.

2.
user=> (name 'user)
"user"
user=> (name :user)
"user"
user=> (name "user")
"user"
user=> (name *ns*)
ClassCastException clojure.lang.Namespace cannot be cast to clojure.lang.Named  clojure.core/name (core.clj:1488)                                                                                                                            

I think of namespaces as being a thing that has a name, so I would expect to be able to call name on them.  I always do this first, and then it blows up, and then I remember ns-name, but that returns a symbol, not a string (which I also always forget), so then I finally have to pipe that through name or str, which ends up seeming messier than it should to me.

user=> (name (ns-name *ns*))
"user"

One solution to that would be name namespaces a clojure.lang.Named, where they return their own name -- which seems reasonable to me.  But that would lead us to this next case, below.

3.
user=> (namespace 'foo)
nil
user=> (namespace 'user/foo)
"user"
user=> (namespace :foo)
nil
user=> (namespace ::foo)
"user"
user=> (namespace *ns*)
ClassCastException clojure.lang.Namespace cannot be cast to clojure.lang.Named  clojure.core/namespace (core.clj:1496)

If we make Namespaces a clojure.lang.Named, then they'll also need to have an implementation of getNamespace.  I suppose we could return the namespace's name, but does that imply that a namespace is in itself?  That seems wrong to me.  Another option would be to return nil, which indicates that a namespace is not in a namespace, which is true, but possibly confusing.  A third possibility would be to split Named into Named (has a name) and Namespaced (has a namespace), and then we don't have this issue at all -- we'd be free to say that namespaces are Named, but not Namespaced.                                                                                                                                                                               

4.        
user=> (symbol "bar")                                                   
bar                                                              
user=> (symbol 'bar)                                         
bar                                                  
user=> (symbol "foo" "bar")        
foo/bar                                
user=> (symbol "foo" 'bar)  
ClassCastException clojure.lang.Symbol cannot be cast to java.lang.String  clojure.core/symbol (core.clj:523)                                                                   
user=> (symbol 'foo 'bar) 
ClassCastException clojure.lang.Symbol cannot be cast to java.lang.String  clojure.core/symbol (core.clj:523)                                             
user=> (symbol *ns* 'bar)                                                                               
ClassCastException clojure.lang.Namespace cannot be cast to java.lang.String  clojure.core/symbol (core.clj:523)                                    
user=> (symbol *ns* "bar")           
ClassCastException clojure.lang.Namespace cannot be cast to java.lang.String  clojure.core/symbol (core.clj:523)                                           

I always forget that symbol allows a string or a symbol for first argument, but that when you have two arguments, they must both be strings.  It's another "d'oh!" thing for me that I find myself correcting a lot.  I'd also like to be able to construct a symbol with a namespace object directly, not just the string that represents the namespace's name.

Anyway, what do you think?  If there are any smart ideas here, I'd be happy to create a jira ticket and attach a patch with tests.

Joe
Re: A few enhancements red...@gmail.com 12/1/11 10:31 AM

#4 is the biggest pain point for me, having symbol take a Namespace
and a Symbol would get rid of a lot of calls that look like (symbol
(name (ns-name *ns*)) (name sym)) and replace them with (symbol *ns*
sym)

>
> I always forget that symbol allows a string or a symbol for first argument,
> but that when you have two arguments, they must both be strings.  It's
> another "d'oh!" thing for me that I find myself correcting a lot.  I'd also
> like to be able to construct a symbol with a namespace object directly, not
> just the string that represents the namespace's name.
>
> Anyway, what do you think?  If there are any smart ideas here, I'd be happy
> to create a jira ticket and attach a patch with tests.
>
> Joe
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

Re: A few enhancements Alan Malloy 12/1/11 11:23 AM
1) I agree this seems silly but I don't think it's ever bitten me

2) I think this has caused me problems once or twice

3) It would be nice to have Named and Namespaced, but I don't think
it's feasible to make the change now - there's too much code assuming
you can get the namespace of a Named thing. Maybe it would be okay if
we left Named alone, added Namespaced, and then had (namespace foo)
first check for Namespaced and then for Named.

4) Yes please this drives me crazy. Every time I do the following I
cringe:
(letfn [(symbol [& args] (apply clojure.core/symbol (map name args)))]
  (...use a sane-seeming symbol function...))

Re: A few enhancements Stefan Kamphausen 12/2/11 7:01 PM


On Thursday, December 1, 2011 8:23:33 PM UTC+1, Alan Malloy wrote:
1) I agree this seems silly but I don't think it's ever bitten me

Ain't this the difference between a language, that's been around for QAW (Quite A While, maybe even long enough to become a standard, e.g. ANSI) and a newcomer which will find all the rough edges when being used by more people?  
Re: A few enhancements Stuart Sierra 12/3/11 9:58 AM
A matter of curiosity: What are you doing that requires so much symbol manipulation?

-S

Re: A few enhancements red...@gmail.com 12/4/11 4:23 PM

macros mostly.

most recently a macro that registers a cache with a name to make it
available for flushing via a command line control interface, and a
macro replacement for syntax-quote.

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

Re: A few enhancements joegallo 12/5/11 8:19 AM
I've added a new ticket http://dev.clojure.org/jira/browse/CLJ-891 to jira for adding the ability to make (symbol some-ns "bar") work.  That tackles half of the issue in #4, and reduces your example to just (symbol *ns* (name sym)).