A "with" construct like Pascal - easy to do, but is it terrible?

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

A "with" construct like Pascal - easy to do, but is it terrible?

Tim Mackinnon
I’ve noticed that as we’ve progressed there has been a move to more concise and fluid code - e.g. I quite like the new String streaming stuff

e.g. 

^ String
streamContents: [ :stream | 
stream nextPut: …. ]


So I was wondering why we don’t have a construct like Pascals with  to avoid Book1.title, Book1.author etc.

(* book 1 specification *)
With Book1 do
begin
   title  := 'C Programming';
   author := 'Nuha Ali '; 
   subject := 'C Programming Tutorial';
   book_id := 6495407;
end;

I often find it a bit tedious with code like the following which then needs an instvar...

self classes do: [ :class |
| metaclass |
metaclass := class metaclass.
metaclass xxxx.
mataclass yyyy.
]


I’m wondering why we don’t have #with:do:  

class with: class metaclass do: [:metaclass |
metaclass xxx.
]


But when such things aren’t there - there is usually a good reason and I’m curious … this said, there are all kinds of other such tricks (which I rarely use that I keep coming across).

Tim

Reply | Threaded
Open this post in threaded view
|

Re: A "with" construct like Pascal - easy to do, but is it terrible?

Esteban A. Maringolo
Well... you have the cascading that provides you that.

book1
   title:'C Programming';
   author: 'Nuha Ali ';
   subject: 'C Programming Tutorial';
   book_id: 6495407.

What I would like, sometimes, is the option to nest cascades, very
much like the WITH DO construct of Pascal, Basic and others.

But also I take this as a hint of some refactoring needed in my objects.

Regards,

Esteban A. Maringolo

El lun., 4 mar. 2019 a las 10:07, Tim Mackinnon (<[hidden email]>) escribió:

>
> I’ve noticed that as we’ve progressed there has been a move to more concise and fluid code - e.g. I quite like the new String streaming stuff
>
> e.g.
>
> ^ String
> streamContents: [ :stream |
> stream nextPut: …. ]
>
>
> So I was wondering why we don’t have a construct like Pascals with  to avoid Book1.title, Book1.author etc.
>
> (* book 1 specification *)
> With Book1 do
> begin
>    title  := 'C Programming';
>    author := 'Nuha Ali ';
>    subject := 'C Programming Tutorial';
>    book_id := 6495407;
> end;
>
>
> I often find it a bit tedious with code like the following which then needs an instvar...
>
> self classes do: [ :class |
> | metaclass |
> metaclass := class metaclass.
> metaclass xxxx.
> mataclass yyyy.
> ]
>
>
> I’m wondering why we don’t have #with:do:
>
> class with: class metaclass do: [:metaclass |
> metaclass xxx.
> ]
>
>
> But when such things aren’t there - there is usually a good reason and I’m curious … this said, there are all kinds of other such tricks (which I rarely use that I keep coming across).
>
> Tim
>

Reply | Threaded
Open this post in threaded view
|

Re: A "with" construct like Pascal - easy to do, but is it terrible?

Sven Van Caekenberghe-2
In reply to this post by Tim Mackinnon
There is #in: that does exactly that:

class metaclass in: [ :metaclass |
  metaclass xxx.
  metaclass yyy ].

This is quite similar to lisp's let.

You can do almost anything with blocks/closures.

Although not relevant in most cases, there is a small performance price to pay - but again this is only relevant inside performance critical loops.

> On 4 Mar 2019, at 14:06, Tim Mackinnon <[hidden email]> wrote:
>
> I’ve noticed that as we’ve progressed there has been a move to more concise and fluid code - e.g. I quite like the new String streaming stuff
>
> e.g.
>
> ^ String
> streamContents: [ :stream |
> stream nextPut: …. ]
>
>
> So I was wondering why we don’t have a construct like Pascals with  to avoid Book1.title, Book1.author etc.
>
> (* book 1 specification *)
> With Book1 do
> begin
>
>    title  
> := 'C Programming';
>
>    author
> := 'Nuha Ali ';
>  
>    subject
> := 'C Programming Tutorial';
>
>    book_id
> := 6495407;
> end;
>
> I often find it a bit tedious with code like the following which then needs an instvar...
>
> self classes do: [ :class |
> | metaclass |
> metaclass := class metaclass.
> metaclass xxxx.
> mataclass yyyy.
> ]
>
>
> I’m wondering why we don’t have #with:do:  
>
> class with: class metaclass do: [:metaclass |
> metaclass xxx.
> ]
>
>
> But when such things aren’t there - there is usually a good reason and I’m curious … this said, there are all kinds of other such tricks (which I rarely use that I keep coming across).
>
> Tim
>


Reply | Threaded
Open this post in threaded view
|

Re: A "with" construct like Pascal - easy to do, but is it terrible?

Christian Haider
Or #ifNotNil:

class metaclass ifNotNil: [ :metaclass |
   metaclass xxx.
   metaclass yyy ].

With the added benefit to not do anything with nil.

Christian

> -----Ursprüngliche Nachricht-----
> Von: Pharo-users <[hidden email]> Im Auftrag von
> Sven Van Caekenberghe
> Gesendet: Montag, 4. März 2019 14:35
> An: Any question about pharo is welcome <[hidden email]>
> Betreff: Re: [Pharo-users] A "with" construct like Pascal - easy to do, but is it
> terrible?
>
> There is #in: that does exactly that:
>
> class metaclass in: [ :metaclass |
>   metaclass xxx.
>   metaclass yyy ].
>
> This is quite similar to lisp's let.
>
> You can do almost anything with blocks/closures.
>
> Although not relevant in most cases, there is a small performance price to
> pay - but again this is only relevant inside performance critical loops.
>
> > On 4 Mar 2019, at 14:06, Tim Mackinnon <[hidden email]> wrote:
> >
> > I’ve noticed that as we’ve progressed there has been a move to more
> concise and fluid code - e.g. I quite like the new String streaming stuff
> >
> > e.g.
> >
> > ^ String
> > streamContents: [ :stream |
> > stream nextPut: …. ]
> >
> >
> > So I was wondering why we don’t have a construct like Pascals with  to
> avoid Book1.title, Book1.author etc.
> >
> > (* book 1 specification *)
> > With Book1 do
> > begin
> >
> >    title
> > := 'C Programming';
> >
> >    author
> > := 'Nuha Ali ';
> >
> >    subject
> > := 'C Programming Tutorial';
> >
> >    book_id
> > := 6495407;
> > end;
> >
> > I often find it a bit tedious with code like the following which then needs an
> instvar...
> >
> > self classes do: [ :class |
> > | metaclass |
> > metaclass := class metaclass.
> > metaclass xxxx.
> > mataclass yyyy.
> > ]
> >
> >
> > I’m wondering why we don’t have #with:do:
> >
> > class with: class metaclass do: [:metaclass |
> > metaclass xxx.
> > ]
> >
> >
> > But when such things aren’t there - there is usually a good reason and I’m
> curious … this said, there are all kinds of other such tricks (which I rarely use
> that I keep coming across).
> >
> > Tim
> >
>



Reply | Threaded
Open this post in threaded view
|

Re: A "with" construct like Pascal - easy to do, but is it terrible?

Tim Mackinnon
In reply to this post by Sven Van Caekenberghe-2


On 4 Mar 2019, at 13:34, Sven Van Caekenberghe <[hidden email]> wrote:

There is #in: that does exactly that:


Classic - it was there - you just needed to get the name right. thanks.
Reply | Threaded
Open this post in threaded view
|

Re: A "with" construct like Pascal - easy to do, but is it terrible?

Tim Mackinnon
In reply to this post by Christian Haider


On 4 Mar 2019, at 13:59, Christian Haider <[hidden email]> wrote:

Or #ifNotNil:

When you are in the right mindset - lots of things come out of the wood work...
Reply | Threaded
Open this post in threaded view
|

Re: A "with" construct like Pascal - easy to do, but is it terrible?

Herby Vojčík
In reply to this post by Esteban A. Maringolo
On 4. 3. 2019 14:15, Esteban Maringolo wrote:

> Well... you have the cascading that provides you that.
>
> book1
>     title:'C Programming';
>     author: 'Nuha Ali ';
>     subject: 'C Programming Tutorial';
>     book_id: 6495407.
>
> What I would like, sometimes, is the option to nest cascades, very
> much like the WITH DO construct of Pascal, Basic and others.

Shameless plug: https://blog.herby.sk/post/customizing-cascade.

Some year(s) ago I did a poll in twitter in ppl would like this in
Amber. It was 5 for, 5 against.

Herby

>
> But also I take this as a hint of some refactoring needed in my objects.
>
> Regards,
>
> Esteban A. Maringolo

Reply | Threaded
Open this post in threaded view
|

Re: A "with" construct like Pascal - easy to do, but is it terrible?

Richard O'Keefe
In reply to this post by Tim Mackinnon
The Pascal 'with' statement is among the most hated features of
Pascal and was not copied in successor languages like Ada or Modula-2.
It was typically replaced by record constructors, such as
  Book1 := Book[title   : 'C Programming';
                author  : 'Nuha Ali ';
                subject : 'C Programming Tutorial';
                book_id : 6495407];
which happens to be Pascal (ISO10206).

In
  self classes do: [:class |
| metaclass |
metaclass := class metaclass.
metaclass xxxx.
mataclass yyyy.
  ]
"metaclass" is a a block temporary, not an instance variable,
and
   self classes do: [:each | (each metaclass) xxxx; yyyy].
suffices.

We observe that
  [:x1 ... :xn | ...] value: e1 ... value: en
is nothing other than a LET expression wearing a funny hat
and should be inlined by a compiler -- mine does -- and that
  e1 in: [:x1 | ...]
is just a "flipped" version of [:x1 | ...] value: e1, so
there's no reason why a compiler shouldn't inline that.
We also observe that
  e0 m1; ... ; mn
can -- at least in my experience -- be most simply implemented
as [:t | t m1. ... t mn] value: e0
followed by the usual inlining of that.
So we might expect that some day
  self classes do: [:each | each metaclass in: [:t | t xxxx. t yyyy]]
would generate the *same* code as the cascaded version.

Right now, do whichever is clearer.

On Tue, 5 Mar 2019 at 02:07, Tim Mackinnon <[hidden email]> wrote:
I’ve noticed that as we’ve progressed there has been a move to more concise and fluid code - e.g. I quite like the new String streaming stuff

e.g. 

^ String
streamContents: [ :stream | 
stream nextPut: …. ]


So I was wondering why we don’t have a construct like Pascals with  to avoid Book1.title, Book1.author etc.

(* book 1 specification *)
With Book1 do
begin
   title  := 'C Programming';
   author := 'Nuha Ali '; 
   subject := 'C Programming Tutorial';
   book_id := 6495407;
end;

I often find it a bit tedious with code like the following which then needs an instvar...

self classes do: [ :class |
| metaclass |
metaclass := class metaclass.
metaclass xxxx.
mataclass yyyy.
]


I’m wondering why we don’t have #with:do:  

class with: class metaclass do: [:metaclass |
metaclass xxx.
]


But when such things aren’t there - there is usually a good reason and I’m curious … this said, there are all kinds of other such tricks (which I rarely use that I keep coming across).

Tim