Polymorphism between Strings and FileReference

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

Polymorphism between Strings and FileReference

CyrilFerlicot
Hi,

I am often in the case where I have a method taking a file or a string
as parameter and I often want to use #readStreamDo:/#writeStreamDo: in
the method because with I do not have to care if I need to close the
file stream or not with those. But since String do not understand
#readStreamDo:/#writeStreamDo: I cannot do that.

Maybe it's worth to implement them on String?

https://pharo.fogbugz.com/f/cases/20796/String-should-understand-readStreamDo-writeStreamDo-to-be-polymorph-with-FileReference

Since it add methods to String I prefer talk about it on the mailing
list to have everyone's advice.

--
Cyril Ferlicot
https://ferlicot.fr

http://www.synectique.eu
2 rue Jacques Prévert 01,
59650 Villeneuve d'ascq France

Reply | Threaded
Open this post in threaded view
|

Re: Polymorphism between Strings and FileReference

Alistair Grant
Hi Cyril,

On 1 December 2017 at 11:50, Cyril Ferlicot <[hidden email]> wrote:

> Hi,
>
> I am often in the case where I have a method taking a file or a string
> as parameter and I often want to use #readStreamDo:/#writeStreamDo: in
> the method because with I do not have to care if I need to close the
> file stream or not with those. But since String do not understand
> #readStreamDo:/#writeStreamDo: I cannot do that.
>
> Maybe it's worth to implement them on String?
>
> https://pharo.fogbugz.com/f/cases/20796/String-should-understand-readStreamDo-writeStreamDo-to-be-polymorph-with-FileReference
>
> Since it add methods to String I prefer talk about it on the mailing
> list to have everyone's advice.

The normal way to handle this is #asFileReference, so you can just have:

aStringOrReference asFileReference readStreamDo: ...

Cheers,
Alistair

Reply | Threaded
Open this post in threaded view
|

Re: Polymorphism between Strings and FileReference

CyrilFerlicot
On Fri, Dec 1, 2017 at 11:55 AM, Alistair Grant <[hidden email]> wrote:
> Hi Cyril,
>
> The normal way to handle this is #asFileReference, so you can just have:
>
> aStringOrReference asFileReference readStreamDo: ...
>

In that case, if it is a String like 'test', we will get a readStream
on the file './test' and not on the string 'test'. This is not what I
want.

What I want is this for example:



MyClass>>addComaAtTheEndOf: aStringOrFileReference
  "If the argument is a String, add a coma at the end. If the
parameter is a file, add a coma at the end of its contents"

  aStringOrFileReference writeStreamDo: [ :s | s atEnd; nextPut: $, ]



If I use your code, it will not add a coma at the end of the string
parameter, but at the end of the file represented by the string if it
exists.

> Cheers,
> Alistair
>



--
Cyril Ferlicot
https://ferlicot.fr

http://www.synectique.eu
2 rue Jacques Prévert 01,
59650 Villeneuve d'ascq France

Reply | Threaded
Open this post in threaded view
|

Re: Polymorphism between Strings and FileReference

Alistair Grant
Hi Cyril,

On 1 December 2017 at 12:04, Cyril Ferlicot <[hidden email]> wrote:

> On Fri, Dec 1, 2017 at 11:55 AM, Alistair Grant <[hidden email]> wrote:
>> Hi Cyril,
>>
>> The normal way to handle this is #asFileReference, so you can just have:
>>
>> aStringOrReference asFileReference readStreamDo: ...
>>
>
> In that case, if it is a String like 'test', we will get a readStream
> on the file './test' and not on the string 'test'. This is not what I
> want.
>
> What I want is this for example:
>
>
>
> MyClass>>addComaAtTheEndOf: aStringOrFileReference
>   "If the argument is a String, add a coma at the end. If the
> parameter is a file, add a coma at the end of its contents"
>
>   aStringOrFileReference writeStreamDo: [ :s | s atEnd; nextPut: $, ]
>
>
>
> If I use your code, it will not add a coma at the end of the string
> parameter, but at the end of the file represented by the string if it
> exists.

Sorry, I misunderstood.

This looks like a good addition.  Although is there any reason it
shouldn't be added to SequenceableCollection, where #readStream and
#writeStream are defined?

Cheers,
Alistair

Reply | Threaded
Open this post in threaded view
|

Re: Polymorphism between Strings and FileReference

CyrilFerlicot
On Fri, Dec 1, 2017 at 12:17 PM, Alistair Grant <[hidden email]> wrote:
> Hi Cyril,
>
>
> Sorry, I misunderstood.
>

No problem :)

> This looks like a good addition.  Although is there any reason it
> shouldn't be added to SequenceableCollection, where #readStream and
> #writeStream are defined?
>

I agree. I talked about string because it is my use case but it's true
it should be with #readStream/#writeStream.

> Cheers,
> Alistair
>



--
Cyril Ferlicot
https://ferlicot.fr

http://www.synectique.eu
2 rue Jacques Prévert 01,
59650 Villeneuve d'ascq France

Reply | Threaded
Open this post in threaded view
|

Re: Polymorphism between Strings and FileReference

Stephane Ducasse-3
In reply to this post by CyrilFerlicot
To me stringOfFileReference is a bad smell.



On Fri, Dec 1, 2017 at 12:04 PM, Cyril Ferlicot
<[hidden email]> wrote:

> On Fri, Dec 1, 2017 at 11:55 AM, Alistair Grant <[hidden email]> wrote:
>> Hi Cyril,
>>
>> The normal way to handle this is #asFileReference, so you can just have:
>>
>> aStringOrReference asFileReference readStreamDo: ...
>>
>
> In that case, if it is a String like 'test', we will get a readStream
> on the file './test' and not on the string 'test'. This is not what I
> want.
>
> What I want is this for example:
>
>
>
> MyClass>>addComaAtTheEndOf: aStringOrFileReference
>   "If the argument is a String, add a coma at the end. If the
> parameter is a file, add a coma at the end of its contents"
>
>   aStringOrFileReference writeStreamDo: [ :s | s atEnd; nextPut: $, ]
>
>
>
> If I use your code, it will not add a coma at the end of the string
> parameter, but at the end of the file represented by the string if it
> exists.
>
>> Cheers,
>> Alistair
>>
>
>
>
> --
> Cyril Ferlicot
> https://ferlicot.fr
>
> http://www.synectique.eu
> 2 rue Jacques Prévert 01,
> 59650 Villeneuve d'ascq France
>