Recent

Author Topic: Bounded With  (Read 15558 times)

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: Bounded With
« Reply #60 on: July 18, 2017, 08:40:25 am »
By "safe" I understand "auto-free" in the case of exception. FPC (and Delphi too) have "safe" with. The usage is very simple:

Code: Pascal  [Select][+][-]
  1. uses
  2.   SynCommons; // yup. mORMot
  3.  
  4. {...}
  5.  
  6. var
  7.   f: TMyObject;
  8. begin
  9.   with TAutoFree.One(f, TMyObject.Create), f do
  10.   begin
  11.     f.foo := 'some';
  12.     {... unsafe code ...}
  13.   end;
  14.  
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Bounded With
« Reply #61 on: July 18, 2017, 08:52:48 am »
Maybe it is time to give the WITH discussion an own thread ?

mse

  • Sr. Member
  • ****
  • Posts: 286
Re: Bounded With
« Reply #62 on: July 18, 2017, 09:05:43 am »
By "safe" I understand "auto-free" in the case of exception. FPC (and Delphi too) have "safe" with. The usage is very simple:
I mostly use "with" with records.


Thaddy

  • Hero Member
  • *****
  • Posts: 14160
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #63 on: July 18, 2017, 09:05:55 am »
The big pro to me is that i can _T_ell the _C_ompiler _E_xactly _W_hat _I_ _Want
I always prefer that over _D_o _W_hat _I_ _M_ean, because as  I wrote earlier: that leaves room for speculation and can change over the years.
Yeah, right, just like with typecasts: "Type casting is the developers way of telling the compiler to "get out of the way! I know what I'm doing"... and the compiler believes you." ~Allen Bauer 2015
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14160
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #64 on: July 18, 2017, 09:07:19 am »
By "safe" I understand "auto-free" in the case of exception. FPC (and Delphi too) have "safe" with. The usage is very simple:
I mostly use "with" with records.
It also works with interfaces already...
Specialize a type, not a var.

mse

  • Sr. Member
  • ****
  • Posts: 286
Re: Bounded With
« Reply #65 on: July 18, 2017, 09:17:01 am »
By "safe" I understand "auto-free" in the case of exception. FPC (and Delphi too) have "safe" with. The usage is very simple:
I mostly use "with" with records.
It also works with interfaces already...
I mostly use "with" with records and in order to improve performance and readability. You probably know what I mean...

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Bounded With
« Reply #66 on: July 18, 2017, 10:11:32 am »
Maybe it is time to give the WITH discussion an own thread ?
Yes please.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Thaddy

  • Hero Member
  • *****
  • Posts: 14160
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #67 on: July 18, 2017, 10:16:11 am »
Yes plz
Code: [Select]
with > null
Specialize a type, not a var.

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: Bounded With
« Reply #68 on: July 18, 2017, 10:50:47 am »
So the topic is moved I tried some codes for learning of with statement:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   btn : TButton;
  4.   frm : TForm;
  5. begin
  6.   btn := Button1;
  7.   frm := Form1;
  8.   with btn,frm do
  9.   begin
  10.     btn.Caption:='Btn.Test';
  11.     frm.Caption:='frm.Test';
  12.     Caption:='frm is already in with';
  13.   end;
  14. end;

But if you need to changes about with behaviour what about this format:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   btn : TButton;
  4.   frm : TForm;
  5. begin
  6.   with (btn as Button1),
  7.           (frm as Form1) do
  8.   begin
  9.     btn.Caption:='Btn.Test';
  10.     frm.Caption:='frm.Test';
  11.     Caption:='frm is already in with';
  12.   end;
  13. end;
  14.  

But maybe the behaviour should work as::

Code: Pascal  [Select][+][-]
  1.  
  2.  with [Label1,Button1,Form1],
  3.           (xfrm as Form2) do
  4.   begin
  5.     Caption:='all captions changed!';
  6.     xfrm.caption := 'xform caption';
  7.   end;
  8.  

Just an idea.

Thaddy

  • Hero Member
  • *****
  • Posts: 14160
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #69 on: July 18, 2017, 11:05:57 am »
Just an idea  :D
Code: [Select]
with object do begin{
something}with object2 do{somethingelse}end;
:-X :P

Curly brackets seem the way to go to define scope...
« Last Edit: July 18, 2017, 11:08:06 am by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Bounded With
« Reply #70 on: July 18, 2017, 11:14:50 am »
(serious)

If you need a syntax simply a

Code: Pascal  [Select][+][-]
  1.  with l: ttype, y:ttype  do

seems fine with me. It is a convenience syntax.

The trouble is that it only fixes accidental clashes when identifiers exist in both "l" and "y", and introduces a new problem, namely that a small mistake (not prefixing with l or y) still might occasionally compile if the identifier exists in the much larger global scope. The accidental fishing in the global scope isn't fixed at all, and this syntax makes it only more likely. (not on misspelling, but also by accidentally omitting the prefix (l,y) )

At least the default WITH automatically overrides the global scope if identifiers match.

A possible, but a bit heavy handed, solution would be to also have to define a prefix for the global scope, and error on any not prefixed value.

Code: Pascal  [Select][+][-]
  1.  with default std, l: ttype, y:ttype,   do

But that eats away the convenience factor again.

Thaddy

  • Hero Member
  • *****
  • Posts: 14160
  • Probably until I exterminate Putin.
Re: Bounded With
« Reply #71 on: July 18, 2017, 12:39:02 pm »
Yes. Serious. I mentioned that already. "with" is short notation. All suggested alternatives that are scope-safe make notation longer. Which is useless if the alternatives are better. This is a 30+ year discussion. Python got it right because of some very clever whitespace features that will never be available in Pascal (Pascal doesn't know whitespace, it skips it) . (Of course Python's roots are Dutch, 51.7974636,5.4325547,11, not Danish or Swiss :D )
« Last Edit: July 18, 2017, 12:48:01 pm by Thaddy »
Specialize a type, not a var.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: Bounded With
« Reply #72 on: July 19, 2017, 09:18:51 pm »
(serious)

If you need a syntax simply a

Code: Pascal  [Select][+][-]
  1.  with l: ttype, y:ttype  do

seems fine with me. It is a convenience syntax.

The trouble is that it only fixes accidental clashes when identifiers exist in both "l" and "y", and introduces a new problem, namely that a small mistake (not prefixing with l or y) still might occasionally compile if the identifier exists in the much larger global scope. The accidental fishing in the global scope isn't fixed at all, and this syntax makes it only more likely. (not on misspelling, but also by accidentally omitting the prefix (l,y) )

At least the default WITH automatically overrides the global scope if identifiers match.

A possible, but a bit heavy handed, solution would be to also have to define a prefix for the global scope, and error on any not prefixed value.

Code: Pascal  [Select][+][-]
  1.  with default std, l: ttype, y:ttype,   do

But that eats away the convenience factor again.
For me convenience is less important, so i could very much live with it.
I also agree with mse to wait until delphi introduces a safe with statement (probably never).
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

 

TinyPortal © 2005-2018