Recent

Author Topic: Why with allows assignment  (Read 41233 times)

jamie

  • Hero Member
  • *****
  • Posts: 6802
Re: Why with allows assignment
« Reply #15 on: January 02, 2025, 10:33:45 pm »
U have your opinion others have theirs including mine. I see nothing wrong with it and it would break code like it's going out of style otherwise.
Please don't compare d with fpc when d can't do something fpc can.
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #16 on: January 03, 2025, 12:18:49 am »
I see nothing wrong with it and it would break code like it's going out of style otherwise.
Other than FPC doing it _wrong_, there is nothing wrong with it.  Fixing that bug can only break code that is wrong... no great loss there and wrong code should go out of style anyway.


Please don't compare d with fpc when d can't do something fpc can.
I remember at least one case where you compared FPC to Delphi to imply there was a bug in FPC but, of course, it's ok when you do it but not others.   
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6802
Re: Why with allows assignment
« Reply #17 on: January 03, 2025, 12:45:03 am »
I see nothing wrong with it and it would break code like it's going out of style otherwise.
Other than FPC doing it _wrong_, there is nothing wrong with it.  Fixing that bug can only break code that is wrong... no great loss there and wrong code should go out of style anyway.


Please don't compare d with fpc when d can't do something fpc can.
I remember at least one case where you compared FPC to Delphi to imply there was a bug in FPC but, of course, it's ok when you do it but not others.

 You say whatever you want but if compiler can do something useful where the other cannot, I prefer that to be a bonus.

 And yes, there are things D can do where FPC still in the back on and it's been demonstrated many times only to get slapped back telling me and others that is the way it is, so you need to do extra work to overcome issues that don't work as expected but works fine in Delphi.

 I won't get into this because it looks like someone wants to start a flame war on something just because they fell, they can but facts are facts.

As for the WITH control statement, I've ported D code over that uses that WITH with returning items from a function that seem to work just fine in D and also FPC. Basic facts about WITH is it creates a local branch of identifiers. In this case a RECORD where that record maybe referenced several times read or written while inside the WITH.

 I get the impression maybe some language features are misunderstood so some would rather have it removed instead of grasping it.


The only true wisdom is knowing you know nothing

dbannon

  • Hero Member
  • *****
  • Posts: 3230
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Why with allows assignment
« Reply #18 on: January 03, 2025, 12:52:47 am »
Are we over thinking this ? The OP wants to return a record's contents, thats easy. He/she has become confused by using "with" and trying, apparently, to apply it to a function. Drop the "with", its use is almost always confusing and this works fine :

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}   // Note !
  3.  
  4. type
  5.   TTest = record
  6.     V: Integer;
  7.         S : string;
  8.   end;
  9.  
  10. var
  11.   X: TTest;
  12.  
  13.   function Test: TTest;
  14.   begin
  15.         result := X;
  16.   end;
  17.  
  18. var
  19.   T: TTest;
  20. begin
  21.         X.V := 27;
  22.         X.S := 'hello';
  23.         T := Test();
  24.         writeln(T.S, ' ', T.V);
  25. end.          
  26.  

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #19 on: January 03, 2025, 01:53:47 am »
You say whatever you want but if compiler can do something useful where the other cannot, I prefer that to be a bonus.
It's not a bonus, it's a bug.  "with" is simply shorthand to specify a scope for one or more statements.  Whether the scope is specified using a "with" or explicitly in the statement should not make any difference.  FPC is _wrong_, period.  That's not an opinion, it's a fact.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

egsuh

  • Hero Member
  • *****
  • Posts: 1534
Re: Why with allows assignment
« Reply #20 on: January 03, 2025, 02:29:09 am »
I love "with". Especially "with" is convenient in following cases. 

Code: Pascal  [Select][+][-]
  1.    with AForm.create (nil) do begin
  2.        Fill_AForms_Values_If_Necessary;
  3.        if ShowModal then begin
  4.           Fill_In_Variables_Of_Calling_Unit_From_AForm;
  5.        end;
  6.        Free;
  7.     end;
  8.  
  9.      with AClass.Create do begin
  10.            Load_Content_Form_DataBase (Key_Field_Values);
  11.            Do_Operations:
  12.            Save_To_DataBase(Key_Field_Values);
  13.  
  14.            Free;
  15.      end;


With advanced records, I can define methods within it to retrieve or save contents to database, etc. just like AClass in the previous case.

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: Why with allows assignment
« Reply #21 on: January 03, 2025, 03:18:18 pm »
didn't OP simly made the error that it should be
Code: Pascal  [Select][+][-]
  1. T.V
That code also fails with Delphi.
But I am sure they don't want the Trumps back...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5870
  • Compiler Developer
Re: Why with allows assignment
« Reply #22 on: January 03, 2025, 04:04:39 pm »
Why the first test raise no error like the second one?

Because for with it's simply not implemented yet. After all from a language point of view both are valid. However current Delphi versions forbid both due to wrong expectations of the users and FPC up to now only errors out on the function result, because it's something that the compiler needs to actively check for.

And on the same topic, is there a way in Pascal that I can return a record by the reference so any change to the result, changes the source?

Use a managed record.

As for the WITH control statement, I've ported D code over that uses that WITH with returning items from a function that seem to work just fine in D and also FPC. Basic facts about WITH is it creates a local branch of identifiers. In this case a RECORD where that record maybe referenced several times read or written while inside the WITH.

*sigh* Current Delphi versions forbid this code as well, because it does not do what you expect it to do.

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: Why with allows assignment
« Reply #23 on: January 03, 2025, 06:34:24 pm »
Yes. For quite some versions btw.
It was their bug, not ours.
But I am sure they don't want the Trumps back...

VisualLab

  • Hero Member
  • *****
  • Posts: 638
Re: Why with allows assignment
« Reply #24 on: January 03, 2025, 07:52:24 pm »
In Delphi, both the assignment within the WITH and the direct assignment are indicated as errors.
I believe (but I am the last one to give credence to) that a similar situation should be handled and the error should be reported to the developer.
That assignment is an error, and therefore it should be reported, even if it does not produce any effect, and that is precisely the point: the developer writing that code expects it to be executed (ok, he wrote a stupid thing) and the fact that it is not executed (or that it cannot be executed) MUST be reported by the compiler.

I confirm. I just checked this code in Delphi 12. The compiler reports 2 errors in both assignments:
Quote
with Test do
    V := 1; //No Error
  Test.V := 1; //Error: Argument cannot be assigned to



Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. { removed }
  4.  
  5. var
  6.   T: TTest;
  7. begin
  8.   with Test do
  9.     V := 1; //No Error
  10.   Test.V := 1; //Error: Argument cannot be assigned to
  11. end.                

Why the first test raise no error like the second one?
I believe you found a compiler bug.  "with Test do V := 1;" should be _exactly_ the same as "Test.V := 1;".  "with" is basically a way to abbreviate, it's not supposed to have any side effect.  IMO and formally, that is a bug.

Let's see what the FPC developers say.

I see nothing wrong with it and it would break code like it's going out of style otherwise.
Other than FPC doing it _wrong_, there is nothing wrong with it.  Fixing that bug can only break code that is wrong... no great loss there and wrong code should go out of style anyway.

Please don't compare d with fpc when d can't do something fpc can.
I remember at least one case where you compared FPC to Delphi to imply there was a bug in FPC but, of course, it's ok when you do it but not others.

You say whatever you want but if compiler can do something useful where the other cannot, I prefer that to be a bonus.
It's not a bonus, it's a bug.  "with" is simply shorthand to specify a scope for one or more statements.  Whether the scope is specified using a "with" or explicitly in the statement should not make any difference.  FPC is _wrong_, period.  That's not an opinion, it's a fact.

I fully agree with 440bx that this is a bug in FPC. And I hope it will be fixed in future versions.



Perhaps in FPC (and Lazarus) a compilation switch would be useful, allowing you to disable the use of "with" in the code (report its presence as an error) in the project options.

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Why with allows assignment
« Reply #25 on: January 03, 2025, 08:07:38 pm »
Pretty simple, with creates a new temporary variable where the function result is copied into. This is intended because the object must be persistent in the with block:
Code: Pascal  [Select][+][-]
  1. with Test do
  2. begin
  3.   V:=42;
  4.   WriteLn(V);
  5. end;

This is not a bug, it is specifically mentioned that this should be possible in a comment in the source of the Compiler. So someone built it this way to make this possible

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #26 on: January 03, 2025, 08:39:51 pm »
Pretty simple, with creates a new temporary variable where the function result is copied into.
That's nonsense.

"with" doesn't create anything.  The "with" statement is used to establish a scope, _nothing_ more, nothing less.  It's information for the parser.

The result of "<scope_identifier>.<identifier> := ..." must be _identical_ to "with <scope_identifier> do <identifier> := ..." because these statements _MUST_ be semantically identical.   "with" is just convenient shorthand to apply a scope identifier to multiple statements (sorely missing in C/C++). 

The results obtained in either case MUST be identical. ANY functional difference is a _bug_.  period.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Why with allows assignment
« Reply #27 on: January 03, 2025, 08:57:44 pm »
It creates a scope for an object. If the object is referencable it just creates a scope around the existing object. If it is the result of some expression, it copies it into a new temporary object to make it referencable: https://gitlab.com/freepascal.org/fpc/source/-/blob/main/compiler/pstatmnt.pas#L659

VisualLab

  • Hero Member
  • *****
  • Posts: 638
Re: Why with allows assignment
« Reply #28 on: January 03, 2025, 09:20:53 pm »
It creates a scope for an object. If the object is referencable it just creates a scope around the existing object. If it is the result of some expression, it copies it into a new temporary object to make it referencable: https://gitlab.com/freepascal.org/fpc/source/-/blob/main/compiler/pstatmnt.pas#L659

I understand that, but it's still a quirk handled by the compiler. I agree with:

"with" doesn't create anything.  The "with" statement is used to establish a scope, _nothing_ more, nothing less.  It's information for the parser.



Apart from the fact that it can be done technically (the compiler supports it), is there any deeper justification for the existence of such language constructs? I.e., does it have any significant/practical use, is it useful for something serious? Because at the moment it seems to be just a technical curiosity, without practical application (and undermining Pascal's clarity and unambiguity).

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #29 on: January 03, 2025, 09:23:39 pm »
It creates a scope for an object. If the object is referencable it just creates a scope around the existing object. If it is the result of some expression, it copies it into a new temporary object to make it referencable: https://gitlab.com/freepascal.org/fpc/source/-/blob/main/compiler/pstatmnt.pas#L659
What FPC is doing is simply _wrong_.  The presence or absence of a "with" should _never_ alter the semantics.  If it does, it's a bug, it's that simple.

In addition to that, outside the scope of a function, a function identifier cannot be used as an lvalue, that doesn't make any sense, which seems to be what FPC is doing in the presence of the "with".  The "with" isn't creating a temporary, a temporary may be created to hold the result of a function but that's totally independent of the presence or absence of a "with" statement".
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018