Type annotations are used at compile time to generate direct method
calls (as opposed to using reflection).

`cast` is a function, so it doesn't do anything until runtime, and it
just delegates to Class#cast.
I don't think it is used much. In the core, it's only called in single-
arity cases for * and +, to make sure that the operand is indeed a
Number.

Not sure about the type inference here. Have you tried with
SourceDataLine imported, but without the annotation? Rich does that in
the example.

On Jul 24, 10:53 pm, Kartik <ksara...@gmail.com> wrote:
> Hello.  I have the following code snippet to start reading an mp3 file
>
> (let [file (File. filename)
>       stream (AudioSystem/getAudioInputStream file)
>       base-format (.getFormat stream)
>       decoded-format (AudioFormat. AudioFormat$Encoding/
> PCM_SIGNED ...)
>       line-info (DataLine$Info. SourceDataLine decoded-format)
>       decoded-stream (AudioSystem/getAudioInputStream decoded-format
> stream)
>       line (AudioSystem/getLine line-info)
>       buffer (byte-array (* 1024 4))]
>   (pprint line)
>   (.open line decoded-format)
>   (.start line)
>   ...)
>
> This works fine on Mac OSX.  The pprint gives
>
> #<MixerSourceLine com.sun.media.sound.MixerSourceLine@56101751>
>
> When I run it in windows I get
>
> Can't call public method of non-public class: public void
> com.sun.media.sound.AbstractDataLine.open(javax.sound.sampled.AudioFormat)
> throws javax.sound.sampled.LineUnavailableException
>   [Thrown class java.lang.IllegalArgumentException]
>
> This time the pprint gives
>
>  #<DirectSDL com.sun.media.sound.DirectAudioDevice$DirectSDL@173bb67>
>
> The problem is that getLine on windows returns DirectSDL which is a
> private inner class of DirectAudioDevice.  I tried casting it to
> SourceDataLine which is the type I need (which is a literal
> translation of equivalent java code I'm following) by doing
>
>       line (cast SourceDataLine (AudioSystem/getLine line-info))
>
> But that didn't work.  I had to declare the type like so
>
>       #^SourceDataLine line (AudioSystem/getLine line-info)
>
> My question is what is the difference between casting and type
> declaration?  Why does one work and not the other in this case?  In
> general, when is one used over the other?
>
> I found a related post from 
> Richhttp://www.mail-archive.com/clojure@googlegroups.com/msg13283.html
> where he says the let (without type declaration I assume) should allow
> the compiler to infer the type.
>
> Thanks
>
> Kartik

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

Reply via email to