tsuraan a écrit :
>> I guess that if you enable reflection warnings, you'll get a warning on
>> the line where you invoke the constructor.
>>
>> I think the reflective dispatch doesn't pick the good constructor and
>> invokes OtpErlangBitstr(Object) instead of OtpErlangBitstr(byte[]).
>> Thus your byte[] is serialized and the 35 above is the length of the
>> serialized array.
>>
>> A type hint on s should fix the issue:
>>
>> (defmethod to-otp String [ #^String s ]
>>   (OtpErlangBinary. (.getBytes s "utf-8")))
>>     
>
> Ok, that worked, but I don't understand why.  How does forcing the
> type of s to be String cause the dispatch to use the byte[]
> constructor instead of the Object constructor?  This would make sense
> if we were somehow coercing the return value of .getBytes to be a
> byte[] instead of an Object, but I don't see how the type checker is
> calling any constructor other than a byte[] constructor when that's
> the return value of getBytes already.
>   

It's a type hint, it's not a type coercion.

Without the type hint, the compiler doesn't know the type of s, so it 
can't find the .getBytes method (nor, of course, its return type) and, 
in doubt, picks the "broader" constructor: OtpErlangBinary(Object).
With the type hint, the compiler know that s must be treated as a 
String, it find .getBytes, sees that it returns a byte[] and is able to 
select the right constructor for OtpErlangBinary.


-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)



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