Re: Type hints and records

2011-10-20 Thread Chris Perkins
Tangentially: In this particular case, reflection isn't strictly necessary 
because toString is a method of Object. In theory, the compiler could 
special-case Object's methods and never do reflection, right?

In practice, I don't know if it's worth the effort, although it's certainly 
a little surprising that (.toString thing) can be 100 times slower than (str 
thing), at least until you know what's going on there.

- Chris

-- 
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: Type hints and records

2011-10-20 Thread Casper Clausen
Thanks for the clarification.

Just to clear up any confusion, the .toString example was just the
simplest example I could think of that illustrated was I was seeing
with regards to reflection and type hints :)

On Oct 20, 4:22 am, Michael Fogus mefo...@gmail.com wrote:
 Another potential option is to implement a record toString method:

 (defrecord Rec [^Integer i]
   Object
   (toString [_] (str i)))

 (str (Rec. 42))
 ;= 42

-- 
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: Type hints and records

2011-10-19 Thread Alan Malloy
You can't really fix that, because (:foo bar) means call
function :foo on bar, and in general the function :foo returns
Object. (.foo bar) says look at the Java object bar, and give me its
foo member. Because there is a typehint on that, Clojure can know
what return type to expect.

On Oct 19, 2:40 pm, Casper Clausen casp...@gmail.com wrote:
 Thanks, that works nicely!

 Is it intended or just incidental that type hints are not retained
 using the :-notation? There doesn't seem to be much advantage to not
 including type hints when available.

 On Oct 11, 1:02 am, Stuart Halloway stuart.hallo...@gmail.com wrote:







   I am using a record to wrap a number of java classes, which I then
   access various properties on. I am trying to avoid reflection so I
   type have type hinted, however when accessing the values in the record
   the type hints are lost. It might look something like this

   (defrecord Rec [^Integer i])

   (defn to-string [^Rec record] (.toString (:i record)))

   However the to-string function gives a reflection warning, even with
   the input the to function type hinted. Now I know that type hints
   don't survive across function boundaries, but is there no way to get a
   type hinted value from the record?

  Treat the record as a typed thing instead of as a map (note the dot instead 
  of the colon).

  (defn to-string [^Rec record] (.toString (.i record)))

  That said, and not knowing exactly what you are doing, the following looks 
  better to me:

  (defrecord Rec [^int i])
  (defn to-string [^Rec record] (str (:i record)))

  Stu

  Stuart Halloway
  Clojure/corehttp://clojure.com

-- 
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


Type hints and records

2011-10-10 Thread casper
I am using a record to wrap a number of java classes, which I then
access various properties on. I am trying to avoid reflection so I
type have type hinted, however when accessing the values in the record
the type hints are lost. It might look something like this

(defrecord Rec [^Integer i])

(defn to-string [^Rec record] (.toString (:i record)))

However the to-string function gives a reflection warning, even with
the input the to function type hinted. Now I know that type hints
don't survive across function boundaries, but is there no way to get a
type hinted value from the record?

-- 
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: Type hints and records

2011-10-10 Thread Stuart Halloway
 I am using a record to wrap a number of java classes, which I then
 access various properties on. I am trying to avoid reflection so I
 type have type hinted, however when accessing the values in the record
 the type hints are lost. It might look something like this
 
 (defrecord Rec [^Integer i])
 
 (defn to-string [^Rec record] (.toString (:i record)))
 
 However the to-string function gives a reflection warning, even with
 the input the to function type hinted. Now I know that type hints
 don't survive across function boundaries, but is there no way to get a
 type hinted value from the record?

Treat the record as a typed thing instead of as a map (note the dot instead of 
the colon).

(defn to-string [^Rec record] (.toString (.i record)))

That said, and not knowing exactly what you are doing, the following looks 
better to me:

(defrecord Rec [^int i])
(defn to-string [^Rec record] (str (:i record)))

Stu


Stuart Halloway
Clojure/core
http://clojure.com

-- 
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