Recent

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

michalis

  • Full Member
  • ***
  • Posts: 105
    • Homepage
Quick Modern Object Pascal Introduction, for Programmers
« on: June 22, 2016, 07:00:30 pm »
Over the last few days, I wrote a document describing many features of the modern Object Pascal language. It is available on:

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

http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.pdf

The document covers the easy language concepts, and then jumps quickly into more "advanced" stuff, like class references, generics, class helpers and so on. I hope that this is informative:)

This is directed at programmers (who know a bit of some programming language, though not necessarily Pascal). Which is really an excuse to not explain in detail some basic stuff ("what is a variable", "what is a class"). I tried to explain more the "advanced" stuff, and illustrate everything with examples.

The source code is in AsciiDoc and available on GitHub https://github.com/michaliskambi/modern-pascal-introduction . Please share and redistribute it freely:) All comments are welcome:)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8597
  • FPC developer.
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #1 on: June 22, 2016, 08:00:00 pm »
I think it is a bit light on the module/unit system.

Thaddy

  • Hero Member
  • *****
  • Posts: 10288
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #2 on: June 22, 2016, 08:39:03 pm »
I think it is a bit light on the module/unit system.

And on string types (compared to C) and why they are faster to evaluate and have some distinct advantages over just only zero terminated strings.. I noticed that when answering a different question and I am now reading it more thoroughly.
I am more like donkey than shrek

michalis

  • Full Member
  • ***
  • Posts: 105
    • Homepage
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #3 on: June 22, 2016, 10:40:56 pm »
Quote
I think it is a bit light on the module/unit system.

What would you like to add specifically?:)

Quote
And on string types (compared to C) and why they are faster to evaluate and have some distinct advantages over just only zero terminated strings.. I noticed that when answering a different question and I am now reading it more thoroughly.

This I would like to keep simple. For programmers coming from Java or C# or modern C++ (with std::string) or PHP or Python or..., it's not news that using strings is easy, and they can be just concatenated with an operator, and you don't need to worry about memory management. I think that only for programmers that never used anything else than bare C, such strings are "news":)

So I would like to keep string "description" to a short comment "strings are automatically managed". Numerous examples show that we simply add strings, and pass them around to/from functions. I feel that the description of inner-workings (reference counting, copy on write etc.), are better left for a proper language manual like http://freepascal.org/docs-html/current/ref/refsu10.html . Same for alternatives (ShortString, WideString) -- for simple string manipulation, using {$H+} and "string" should suffice, IMHO.

In unrelated news, stuff that I'd still like to add is

- (maybe) Basic RTTI example.
- Example of streaming of the published properties - simple read/write example of a custom class.
« Last Edit: June 23, 2016, 04:47:27 am by michalis »

shobits1

  • Sr. Member
  • ****
  • Posts: 278
  • .
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #4 on: June 23, 2016, 04:45:14 am »
good read  :)

shobits1

  • Sr. Member
  • ****
  • Posts: 278
  • .
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #5 on: June 23, 2016, 05:12:09 am »
found small typo at
Quote
7.1. Local (nested) routines
....
....
Another version, where we let the local routine Square to access I directly:]
Code: Pascal  [Select][+][-]
  1. ...
  2.   for I := 0 to N do
  3.     Result := Result + Square(I);
  4.  
should be
Code: Pascal  [Select][+][-]
  1. ...
  2.   for I := 0 to N do
  3.     Result := Result + Square;
  4.  

michalis

  • Full Member
  • ***
  • Posts: 105
    • Homepage
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #6 on: June 23, 2016, 05:29:14 am »
found small typo at
Quote
7.1. Local (nested) routines
....
....
Another version, where we let the local routine Square to access I directly:]
Code: Pascal  [Select][+][-]
  1. ...
  2.   for I := 0 to N do
  3.     Result := Result + Square(I);
  4.  
should be
Code: Pascal  [Select][+][-]
  1. ...
  2.   for I := 0 to N do
  3.     Result := Result + Square;
  4.  

Ah yes! Thank you, fixed!

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8597
  • FPC developer.
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #7 on: June 23, 2016, 11:58:05 am »
Quote
I think it is a bit light on the module/unit system.

What would you like to add specifically?:)


I reread it again (initially a bit in a hurry), and I see there is some coverage about circular inclusion, which would be the number one priority.  I think that should be extracted to a separate paragraph, and add something about mitigation (e.g. extract some of the needed parts to a separate unit, use a base class in case of classes etc)

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).

Qualified identifier usage, and, related, type aliases.  (type xx = unity.xx)

The latter is also a mitigation strategy for the case where you need to move types because of circular dependencies, or simply while they belong to unit X, some other, deeper units exist too.



Zoran

  • Hero Member
  • *****
  • Posts: 1573
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #8 on: June 23, 2016, 02:10:30 pm »
I like the section about (good and ugly ;) ) interfaces.
But I am confused now:

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

However, In your example for good (corba) interfaces, you use "is" operator to check if class implements an interface and it works (I tried the example).

Is the documentation wrong? I remember I had problems in past when tried using "is" operator with corba interfaces, but, as I said, now I can confirm that your example works...

 %)

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #9 on: June 23, 2016, 02:15:35 pm »
says that "is" operator can be used only on com interfaces!

Is the documentation wrong?
huh ?

No, you are simply not reading _all_ :-)

Quote
An expression containing the is operator results in a boolean type. The is operator can only be used with a class reference or a class instance.
and
Quote
The as and is operators also work on COM interfaces.
:D

Zoran

  • Hero Member
  • *****
  • Posts: 1573
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #10 on: June 23, 2016, 02:21:39 pm »
says that "is" operator can be used only on com interfaces!

Is the documentation wrong?
huh ?

No, you are simply not reading _all_ :-)

Quote
An expression containing the is operator results in a boolean type. The is operator can only be used with a class reference or a class instance.
and
Quote
The as and is operators also work on COM interfaces.
:D

Hello, Molly,
But in the example the is operator is used for CORBA interface. Isn't that in contrast to the documentation?

Also note the final sentece in this Reference Guide topic: "Although the interfaces must be COM interfaces, ..."

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #11 on: June 23, 2016, 02:32:28 pm »
Hello, Molly,
But in the example the is operator is used for CORBA interface. Isn't that in contrast to the documentation?
Ah ok. Your question was specifically with regards to corba interfaces. I interpreted that wrongly.

No i believe it is not in contrary to the documentation only because corba interfaces are not mentioned specifically.

Quote
Also note the final sentece in this Reference Guide topic: "Although the interfaces must be COM interfaces, ..."
Selective reading always deliver nice results :-)

Please also read directly after that:
Quote
...the typecast back to a class will only work if the interface comes from an Object Pascal class.
Which explains, i guess, why it works for corba interfaces as well ?

See also this
« Last Edit: June 23, 2016, 02:34:19 pm by molly »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #12 on: June 23, 2016, 02:56:53 pm »
@michalis
That is a very nice read, my compliments.

You already expressed that you did not wanted to start off with variables, types and the like but from a pure personal perspective i always like the: types -> structured types -> advanced records -> classes, as a nice progressive way to use as guideline, because of the overlap.

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

circular

  • Hero Member
  • *****
  • Posts: 3378
    • Personal webpage
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #13 on: June 23, 2016, 04:11:59 pm »
Quite a nice format to read  :)
Conscience is the debugger of the mind

Zoran

  • Hero Member
  • *****
  • Posts: 1573
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Quick Modern Object Pascal Introduction, for Programmers
« Reply #14 on: June 23, 2016, 04:48:55 pm »
Quote
Also note the final sentece in this Reference Guide topic: "Although the interfaces must be COM interfaces, ..."
Selective reading always deliver nice results :-)

Sorry, I don't think that I read it selectively, I read it thoroughly several times.

Please also read directly after that:
Quote
...the typecast back to a class will only work if the interface comes from an Object Pascal class.
Which explains, i guess, why it works for corba interfaces as well ?

The whole sentence is:
Code: Pascal  [Select][+][-]
  1. Although the interfaces must be COM interfaces, the typecast back to a class will only work if the interface comes from an Object Pascal class. It will not work on interfaces obtained from the system by COM.
But, please...
You really think that it says to the reader that CORBA interfaces can use "is" operator, as well as COM interfaces?

See also this
There is nothing about "is" operator and corba.

I am still confused...  %)

 

TinyPortal © 2005-2018