Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Fluent Api in Pascal, nothing earth-shattering ...

Fluent Api in Pascal, nothing earth-shattering ...

Scheduled Pinned Locked Moved The Lounge
csharpdelphiregexjson
28 Posts 11 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 0x01AA

    I assume you know about the 'fluent api pattern', e.g. in c#, something like:

    new MyFluentApi()
    .DoThis(x)
    .DoThat(y)
    .DoAnother(z);

    Now, I was looking for that pattern to do a similar thing in Pascal and recognized, that it is allready something like 'build in' since decades ;)

    WITH myFluentThing DO BEGIN
    DoThis(x);
    DoThat(y);
    DoAnother(z);
    END;

    Mike HankeyM Offline
    Mike HankeyM Offline
    Mike Hankey
    wrote on last edited by
    #2

    Yep Relearning Pascal, Turbo Pascal 3.0 on a RC2014 Z80 hardware running under CP/M 2.2. Been since college days the last time I even saw any Pascal code. Wrote a couple of simple apps, still learning. Learning the editor is twice as hard as the code. We sure take modern editors for granite. :)

    If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

    0 N 2 Replies Last reply
    0
    • Mike HankeyM Mike Hankey

      Yep Relearning Pascal, Turbo Pascal 3.0 on a RC2014 Z80 hardware running under CP/M 2.2. Been since college days the last time I even saw any Pascal code. Wrote a couple of simple apps, still learning. Learning the editor is twice as hard as the code. We sure take modern editors for granite. :)

      If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

      0 Offline
      0 Offline
      0x01AA
      wrote on last edited by
      #3

      To be honest, I don't know Pascal really. I was a Modula2 Guy for decades (Modula also invented by Niklaus Wirth, the Pascal inventor I think). And yes, I hate the 'WITH' in Pascal/Modula (especally when nested), but now I recoginze, it can be usefull too :)

      Mike HankeyM 1 Reply Last reply
      0
      • 0 0x01AA

        To be honest, I don't know Pascal really. I was a Modula2 Guy for decades (Modula also invented by Niklaus Wirth, the Pascal inventor I think). And yes, I hate the 'WITH' in Pascal/Modula (especally when nested), but now I recoginze, it can be usefull too :)

        Mike HankeyM Offline
        Mike HankeyM Offline
        Mike Hankey
        wrote on last edited by
        #4

        Modula is available to run on CP/M, been thinking about kicking the tires. Going on vacation tomorrow for a week but may give it a whirl when I get back.

        If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

        0 1 Reply Last reply
        0
        • Mike HankeyM Mike Hankey

          Modula is available to run on CP/M, been thinking about kicking the tires. Going on vacation tomorrow for a week but may give it a whirl when I get back.

          If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

          0 Offline
          0 Offline
          0x01AA
          wrote on last edited by
          #5

          Enjoy your vacation :)

          R 1 Reply Last reply
          0
          • 0 0x01AA

            Enjoy your vacation :)

            R Offline
            R Offline
            raddevus
            wrote on last edited by
            #6

            I was stunned by "FluentApi" when I heard the term. I first saw it in modern times in C# unit testing and all these new devs were like "fluent api is so special...oh, wow, this new fluent api is just so amazing." I was like, "what is fluent api?" Then I saw it was something we were doing in C++ (specifically MFC/C++) back in 1995 or something. Ok, so you return an object from the method and then you can call another method on the object immediately. These new technologies which are 25 years old are so amazing!! :rolleyes: Bell bottoms are in too and they are also amazing new technology that the world has never seen. :laugh:

            Mike HankeyM 1 Reply Last reply
            0
            • R raddevus

              I was stunned by "FluentApi" when I heard the term. I first saw it in modern times in C# unit testing and all these new devs were like "fluent api is so special...oh, wow, this new fluent api is just so amazing." I was like, "what is fluent api?" Then I saw it was something we were doing in C++ (specifically MFC/C++) back in 1995 or something. Ok, so you return an object from the method and then you can call another method on the object immediately. These new technologies which are 25 years old are so amazing!! :rolleyes: Bell bottoms are in too and they are also amazing new technology that the world has never seen. :laugh:

              Mike HankeyM Offline
              Mike HankeyM Offline
              Mike Hankey
              wrote on last edited by
              #7

              raddevus wrote:

              Bell bottoms are in too

              Ooo I can drag mine back out. :) Eric Clapton - Bell Bottom Blues (Live Video) | Warner Vault - YouTube[^]

              If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

              R 1 Reply Last reply
              0
              • Mike HankeyM Mike Hankey

                raddevus wrote:

                Bell bottoms are in too

                Ooo I can drag mine back out. :) Eric Clapton - Bell Bottom Blues (Live Video) | Warner Vault - YouTube[^]

                If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

                R Offline
                R Offline
                raddevus
                wrote on last edited by
                #8

                I know some Clapton but hadn't heard that song. Great stuff! Sounds great and it's live. Thanks for sharing. :thumbsup:

                1 Reply Last reply
                0
                • Mike HankeyM Mike Hankey

                  Yep Relearning Pascal, Turbo Pascal 3.0 on a RC2014 Z80 hardware running under CP/M 2.2. Been since college days the last time I even saw any Pascal code. Wrote a couple of simple apps, still learning. Learning the editor is twice as hard as the code. We sure take modern editors for granite. :)

                  If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

                  N Offline
                  N Offline
                  Nelek
                  wrote on last edited by
                  #9

                  Mike Hankey wrote:

                  Been since college days the last time I even saw any Pascal code.

                  Was Pascal already there when you went to college? ;) :rolleyes: :laugh: :laugh: :laugh: Sorry... I could not avoid it

                  M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                  Mike HankeyM 1 Reply Last reply
                  0
                  • N Nelek

                    Mike Hankey wrote:

                    Been since college days the last time I even saw any Pascal code.

                    Was Pascal already there when you went to college? ;) :rolleyes: :laugh: :laugh: :laugh: Sorry... I could not avoid it

                    M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                    Mike HankeyM Offline
                    Mike HankeyM Offline
                    Mike Hankey
                    wrote on last edited by
                    #10

                    Towards the end, started out with PL/1. No seriously we would scratch 1s and 0s in the sand and wait for the guy named Alu to come around with his abacus and perform the operation for us. :) I'm so old I was the counter for Noah, but I was fired for letting cockroaches on the ship. In my defense I was busy picking out two of the female species to load.

                    If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

                    T 1 Reply Last reply
                    0
                    • Mike HankeyM Mike Hankey

                      Towards the end, started out with PL/1. No seriously we would scratch 1s and 0s in the sand and wait for the guy named Alu to come around with his abacus and perform the operation for us. :) I'm so old I was the counter for Noah, but I was fired for letting cockroaches on the ship. In my defense I was busy picking out two of the female species to load.

                      If you can't find time to do it right the first time, how are you going to find time to do it again? PartsBin an Electronics Part Organizer - Release Version 1.4.0 (Many new features) JaxCoder.com Latest Article: EventAggregator

                      T Offline
                      T Offline
                      trønderen
                      wrote on last edited by
                      #11

                      Mike Hankey wrote:

                      I'm so old I was the counter for Noah

                      So you have changed your name since? I mean, if you survived the flood, you must have been one of his sons...

                      I was busy picking out two of the female species to load

                      Have you noticed that a large fraction of the pictures made of the animals entering the ship, two by two, where the two lions both have large manes? I wonder how we can have lions today... But then, we are getting close to the yearly rainbow festivals, which is a good occasion to bring up these drawings/paintings.

                      Religious freedom is the freedom to say that two plus two make five.

                      Richard Andrew x64R 1 Reply Last reply
                      0
                      • 0 0x01AA

                        I assume you know about the 'fluent api pattern', e.g. in c#, something like:

                        new MyFluentApi()
                        .DoThis(x)
                        .DoThat(y)
                        .DoAnother(z);

                        Now, I was looking for that pattern to do a similar thing in Pascal and recognized, that it is allready something like 'build in' since decades ;)

                        WITH myFluentThing DO BEGIN
                        DoThis(x);
                        DoThat(y);
                        DoAnother(z);
                        END;

                        T Offline
                        T Offline
                        trønderen
                        wrote on last edited by
                        #12

                        I guess that my background with Pascal and WITH as a student is a major reason why I never use _this._something when programming in OO languages. As classical Pascal did not have classes, you could not put functions manipulating the object (aka. RECORD) into the object definition itself. For functions that would later, in OO languages, become methods of the class, WITH was a nice way to simulate that you were inside the object definition. I never understood why some people insist on using this. everywhere. I read it as a warning: The simple identifier would be ambiguous, and this. is a way to resolve the ambiguity. Sort of like always including the full path from the complete namespace name down to the method name when calling it. I guess my negative reaction to both is that I grew up with WITH as standard way to indicate that 'Now I am manipulating this object'.

                        Religious freedom is the freedom to say that two plus two make five.

                        R 1 Reply Last reply
                        0
                        • T trønderen

                          Mike Hankey wrote:

                          I'm so old I was the counter for Noah

                          So you have changed your name since? I mean, if you survived the flood, you must have been one of his sons...

                          I was busy picking out two of the female species to load

                          Have you noticed that a large fraction of the pictures made of the animals entering the ship, two by two, where the two lions both have large manes? I wonder how we can have lions today... But then, we are getting close to the yearly rainbow festivals, which is a good occasion to bring up these drawings/paintings.

                          Religious freedom is the freedom to say that two plus two make five.

                          Richard Andrew x64R Offline
                          Richard Andrew x64R Offline
                          Richard Andrew x64
                          wrote on last edited by
                          #13

                          Add to that the fact that 2 individuals are not enough to re-establish a population.

                          The difficult we do right away... ...the impossible takes slightly longer.

                          1 Reply Last reply
                          0
                          • 0 0x01AA

                            I assume you know about the 'fluent api pattern', e.g. in c#, something like:

                            new MyFluentApi()
                            .DoThis(x)
                            .DoThat(y)
                            .DoAnother(z);

                            Now, I was looking for that pattern to do a similar thing in Pascal and recognized, that it is allready something like 'build in' since decades ;)

                            WITH myFluentThing DO BEGIN
                            DoThis(x);
                            DoThat(y);
                            DoAnother(z);
                            END;

                            P Offline
                            P Offline
                            Peter Turtle
                            wrote on last edited by
                            #14

                            That Pascal code isn't anything fluent at all, it's just a series of method calls. The direct equivalent of the C# code would be (removing the use of WITH as an anti-pattern):

                            var myFluentThing := TFluentThing.Create
                            .DoThis(x)
                            .DoThat(y)
                            .DoAnother(z);

                            This is only possible, as is true for C#, if the DoThis and DoThat functions return an instance of the TFluentThing class.

                            0 1 Reply Last reply
                            0
                            • P Peter Turtle

                              That Pascal code isn't anything fluent at all, it's just a series of method calls. The direct equivalent of the C# code would be (removing the use of WITH as an anti-pattern):

                              var myFluentThing := TFluentThing.Create
                              .DoThis(x)
                              .DoThat(y)
                              .DoAnother(z);

                              This is only possible, as is true for C#, if the DoThis and DoThat functions return an instance of the TFluentThing class.

                              0 Offline
                              0 Offline
                              0x01AA
                              wrote on last edited by
                              #15

                              But at the end comes nearly to the same

                              P 1 Reply Last reply
                              0
                              • 0 0x01AA

                                But at the end comes nearly to the same

                                P Offline
                                P Offline
                                Peter Turtle
                                wrote on last edited by
                                #16

                                Ermmm, no, because that is like saying 2 + 2 = 3, because 3 is nearly 4

                                1 Reply Last reply
                                0
                                • 0 0x01AA

                                  I assume you know about the 'fluent api pattern', e.g. in c#, something like:

                                  new MyFluentApi()
                                  .DoThis(x)
                                  .DoThat(y)
                                  .DoAnother(z);

                                  Now, I was looking for that pattern to do a similar thing in Pascal and recognized, that it is allready something like 'build in' since decades ;)

                                  WITH myFluentThing DO BEGIN
                                  DoThis(x);
                                  DoThat(y);
                                  DoAnother(z);
                                  END;

                                  A Offline
                                  A Offline
                                  Al Gonzalez
                                  wrote on last edited by
                                  #17

                                  In the C# chain, methods may return the same or different objects at any stage. The WITH block just calls the methods on the original object. ```csharp IThing thing = new ThingBuilder() // returns ThingBuilder instance .AddTitle("I'm a Thing!") // returns same ThingBuilder instance .CreatePreciousThing() // returns PreciousThing instance .SetPreciousLevel(100); // returns same PreciousThing instance // thing is now a PreciousThing instance ```

                                  0 1 Reply Last reply
                                  0
                                  • A Al Gonzalez

                                    In the C# chain, methods may return the same or different objects at any stage. The WITH block just calls the methods on the original object. ```csharp IThing thing = new ThingBuilder() // returns ThingBuilder instance .AddTitle("I'm a Thing!") // returns same ThingBuilder instance .CreatePreciousThing() // returns PreciousThing instance .SetPreciousLevel(100); // returns same PreciousThing instance // thing is now a PreciousThing instance ```

                                    0 Offline
                                    0 Offline
                                    0x01AA
                                    wrote on last edited by
                                    #18

                                    Finally it comes up to the same from a user point ... ;)

                                    1 Reply Last reply
                                    0
                                    • 0 0x01AA

                                      I assume you know about the 'fluent api pattern', e.g. in c#, something like:

                                      new MyFluentApi()
                                      .DoThis(x)
                                      .DoThat(y)
                                      .DoAnother(z);

                                      Now, I was looking for that pattern to do a similar thing in Pascal and recognized, that it is allready something like 'build in' since decades ;)

                                      WITH myFluentThing DO BEGIN
                                      DoThis(x);
                                      DoThat(y);
                                      DoAnother(z);
                                      END;

                                      R Offline
                                      R Offline
                                      Ralf Quint
                                      wrote on last edited by
                                      #19

                                      Well, no. I must admit that I do not use C#, only know the very basics of it. "new" in C# is creating an instance of an previously defined object (MyFluentAPI) and executes those 3 methods. As for "Pascal", you did not mentioned which kind of Pascal you are using. In "standard" Pascal (including but not limited to ISO7185), the "with" statement is used to set the scope for the statements within the begin...end block, and is used in connection with "records" ("struct" in C) to save on some typing, as you would not have to precede each record element with its associated record name. But in this case, those elements within that record are purely data! It is purely what sometimes these days is referred to as "syntactic sugar". But the major difference is that the record (and in extension, this applies mostly to objects/classes in Object Pascal (Delphi/FreePascal) as well) needs to be existing (already instantiated) when you are referring to it using the "with" statement (block). If you are using the "with" statement in either standard/procedural Pascal or Object Pascal, you need to do this carefully, as, while it saves some typing and the source code lines shorter, it can lead to some nasty side effects/bugs (or features ;P ) if you have elements, like data or methods/procedure/functions with the same names in different records/methods/etc...

                                      1 Reply Last reply
                                      0
                                      • T trønderen

                                        I guess that my background with Pascal and WITH as a student is a major reason why I never use _this._something when programming in OO languages. As classical Pascal did not have classes, you could not put functions manipulating the object (aka. RECORD) into the object definition itself. For functions that would later, in OO languages, become methods of the class, WITH was a nice way to simulate that you were inside the object definition. I never understood why some people insist on using this. everywhere. I read it as a warning: The simple identifier would be ambiguous, and this. is a way to resolve the ambiguity. Sort of like always including the full path from the complete namespace name down to the method name when calling it. I guess my negative reaction to both is that I grew up with WITH as standard way to indicate that 'Now I am manipulating this object'.

                                        Religious freedom is the freedom to say that two plus two make five.

                                        R Offline
                                        R Offline
                                        Ralf Quint
                                        wrote on last edited by
                                        #20

                                        Well, as I mentioned in another reply in this thread, the problem with using "with" is that is you have multiple records/objects with the same names for data (of the same TYPE) or methods/procedures/functions, you can not be sure which one might be called/referred to. So the use of "with" blocks should be deliberate and limited in scope, in order to prevent any ill effects, even if it is that your source won't compile because of the stringent type checking of Pascal. At the very least it requires you to find out which element of which record/object is actually referred to, which then needs to be fixed by adding at least one non-ambiguous prefix to the code line(s). For example, if you have two records (A and B), which both have a element called C, a "with" block like with A, B do begin C := 1; C := 0; end; Not only looks wrong, but might actually yield unexpected results, if what you actually intended to write is A.C := 1; B.C := 0; The same goes for calling methods of the same name in different objects, so something like with A, B do begin C ("foo"); C ("bar"); end; when actually is intended to be A.C ("foo"); B.C ("bar"); With different types of parameters of the methods A.C() and B.C(), this won't compile, but if they happen to have the same parameter (or are parameter-less!), again, the outcome might now be what you have intended...

                                        T 1 Reply Last reply
                                        0
                                        • R Ralf Quint

                                          Well, as I mentioned in another reply in this thread, the problem with using "with" is that is you have multiple records/objects with the same names for data (of the same TYPE) or methods/procedures/functions, you can not be sure which one might be called/referred to. So the use of "with" blocks should be deliberate and limited in scope, in order to prevent any ill effects, even if it is that your source won't compile because of the stringent type checking of Pascal. At the very least it requires you to find out which element of which record/object is actually referred to, which then needs to be fixed by adding at least one non-ambiguous prefix to the code line(s). For example, if you have two records (A and B), which both have a element called C, a "with" block like with A, B do begin C := 1; C := 0; end; Not only looks wrong, but might actually yield unexpected results, if what you actually intended to write is A.C := 1; B.C := 0; The same goes for calling methods of the same name in different objects, so something like with A, B do begin C ("foo"); C ("bar"); end; when actually is intended to be A.C ("foo"); B.C ("bar"); With different types of parameters of the methods A.C() and B.C(), this won't compile, but if they happen to have the same parameter (or are parameter-less!), again, the outcome might now be what you have intended...

                                          T Offline
                                          T Offline
                                          trønderen
                                          wrote on last edited by
                                          #21

                                          Ralf Quint wrote:

                                          you have multiple records/objects with the same names for data (of the same TYPE) or methods/procedures/functions, you can not be sure which one might be called/referred to.

                                          You have exactly the same problem with instances of a class definition (i.e. objects): When you read the code in the class methods, you cannot know which instance, which object, is being manipulated. A Pascal "with" isn't very good at hiding it: "with SomeObject do ..." sort of identifies SomeObject as the one being manipulated, doesn't it? I cannot remember ever listing multiple variables the way you do - it seems like you are doing it specifically to create a problem - one that isn't there, if you follow the Pascal line of thought. Let me construct another example:

                                          procedure P;
                                          var C: integer;

                                          procecdure Q;
                                          var C: integer;
                                          begin
                                          C:= 1;
                                          end;

                                          begin
                                          { some other code }
                                          end;

                                          The "C:= 1;" - which C does it refer to? The one declared in Q or the one declared in P? No Pascal programmer would be in doubt. Let's now write

                                          with A do
                                          with B do
                                          begin
                                          C:= 1;
                                          end;

                                          Is it difficult to see whether the "C:= 1;" refers to B.C or to A.C? No Pascal programmer would find it difficult!

                                          with A, B do

                                          is just a short form of

                                          with A do
                                          with B do

                                          As long as you are within the 'with B do' (in either the short or the long form), the B scope is the innermost. The search for the definition of a symbol always starts in the scope where there reference is made, and the first definition in the outwards search applies. This is always true; it is not particular to nested "with" statements. If you, in the first example, need to reference P's C from the body of Q, you must rename Q's C. (Pascal has no syntax for explicitly referencing a local variable at an outer procedure level.) If you need to reference A's C from within a 'with B do', then you do have a syntax for it: A.C references A's C (surprise!). "with B do C:= 1" references B's C (surprise!). This is not any problem, no sort of ambiguity, but plain scoping rules. An inner scope redefines an identically named symbol in an outer scope. If you grew up in a K&R C environment, maybe you don't have statically nested scopes under your skin. A Pascal programmer has.

                                          Religious freedom is the freedom to say that two plus two make five.

                                          R 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups