Recent

Author Topic: Bounded With  (Read 15589 times)

AlexK

  • Jr. Member
  • **
  • Posts: 55
Re: Bounded With
« Reply #15 on: July 12, 2017, 10:19:35 am »
Uhhmmmmm, NO. It has the exact same design purpose. Only in Python it is easier to spot scoping. Because it is Python. Indentation, silly.... <grumpy >:D>

You stubbornly refuse to understand, like people that don't even want to hear about anything with a word "Pascal" in it.
WITH in Python represents some kind of short transactions. It's not a shortcut to instance members, but a fundamental thought through construct.

This construct can be explained in terms of an industrial machine, which must execute some procedures in a checked/ensured context:
Code: Python  [Select][+][-]
  1. with SomeMachinePart as ctx1, SimplePreparationDevice, SomeSensor as ctx2:
  2.     # calls some procedures, use ctx1 and ctx2
  3.  
  4. # What it does:
  5. ctx1 = SomeMachinePart.__enter__()
  6. SimplePreparationDevice.__enter__()
  7. ctx2 = SomeSensor.__enter__()
  8. # calls some procedures, use ctx1 and ctx2
  9. SomeSensor.__exit__()
  10. SimplePreparationDevice.__exit__()
  11. SomeMachinePart.__exit__()
  12.  

I have a vague understanding how to implement it in a compiler for a lang with static types.
I know how to implement it in Lisp right away, with a macro.
« Last Edit: July 12, 2017, 10:40:02 am by AlexK »

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #16 on: July 12, 2017, 10:24:45 am »
That's just cluttering syntax. Study language design. I stick to what  wrote. You lack knowledge. You can have an opinion, but please have an informed opinion.
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Bounded With
« Reply #17 on: July 12, 2017, 10:39:21 am »
I don't know much about Python, but I think what AlexK said about 'with', is this one:
http://effbot.org/zone/python-with-statement.htm

I can't understand it, that because I never learned Python. So, I'll let more experienced seniors to decide it is good or not.

But if someone want to suggest new features, perhaps (s)he should read this page first:
http://wiki.freepascal.org/Modernised_Pascal

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Bounded With
« Reply #18 on: July 12, 2017, 10:45:17 am »
A WITH statement with pseudo variables has been implemented by several other languages (since the eighties, nothing new in Python).

VB had a kind of syntax that could prefix identifiers with ..

However, with is in general simply a bad idea. Forget the prefix, and you still accidentally will grab the field name. It mixes scopes, one of which (the active one on the point of the WITH statement) is potentially vast, and the consequences are hard to commit.

That doesn't mean that WITH is diabolical, just don't overuse it, and give it some attention if you must use it. Since the chance on error is fairly small, small scale usage isn't usually a problem. (and static analysis tools could point out clashes)
« Last Edit: July 12, 2017, 10:46:52 am by marcov »

AlexK

  • Jr. Member
  • **
  • Posts: 55
Re: Bounded With
« Reply #19 on: July 12, 2017, 10:50:55 am »
I don't know much about Python, but I think what AlexK said about 'with', is this one:
http://effbot.org/zone/python-with-statement.htm

I can't understand it, that because I never learned Python. So, I'll let more experienced seniors to decide it is good or not.

Code: Python  [Select][+][-]
  1. with SomeMachinePart as ctx1, SimplePreparationDevice, SomeSensor as ctx2:
  2.     # calls some procedures, use ctx1 and ctx2
  3.  
  4. # What that statement does:
  5. ctx1 = SomeMachinePart.__enter__()
  6. SimplePreparationDevice.__enter__()
  7. ctx2 = SomeSensor.__enter__()
  8. # calls some procedures, use ctx1 and ctx2
  9. SomeSensor.__exit__()
  10. SimplePreparationDevice.__exit__()
  11. SomeMachinePart.__exit__()
  12.  

It can be used with graphics, OpenGL for example is a state machine.
If any __enter__method or suite raises an Exception, that exception's instance is passed to __exit__methods as arguments.
__enter__ and __exit__ methods are special methods that must be implemented in classes eligible to appear in a WITH statement.
In Object Pascal some kind of IContextManager interface, and I was thinking of compiler logic to implement it in a static lang.
This concept can help organize manual memory management in some general cases, implement transactions(rollback), machine control.

But if someone want to suggest new features, perhaps (s)he should read this page first:
http://wiki.freepascal.org/Modernised_Pascal

Interesting, I'll check it. Wiki format is good too. Python uses systematic process, PEP - Python Enhancement Proposals, each proposal is assigned a number to refer in discussions and "what's new" reports if a PEP was implemented and not rejected. So, each PEP must be formatted as a some kind of "business plan".
« Last Edit: July 12, 2017, 11:10:28 am by AlexK »

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Bounded With
« Reply #20 on: July 12, 2017, 10:56:14 am »
debugging bugs? what debugging bugs? The problem with the "with" statement, as far as I understand it, was one of scope, if it was ever bug it was in our ability to comprehend not the with statement.

Check this link https://tinyurl.com/ycarjm4y (Don't worry, you're not going to get RickRolled!)
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #21 on: July 12, 2017, 10:57:37 am »
Or to put it more bluntly: with<something> do <something else that can do something with something>. Now...? Is there any difference? Of course not!
Maybe open a new sub forum on language design. Such things are more fundamental than most of these links warrant.
« Last Edit: July 12, 2017, 11:01:04 am by Thaddy »
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Bounded With
« Reply #22 on: July 12, 2017, 11:08:50 am »
Can someone show me the code that contains 'with' bug in free pascal? I use 'with' properly and I found that it's not only makes you less typing but also the code becomes more readable.

Perhaps someone does not understand 'last to first' concept of 'with' statement, which is explained in this page:

https://www.freepascal.org/docs-html/ref/refsu62.html

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #23 on: July 12, 2017, 11:12:11 am »
Code: Pascal  [Select][+][-]
  1. with label1 do with label2 do caption := 'test';
  2.  
Nuff?? < just slightly grumpy, Handoko  >:D >:DO:-)
« Last Edit: July 12, 2017, 11:17:08 am by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Bounded With
« Reply #24 on: July 12, 2017, 11:17:40 am »
Can someone show me the code that contains 'with' bug in free pascal? I use 'with' properly and I found that it's not only makes you less typing but also the code becomes more readable.

Perhaps someone does not understand 'last to first' concept of 'with' statement, which is explained in this page:

https://www.freepascal.org/docs-html/ref/refsu62.html

Code: Pascal  [Select][+][-]
  1. with bla do
  2.   ffield:=3;
  3.  

If the class you are using has a field(and thus in the global scope), but BLA has the same or very similar too, that is dangerous. Any misspelling (like using underscores inbetween, "f" or other prefixes or not) can introduce very hard to find bugs.  This often doesn't happen in the initial code writing, but later when you go back to it and you have a less then complete overview (even more so if you didn't write it yourself to begin with)

It is at least a reason to keep a bit of a lid  on WITH usage, and keep the use cases straight and single (one expression only, no nesting etc)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Bounded With
« Reply #25 on: July 12, 2017, 11:21:06 am »
But that is not bug, right? It is the programmer problem.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Bounded With
« Reply #26 on: July 12, 2017, 11:24:11 am »
But that is not bug, right? It is the programmer problem.

Yes. But the WITH statement is said to invite programmer's errors too much. In such cases, there is always an amount of personal preferences involved, but still that seems to be the consensus.

But as so often (see e.g. GOTO) this creates a whole cult that daemonizes it. Just like with GOTO, just be careful with it. If you program Pascal long enough overuse will bite :-)
« Last Edit: July 12, 2017, 11:25:56 am by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #27 on: July 12, 2017, 11:25:21 am »
Yes... but with is ambiguous. It is not only a programmers problem, but a language design problem...
I seem to agree with Marco again  8-) Are these examples clear?
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Bounded With
« Reply #28 on: July 12, 2017, 11:26:20 am »
Yes... but with is ambiguous. It is not only a programmers problem, but a language design problem...

That is a perfectly valid opinion. (of a cultist :_)

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #29 on: July 12, 2017, 11:27:36 am »
Grrr... Oh well...Point taken... :o
« Last Edit: July 12, 2017, 11:29:17 am by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018