with Object do

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

with Object do

Postby johannes » Thu Mar 20, 2008 6:10 am

I would really love a feature like the "with .. do" feature of Object Pascal.
It takes an Object as an argument and works more or less in the "Namespace" of this object
for example instead of
Code: Select all
myObject.colour := clRed;
myObject.size   := 23.5;   := 'Fred';

you can write
Code: Select all
With myObject do
  colour := clRed;
  size   := 23.5;
  name   := 'Fred';

It would be even cooler if you could do something like "with myObject1 myOtherObject do" so that both namespaces would be mixed.

The problem is that the code would be a bit less readable with that :( And there will be Problems if 2 or more methods share the same signature...
Posts: 8

Re: with Object do

Postby Charles » Thu Mar 20, 2008 9:28 am

In addition to the problems you mentioned, I have shied away from this because of how easy it is to workaround. You can assign the object of interest to a short variable name:
# given the lack of this:
with node.parent
name = 'whatever'
score = 0

# you can take this approach:
p = node.parent = 'whatever'
p.score = 0

Pascal doesn't have such easy/slim/localized variable declarations so "with" is more desirable in that language.
Posts: 2514
Location: Los Angeles, CA

Re: with Object do

Postby Peufeu » Sat Apr 12, 2008 8:12 am

I like the "With" construct, here is a proposal to make it cleaner :

Temporary variable aliases, limited in scope

Code: Select all
with myObject a, myOtherObject.someMember b
  a.colour = clRed;
  a.size   = 23.5;   = 'Fred';
  a.someField = b.someField = +
  .objects.add( a )

IMHO typing an extra "a." is not that difficult, it looks nice, reads well, and doesn't mess with the ". == self." syntax which is a great idea.
In this case a and b are just aliases for expressions or variables that get wiped out when exiting the code block below the with.

A variable would be simply aliased, but the result of a function call would be stored in a temporary variable and destroyed at the end. This opens up a very nice syntax :

Code: Select all
with openfile( fooname ) as f, getLock() as lock
  do something with the file

here after this block has executed, f and lock are destroyed which closes the file and releases the lock. It couldn't be any more simpler, much better than Try/Finally.
Posts: 2

Re: with Object do

Postby Charles » Sun Apr 13, 2008 10:55 am

There is already a "using" statement, although it does not change scope:
using sr = File.openText(fileName)
print sr.readLine

# "using" is a convenience for:
sr = File.openText(fileName)
print sr.readLine
if sr is not nil
sr = nil

It is a convenience for those classes that implement IDisposable which typically includes file I/O, database connections, etc.

The scope issue could be treated separately and as something that can be combined with using:
scoped c = Customer() = name
c.url = url
print c # <-- error because 'c' is not visible

# ...

using scoped sr = File.openText(.fileName)
print sr.readLine

I don't know if "scoped" is really the best name.

But every time I've found myself wanting this in Cobra, it's been because my method was too long and ugly.

Or, instead of this "scoped" idea, "using" could be changed to work on anything and limit scope as well. And we could call it "with". :-)

I guess limiting syntactic scope and calling .dispose can be seen as related.

This is an interesting idea. I want to think about it some more.
Posts: 2514
Location: Los Angeles, CA

Return to Discussion

Who is online

Users browsing this forum: No registered users and 6 guests