[Idiom] 'myfile.txt' asFileName readStream contents

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

[Idiom] 'myfile.txt' asFileName readStream contents

Hannes Hirzel
Hello

Randal Schwartz writes in his blog
http://methodsandmessages.posterous.com/writestreams-of-consciousness-going-to-xtream
about streams citing Derek Williams   http://derekwilliams.us/?p=1411

He gives the following as an example of elegant syntax of Smalltalk

    'myfile.txt' asFileName readStream contents

This does not work in Squeak nor in Pharo.

It produces a ReadStream on the contents of the _String_  'myfile.txt'.
i.e. the ByteString with 10 elements.

Comments?

Regards

Hannes

Reply | Threaded
Open this post in threaded view
|

Re: [Idiom] 'myfile.txt' asFileName readStream contents

Hannes Hirzel
On 10/30/10, Norbert Hartl <[hidden email]> wrote:

>
> On 30.10.2010, at 13:14, Hannes Hirzel wrote:
>
>> Surely, but not what Randal implies in terms of streams I suppose....
>>
>> The idiom implies that a file stream is created and that I get the
>> content of a file -- not the content of the file name.
>>
> I read it different. You get an readStream on a filename which is a String.
> I would expect it to be
>
> 'myFile.txt' asFile readStream contents

Yes something like this; but asFile is neither in Squeak nor in Pharo.

So what we currently have (in both Squeak and Pharo) is

(FileStream fileNamed: 'myfile.txt') contents

to express this. Not as elegant but it works.


> or
>
> 'myFile.txt' asFileName file readStream contents (but than someone will
> bring up Demeter)
>
> Norbert
>
>> --Hannes
>>
>> On 10/30/10, Peter Hugosson-Miller <[hidden email]> wrote:
>>> The result you got is what I would have expected too.
>>>
>>> --
>>> Cheers,
>>> Peter
>>>
>>> On Sat, Oct 30, 2010 at 12:50 PM, Hannes Hirzel
>>> <[hidden email]>wrote:
>>>
>>>> Hello
>>>>
>>>> Randal Schwartz writes in his blog
>>>>
>>>> http://methodsandmessages.posterous.com/writestreams-of-consciousness-going-to-xtream
>>>> about streams citing Derek Williams   http://derekwilliams.us/?p=1411
>>>>
>>>> He gives the following as an example of elegant syntax of Smalltalk
>>>>
>>>>   'myfile.txt' asFileName readStream contents
>>>>
>>>> This does not work in Squeak nor in Pharo.
>>>>
>>>> It produces a ReadStream on the contents of the _String_  'myfile.txt'.
>>>> i.e. the ByteString with 10 elements.
>>>>
>>>> Comments?
>>>>
>>>> Regards
>>>>
>>>> Hannes
>>>>
>>>
>>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: [Idiom] 'myfile.txt' asFileName readStream contents

K K Subbu
In reply to this post by Hannes Hirzel
On Saturday 30 Oct 2010 4:20:46 pm Hannes Hirzel wrote:

> Hello
>
> Randal Schwartz writes in his blog
> http://methodsandmessages.posterous.com/writestreams-of-consciousness-going
> -to-xtream about streams citing Derek Williams  
> http://derekwilliams.us/?p=1411
>
> He gives the following as an example of elegant syntax of Smalltalk
>
>     'myfile.txt' asFileName readStream contents
>
> This does not work in Squeak nor in Pharo.
>
> It produces a ReadStream on the contents of the _String_  'myfile.txt'.
> i.e. the ByteString with 10 elements.
Looks like a typo. I suppose what was meant was:
   'myfile.txt' asFileName asFileStream contents

where asFileStream is a monkey patch on String:

String>>asFileStream
        ^FileStream readOnlyFileNamed: self.

Subbu

Reply | Threaded
Open this post in threaded view
|

Re: [Idiom] 'myfile.txt' asFileName readStream contents

Sean P. DeNigris
Administrator
In reply to this post by Hannes Hirzel
Hannes Hirzel-2 wrote
> 'myfile.txt' asFileName readStream contents
[vs.]
(FileStream fileNamed: 'myfile.txt') contents
As cool as the first one is, I prefer the second because it doesn't fatten String.  Kent Beck's advice to reserve asXxx to simple conversions between objects with similar protocols feels right to me (e.g. Number>>asFloat, but not String>>asDate).

See Converter Method in http://stephane.ducasse.free.fr/FreeBooks/BestSmalltalkPractices/Draft-Smalltalk%20Best%20Practice%20Patterns%20Kent%20Beck.pdf
Cheers,
Sean