Recent

Author Topic: weird error message  (Read 5838 times)

airpas

  • Full Member
  • ***
  • Posts: 179
weird error message
« on: February 19, 2015, 11:39:07 am »
Hi
accessing private members should generate error message like "error: [member] is private" .
but fpc says :

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 918
    • Burdjia
Re: weird error message
« Reply #1 on: February 19, 2015, 01:06:01 pm »
Not really.

"PRIVATE" is part of how Object Pascal implements the encapsulation property of Oriented Programming.  Encapsulation means that you don't need to know how an object works internally.  Also you really don't need to know if something is private or just doesn't exists as it restults in the same: you can't use to it.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Leledumbo

  • Hero Member
  • *****
  • Posts: 8111
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: weird error message
« Reply #2 on: February 19, 2015, 04:15:37 pm »
Hi
accessing private members should generate error message like "error: [member] is private" .
but fpc says :
Why would "error: [member] is private" be better than the current message? There's no usefulness in knowing a member is private or not, and in fact, you should never know if a member is private. After all, it's private. #Privateception

airpas

  • Full Member
  • ***
  • Posts: 179
Re: weird error message
« Reply #3 on: February 19, 2015, 10:14:25 pm »
i mean the message is not clear enough , if you try other popular compilers (MSVC , GCC  ) , you will see precise messages .
to me "error: s is private" is more precise than "Error: identifier idents no member "s"" .

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2280
    • havefunsoft.com
Re: weird error message
« Reply #4 on: February 19, 2015, 10:26:10 pm »
In object pascal, if accessing to the private members of the class defined at the same unit is allowed.
Except for strict private.
Thus error message "you cannot access the member because it's private" is not valid. It might be "you cannot access the member because it's strict private".

Another point for Pascal being modular language. Any private fields are not-visible outside of the unit. Trying to access them will cause an error "there's no member". (think about compiled .obj/.ppu or .dcu files, where no source is available).
So "strict private" is implemented via the same (scope visibility) mechanism - thus it's hidden.

Unlike Pascal, C++ is not modular... in the end (after processing all C++ codes) it's just a big code listing, where you can "see" "private" fields. The C++ compiler just restricts you from accessing them directly.

Having all  that said in mind. Using "strict private" in object pascal only makes sense for .Net or Java declarations compatibility.

Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Leledumbo

  • Hero Member
  • *****
  • Posts: 8111
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: weird error message
« Reply #5 on: February 20, 2015, 07:33:39 am »
i mean the message is not clear enough , if you try other popular compilers (MSVC , GCC  ) , you will see precise messages .
to me "error: s is private" is more precise than "Error: identifier idents no member "s"" .
Aaaaand, if you get the precise error message like that, what would be the advantage for you? Again, refer to the encapsulation principle. One should NEVER know if a member if private, otherwise, it's not considered private. You can even override private member in descendant classes, the compiler won't complain as if the parent class has the same member as well. For protected/public, it will be an error to do so.

airpas

  • Full Member
  • ***
  • Posts: 179
Re: weird error message
« Reply #6 on: February 20, 2015, 09:23:28 am »
Quote
Aaaaand, if you get the precise error message like that, what would be the advantage for you?
well at least it telling the truth (x is private) , not lying (no member x ) .  8-) . am joking
it dosen't bothering me .  but could some one explain why pascal treats private the same as public in the same unit ?

thanks

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2280
    • havefunsoft.com
Re: weird error message
« Reply #7 on: February 20, 2015, 02:00:58 pm »
but could some one explain why pascal treats private the same as public in the same unit ?
Pascal unit has a "public" part - "interface" and "private" part - "implementation".

Variables, types and constants declare in "implementation" is only available in the code of the implementation.
Private members of a class follows the same rule being considered part of implementation (despite of being declared in interface).

I wish pascal would allow to have "interface" declare in one unit and implemented in another. That might have provide a better control of "interface" changes.
Code: [Select]
unit CommonAPI;

type
  TSomeAPI
  public
     procedure DoSomething;   
  end;

function CreateSomAPI(const param: string): TSomeAPI;

implementation external

end.
and then a separated implementation
Code: [Select]
unit LinuxAPI implements CommonAPI;

interface

implementation

type
  TSomeAPI = class
  private
    oshandle : integer;
  end;

procedure TSomeAPI.DoSomething;   
begin
 ...
end;

function CreateSomAPI(const param: string): TSomeAPI;
begin
  Result:=TSomeAPI.Create;
  Result.oshandle:=sysfuncion(param);
end;

end.
« Last Edit: February 20, 2015, 02:10:51 pm by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

airpas

  • Full Member
  • ***
  • Posts: 179
Re: weird error message
« Reply #8 on: February 20, 2015, 03:34:06 pm »
thanks skalogryz

Quote
I wish pascal would allow to have "interface" declare in one unit and implemented in another
but it becomes complicated like that . i hate c/c++ only for this point (separate header&source)  .
to me , one header+implementation  is one of the best thing in pascal .

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2280
    • havefunsoft.com
Re: weird error message
« Reply #9 on: February 20, 2015, 03:39:19 pm »
but it becomes complicated like that . i hate c/c++ only for this point (separate header&source)  .
to me , one header+implementation  is one of the best thing in pascal .
lol. then you should look through RTL, FCL, LCL sources. C-style header / implementation are all over the place.




« Last Edit: February 20, 2015, 04:28:12 pm by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Leledumbo

  • Hero Member
  • *****
  • Posts: 8111
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: weird error message
« Reply #10 on: February 20, 2015, 05:06:26 pm »
but could some one explain why pascal treats private the same as public in the same unit ?
It's the way unit works. Everything in the same unit are friends. Every language has its own rule regarding identifier visibility and Object Pascal choose it this way, certainly you could ask Anders why he designed the language that way.

From MY point of view, two or more things living in the same unit meaning they're coded together (Object Pascal doesn't have partial unit). Thus, one can always see all of them together. The case is different when they're not in the same unit, since you could only give the compiled unit without source, thus you can only know what's documented. Implementation details are hidden in the forest of binary.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 918
    • Burdjia
Re: weird error message
« Reply #11 on: February 23, 2015, 10:28:05 am »
but it becomes complicated like that . i hate c/c++ only for this point (separate header&source)  .
to me , one header+implementation  is one of the best thing in pascal .
lol. then you should look through RTL, FCL, LCL sources. C-style header / implementation are all over the place.
That's not actually true.  The use of {$Include ...} in the RTL, FCL and LCL isn't to separate header and implementation.  It's done this way to help compiling on different platforms.  When compiling the compiler and the library, there are defined platform-dependent search paths, so it "includes" the apropriate code for each platform (Windows, Linux, Mac, etc).
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal