Recent

Author Topic: Quick Modern Object Pascal Introduction, for Programmers  (Read 46339 times)

BeniBela

  • Hero Member
  • *****
  • Posts: 908
    • homepage
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #15 on: June 23, 2016, 04:54:13 pm »
 
  Numerous examples show that we simply add strings, and pass them around to/from functions.

It is a trap.

You make all your functions for string, and then you get a pchar and cannot use it with any of them

Better use only pchar for functions

Quote
The as and is operators also work on COM interfaces.
:D

But it is a bad idea to use them

They are too slow

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #16 on: June 23, 2016, 05:32:49 pm »
Sorry, I don't think that I read it selectively, I read it thoroughly several times.
Oops, sorry. that might have come across as an accusation (which it was not meant to be).

I'm more then positive that you've read the documentation thoroughly.

Quote
You really think that it says to the reader that CORBA interfaces can use "is" operator, as well as COM interfaces?
No, it does not say that nor did i ever mentioned that it does.

There is a shitload more things not written in the official documentation so that it does not get mentioned. Does that mean it is wrong on all those accounts simply because it ain't mentioned ?

Quote
There is nothing about "is" operator and corba.
That wasn't my point. Point was that it explains that support for corba interfaces is present.

Quote
I am still confused...
1. The official documentation regarding the class operator is does not specifically mention corba interfaces
2. The official documentation regarding the class operator is does specifically mention COM interfaces
3. You asked why corba interfaces work with is operator
4. i tried to explain why by reading the official documentation and point out what _might_ be the reason for that.
5. In the process i confused user Zoran

I really don't have any idea what more i could add to be able to lift the confusion.

In case you feel 'betrayed' because the official documentation regarding the is operator does not state specifically that it also works with corba, then please feel free to file a bugreport on mantis ?

But it is a bad idea to use them

They are too slow
Well, if i remember correctly i did not mention explicitly that you should use it :)

Thanks for the warning.

michalis

  • Full Member
  • ***
  • Posts: 140
    • Castle Game Engine
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #17 on: June 24, 2016, 02:32:32 am »
Wow, thank you everyone for the comments! Here's a large answer:)

Quote from: marcov
I also would remove the apologetic "reasonable", and refer to Pascal's  "declare before usage" principle. ( I assume it is somewhere mentioned near the beginning).

I reworded and shortened this part a bit. Thanks:)

It was apologetic, because indeed other languages do not have this limitation --- e.g. in Java you also usually need to declare stuff before using it, and yet two classes, in two separate files, can freely use each other. In exchange, in Java you can easily get a run-time error if you don't recompile everything after changing some class. The compiler does not track dependencies, you decide what and when to recompile.

Clearly, the Pascal has advantages here, giving you a compilation that is fast and also guaranteed to be correct, with all identifier references checked at compile-time. But for this, we sacrifice the possibility of circular dependencies, and I felt it should be explained:)

Anyway, my clumsy reasoning in the document was probably not helpful here, so I shortened it.

Quote from: marcov
Qualified identifier usage, and, related, type aliases.  (type xx = unity.xx)

Good idea, thank you - I described both these things in new sections:

http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#_qualifying_identifiers_with_unit_name

http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#_exposing_one_unit_identifiers_from_another

Quote from: Zoran
FPC Reference Guide (topic about class operators) says that "is" operator can be used only on com interfaces!

There were some things that didn't work with CORBA interfaces in the past. With FPC 3.0.0 things just work, and I would focus on that:) The "is" and "as" operators work as expected on both COM and CORBA interfaces now. Since they are consistent with classes and wotk for both interface types now, I would advice just using them.

Note that for COM, you can also use the "Supports" function, but for CORBA the only way to check is to use "is" (as far as I know).

It seems that this part of the FPC documentation was simply not updated.

Quote from: molly
Or to put it into other words: i thought the record/advanced record chapter was a bit small and a bit tucked away.

That is deliberate indeed. I wanted to focus on our main concept, which is "class".

As mentioned, the document is directed at programmers, who usually already know what a "class" is from other programming languages. They know it from Java, C#, C++, Python... everybody has a "class" now:) That's why introducing an "intermediate" concept of a record did not seem necessary in my eyes.

So I treated the record types as "use only if you need to squeeze more performance / particular memory layout". That's also how e.g. structures are treated in C#, and that's also how I find most modern Pascal code looks like. (This is of course biased by my own code.) You use classes, for their flexibility, and think about records only when performance matters, when you really have lists of thousands of somethings.

If this was an introduction to the language for beginners (not people already familiar with the concept of "class"), I would possibly do it differently, more like you describe. Introducing records could be a nice "launching point" for the beginners indeed, to not burden them with the need to construct / destruct explicitly and all the extra class features.

Quote from: BeniBela
It is a trap.

You make all your functions for string, and then you get a pchar and cannot use it with any of them

Better use only pchar for functions

Sorry, but I don't agree with this. The usual type to use for text in Pascal is "string" -- not "PChar". Functions that take and return "string" (usually equal to "AnsiString") are consistently introduced across RTL, FCL, LCL...

I definitely do not want to use PChar in all my functions, it would mean throwing away all the comfort that AnsiString brings.

And I prefer not even talking about PChar in this document -- as it is useful mainly for interfacing with libraries written in other languages. And you can, with some safety cautions (UniqueString etc.) convert AnsiString to PChar. That's one of the advantages of AnsiString. So you're not "locked" even if you used AnsiStrings in all your code.

Quote from: BeniBela
Quote
The as and is operators also work on COM interfaces.

But it is a bad idea to use them

They are too slow

That's a too general statement, in my opinion.

Sure, it's good to avoid run-time checking with "is", and casting with "as". Not only faster, but also the code is safer. But it's not possible to avoid it entirely in real, large applications. That's why every OOP language has a syntax to check the class type, and perform a checked typecast to a descendant. This applies to both classes and interfaces.

You should not reject a useful language construct because "it has a performance cost", in my opinion. A lot of things have a performance cost. But the conclusion to optimize, or even avoid them, should come after you profile the speed of a concrete code, and indeed see that the bottle-neck lies within is / as operators.

michalis

  • Full Member
  • ***
  • Posts: 140
    • Castle Game Engine
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #18 on: June 24, 2016, 03:08:49 am »
Quote from: michalis
Sure, it's good to avoid run-time checking with "is", and casting with "as". Not only faster, but also the code is safer.

Hm, I realized that my document did not mention that you can typecast to interfaces without using "as" operator. I added a section "Typecasting interfaces without "as" operator" that remedies that:)

http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#_typecasting_interfaces_without_as_operator

Zoran

  • Hero Member
  • *****
  • Posts: 1831
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #19 on: June 24, 2016, 02:31:42 pm »
Quote
There is nothing about "is" operator and corba.
That wasn't my point. Point was that it explains that support for corba interfaces is present.

Okay, the thing is that I believe that it actually didn't work in the past, so I was confused when I saw the example and tried to compile it and it just worked.

In case you feel 'betrayed' because the official documentation regarding the is operator does not state specifically that it also works with corba, then please feel free to file a bugreport on mantis ?

Okay, bug report 30308.

Quote from: Zoran
FPC Reference Guide (topic about class operators) says that "is" operator can be used only on com interfaces!

There were some things that didn't work with CORBA interfaces in the past. With FPC 3.0.0 things just work, and I would focus on that:) The "is" and "as" operators work as expected on both COM and CORBA interfaces now. Since they are consistent with classes and wotk for both interface types now, I would advice just using them.

Note that for COM, you can also use the "Supports" function, but for CORBA the only way to check is to use "is" (as far as I know).

It seems that this part of the FPC documentation was simply not updated.

Thanks, yes, it seems that it is a new feature of FPC 3.0. However, FPC New Features wiki page says nothing about it.

Now we have really usable interfaces in FPC.

BeniBela

  • Hero Member
  • *****
  • Posts: 908
    • homepage
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #20 on: June 24, 2016, 03:42:38 pm »

I definitely do not want to use PChar in all my functions, it would mean throwing away all the comfort that AnsiString brings.

I did not want it either, but now I am doing it


And you can, with some safety cautions (UniqueString etc.) convert AnsiString to PChar. That's one of the advantages of AnsiString.

Yes, that is the advantage of having ansistring, you can call the functions who takes pchars.

But not the other way around, thus it is better, if all your functions take pchars.

Or you need to have both variants





You should not reject a useful language construct because "it has a performance cost", in my opinion. A lot of things have a performance cost.

Yes, but it is extremly slow on interfaces.

You can make a function getSelf: TObject; begin result := self; end and then cast the class instead the interface.


Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #21 on: June 24, 2016, 03:50:31 pm »
BeniBela : You do realize that converting constant strings to PChars include slight penalty in FPC 3.x series?

BeniBela

  • Hero Member
  • *****
  • Posts: 908
    • homepage
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #22 on: June 24, 2016, 04:31:17 pm »
BeniBela : You do realize that converting constant strings to PChars include slight penalty in FPC 3.x series?

It is rather slight

What I just realized last week, when you use strings the entire function gets wrapped in a try .. finally block, at least on linux amd64. That is really bad.

The real issue are slices. E.g. you have a 1000 character string s and want to do something with the middle, characters s[400] to s[600]. With a pchar, you can just point in the middle and use a smaller length. With a string, everything in that range needs to be copied.
« Last Edit: June 24, 2016, 04:38:33 pm by BeniBela »

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #23 on: June 24, 2016, 05:30:30 pm »
What I just realized last week, when you use strings the entire function gets wrapped in a try .. finally block, at least on linux amd64. That is really bad.

Reading documentation is an art. You can turn that behavior off... If you really don't want it.
Code: Pascal  [Select][+][-]
  1. {$IMPLICITEXCEPTIONS OFF}
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #24 on: June 24, 2016, 05:33:01 pm »
The real issue are slices. E.g. you have a 1000 character string s and want to do something with the middle, characters s[400] to s[600]. With a pchar, you can just point in the middle and use a smaller length. With a string, everything in that range needs to be copied.

Huh? Why? Of course not! Example please....
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

BeniBela

  • Hero Member
  • *****
  • Posts: 908
    • homepage
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #25 on: June 24, 2016, 05:50:55 pm »
Reading documentation is an art. You can turn that behavior off... If you really don't want it.
Code: Pascal  [Select][+][-]
  1. {$IMPLICITEXCEPTIONS OFF}

That is hard to find

Huh? Why? Of course not! Example please....

https://github.com/benibela/bbutils/blob/master/bbutils.pas#L2566-L2588
« Last Edit: June 24, 2016, 05:52:34 pm by BeniBela »

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #26 on: June 24, 2016, 06:00:27 pm »
That is hard to find
It is in the programmer's manual? So it is easy to find....

Huh? Why? Of course not! Example please....
Quote
https://github.com/benibela/bbutils/blob/master/bbutils.pas#L2566-L2588
I' ll look into it..
[edit] I did. Conclusion: you actually wish C had a decent string type, but you accuse Object Pascal of having too many? 
That code is by the way the biggest piece of bull I have seen in a very long time and is refused for production ;) But KOL is worse.... I grant you that.
« Last Edit: June 24, 2016, 06:35:15 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Awkward

  • Full Member
  • ***
  • Posts: 135
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #27 on: June 24, 2016, 06:19:50 pm »
Thaddy, you know, i used pascal strings a lot in past but when needed to write code compatible with C code, turned to PChar uses. And using of PChar is much simpler for me, i can control all things, i clearly know what happens...
And about documentation. FPC documentation still don't have article what objects can use properties, not classes and records only. What to say about single things which you don't knows where to search?

graemex

  • New Member
  • *
  • Posts: 12
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #28 on: June 24, 2016, 07:47:13 pm »
I found it extremely helpful. I was curious about modern Pascal, and it gave me as much of a feel for it as I am going to get until I start learning it properly - and it will make that a bit easier as well.

One minor correction. "As in all object-oriented languages", should be "most" instead of "all" in section 4.5

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #29 on: June 24, 2016, 10:24:06 pm »
Wow, thank you everyone for the comments! Here's a large answer:)
Well, thank you very much for the write-up. The least a person could do is giving it a read :hint hint:  :D

Quote from: michalis
Quote from: molly
Or to put it into other words: i thought the record/advanced record chapter was a bit small and a bit tucked away.

That is deliberate indeed. I wanted to focus on our main concept, which is "class".
....
Fair enough, especially since you have a particular reader in mind.

 

TinyPortal © 2005-2018