Integer>>#putOn:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Integer>>#putOn:

EstebanLM
Hi,

I just spotted this:


Integer>>#putOn: aStream

        (aStream isBinary ifTrue: [ self asByteArray ] ifFalse: [ self asString]) putOn: aStream
       

It does not look so bad at the begining, but… if I do:

ByteArray streamContents: [ :s | 42 putOn: s ]

I got an infinite loop.

I know, it does not look as a very typical case, but… if I’m writing to a Socket and I want to use

mySocket << 255 “IAC”, that’s exactly what will happen :(

If I redefine it as:

Integer>>#putOn: aStream
        aStream isBinary ifFalse: [
                self asString putOn: aStream.
                ^ self ].
        self asByteArray do: [ :each | aStream nextPut: each ]

then is ok…

Would that be a good change?

Esteban
Reply | Threaded
Open this post in threaded view
|

Re: Integer>>#putOn:

Sven Van Caekenberghe-2

> On 12 Oct 2015, at 14:26, Esteban Lorenzano <[hidden email]> wrote:
>
> Hi,
>
> I just spotted this:
>
>
> Integer>>#putOn: aStream
>
> (aStream isBinary ifTrue: [ self asByteArray ] ifFalse: [ self asString]) putOn: aStream

IMO, this is not good, implicit (and inefficient) conversions are being done. Why ?

What is the exact contract of #putOn: anyway ?

If the result should really depend on the argument, double dispatch might be better.

> It does not look so bad at the begining, but… if I do:
>
> ByteArray streamContents: [ :s | 42 putOn: s ]
>
> I got an infinite loop.
>
> I know, it does not look as a very typical case, but… if I’m writing to a Socket and I want to use
>
> mySocket << 255 “IAC”, that’s exactly what will happen :(
>
> If I redefine it as:
>
> Integer>>#putOn: aStream
> aStream isBinary ifFalse: [
> self asString putOn: aStream.
> ^ self ].
> self asByteArray do: [ :each | aStream nextPut: each ]
>
> then is ok…
>
> Would that be a good change?

Yeah that looks about right, still weird, but no weirder than before ;-)

> Esteban


Reply | Threaded
Open this post in threaded view
|

Re: Integer>>#putOn:

EstebanLM

On 12 Oct 2015, at 14:39, Sven Van Caekenberghe <[hidden email]> wrote:


On 12 Oct 2015, at 14:26, Esteban Lorenzano <[hidden email]> wrote:

Hi, 

I just spotted this:


Integer>>#putOn: aStream

(aStream isBinary ifTrue: [ self asByteArray ] ifFalse: [ self asString]) putOn: aStream

IMO, this is not good, implicit (and inefficient) conversions are being done. Why ?

I don’t know… I was just passing through :)


What is the exact contract of #putOn: anyway ?

If the result should really depend on the argument, double dispatch might be better.

It does not look so bad at the begining, but… if I do: 

ByteArray streamContents: [ :s | 42 putOn: s ]

I got an infinite loop.

I know, it does not look as a very typical case, but… if I’m writing to a Socket and I want to use 

mySocket << 255 “IAC”, that’s exactly what will happen :(

If I redefine it as: 

Integer>>#putOn: aStream
aStream isBinary ifFalse: [ 
self asString putOn: aStream.
^ self ].
self asByteArray do: [ :each | aStream nextPut: each ]

then is ok… 

Would that be a good change?

Yeah that looks about right, still weird, but no weirder than before ;-)

I submitted a fix, still ugly but well… at least it avoids the infinite loop :P



Esteban