Recent

Author Topic: Free Pascal Compiler 2.4.0 Released!  (Read 27352 times)

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: Free Pascal Compiler 2.4.0 Released!
« Reply #15 on: January 05, 2010, 09:49:29 am »
(this is a contrived example, but in complex programs similar constructions can easily occur).

Really?  I don't think I've ever seen something like that happening in real-world code.  A function shouldn't be trying to modify a direct reference to a variable it's receiving as a parameter.  (Or, depending on how you look at it, a user shouldn't go passing something as a parameter to a method that the method already has a direct reference to.) Where have you seen something like that happen?

In the FPC compiler itself, albeit with a global variable that's (sometimes) passed as var-parameter to a function that also directly uses this global variable. The principle is the same though.

I agree that in general this is bad practice, but with OOP it can easily happen in case
a) a method may be used to operate on different fields (so instead of writing one version of the method per field, or hardcoding the different fields in the method and passing some selector parameter to the method to indicate on which field it should operate, you pass the field it should operate on by var-parameter)
b) this method calls other methods that work directly with the various fields (i.e., those other methods don't get the field passed by var-parameter)

It certainly is possible to rewrite such code (e.g., by changing the interface of the secondary methods to also take their input via var-parameters), and that may even be the right thing to do, but the original code is still valid Pascal with behaviour that's fully defined.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Free Pascal Compiler 2.4.0 Released!
« Reply #16 on: January 05, 2010, 02:24:45 pm »
I had problems with object encapsulation with things like this:
Code: [Select]
   TMyClass = [b]class[/b]   
      ...
      [b]property[/b] MyNumber: Integer [b]read[/b] FMyNumber [b]write[/b] SetMyNumber;
   [b]end;[/b]

   [b]procedure[/b] Calculate([b]var[/b] AnInt: Integer);
   [b]begin[/b]
   ...
   AnInt := LocalVar;
   [b]end;[/b]

   ......
   [b]begin[/b]
   ...
   Calculate(MyClass.MyNumber); // this modifies the Property MyNumber without calling the setter
   ...
   [b]end;[/b]


I was very concerned when I realized that the setter was not executed!!!

What I need to keep my code clean is that the Setter/Getter executed inside those procedure/functions without using temporary variables, but I don't know if such things are possible ...

LazaruX

  • Hero Member
  • *****
  • Posts: 597
  • Lazarus original cheetah.The cheetah doesn't cheat
Re: Free Pascal Compiler 2.4.0 Released!
« Reply #17 on: January 05, 2010, 02:36:59 pm »
BTW the links in the first post are not correct, you should remove the http:
;-)

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: Free Pascal Compiler 2.4.0 Released!
« Reply #18 on: January 05, 2010, 02:37:40 pm »
What I need to keep my code clean is that the Setter/Getter executed inside those procedure/functions without using temporary variables, but I don't know if such things are possible ...

Not without using higher level concepts than those normally used in Pascal, such as the ability to treat language expressions as first class elements in the language (so you can basically pass a reference to an expression as parameter). It might also be possible to implement this sort of things based on whatever infrastructure is required for anonymous functions/closures, but FPC doesn't support any of that yet.

 

TinyPortal © 2005-2018