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 40 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.
  • 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
    • 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
      Ravi Bhavnani
      wrote on last edited by
      #22

      Not quite. :) While the code sample you posted may look fluent, in reality the functions DoThis(), DoThat() and DoAnother() all operate on a myFluentThing.  A truly fluent piece of code contains chained methods each of which operate on the context of the previous returned type. So unless I misunderstood what you wrote, I don't think you'd be able to simulate this in Pascal:

      int i = new FooBuilder()
      .Build()
      .DoSomethingToFoo()
      .DoSomethingElseToFoo()
      .GetAnEnumerableIntPropertyOfFoo()
      .Sum();

      /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      0 1 Reply Last reply
      0
      • T trønderen

        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 Offline
        R Offline
        Ralf Quint
        wrote on last edited by
        #23

        Very interesting that you are trying to explain how Pascal works to someone who is programming in various versions of Pascal, as the main programming language, for 48 years now. Yes, the "with A, B" is (for decades, nothing new) just a shorter form of writing a nested "with A..with B". That alone is not a problem, never is, never was. The problem that I was pointing out, and you apparently didn't understand, is that within the nested WITH statements (regardless of how you write it), it IS POSSIBLE to create an unintentional ambiguity, which is NOT guaranteed to be always have the same precedence. If you think that, it clearly shows that you have not worked with a lot of different Pascal implementations. And the application of scopes within nested procedures/functions, that is completely different issue. THAT is clearly defined. But that would be also not related to the initial post of this thread.

        T 1 Reply Last reply
        0
        • R Ralf Quint

          Very interesting that you are trying to explain how Pascal works to someone who is programming in various versions of Pascal, as the main programming language, for 48 years now. Yes, the "with A, B" is (for decades, nothing new) just a shorter form of writing a nested "with A..with B". That alone is not a problem, never is, never was. The problem that I was pointing out, and you apparently didn't understand, is that within the nested WITH statements (regardless of how you write it), it IS POSSIBLE to create an unintentional ambiguity, which is NOT guaranteed to be always have the same precedence. If you think that, it clearly shows that you have not worked with a lot of different Pascal implementations. And the application of scopes within nested procedures/functions, that is completely different issue. THAT is clearly defined. But that would be also not related to the initial post of this thread.

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

          Ralf Quint wrote:

          Very interesting that you are trying to explain how Pascal works to someone who is programming in various versions of Pascal, as the main programming language, for 48 years now.

          I did not intend my post an answer to you personally, but as a post available to the general public. The majority of CP readers are unfamiliar with the semantics of Pascal "with" - a great deal haven't even worked much with statically nested scopes in any sense. Your profile doesn't tell that you have been programming Pascal for 48 years, so I couldn't possibly have adapted my post to that!

          within the nested WITH statements (regardless of how you write it), it IS POSSIBLE to create an unintentional ambiguity

          Are you saying that it not only is POSSIBLE, but you did it? You claim that there is 'an unintentional ambiguity', but I see none.

          which is NOT guaranteed to be always have the same precedence

          What are you saying here? That you have been working with Pascal compilers that did not create an inner scope for B with the short form ("with A, B do")? Did that compiler claim to be conformant to the language standard? If there were an ambiguity, then there would be two different ways to interpret the semantics of the statement (here: "C:= 1;"). Some Pascal compilers would select one semantic, others would select the other. If you claim that you have seen both, in different Pascal implementations, I would sure like to hear which they are, and which one provides which semantics. Also, I'd like to know if they both claim standard conformance. I am quite sure that they both could!

          it clearly shows that you have not worked with a lot of different Pascal implementations

          Only about half a dozen, on VAX, CDC, ND, IBM, MC68 and x86 machines. It is long ago, so they were all Pascal compilers, and

          calling methods of the same name in different objects, so something like

          with A, B do
          begin
          C ("foo");
          C ("bar");
          end;

          was not a valid syntax - plain Pascal doesn't have objects, only records, and you cannot "call methods in different objects". Original Pascal was a well defined, consistent language, defined by people who knew how to define unambiguous grammars. A couple years later, a more successful competitor, as it turned out, was created by people who certainly did not master formal language gra

          R 1 Reply Last reply
          0
          • T trønderen

            Ralf Quint wrote:

            Very interesting that you are trying to explain how Pascal works to someone who is programming in various versions of Pascal, as the main programming language, for 48 years now.

            I did not intend my post an answer to you personally, but as a post available to the general public. The majority of CP readers are unfamiliar with the semantics of Pascal "with" - a great deal haven't even worked much with statically nested scopes in any sense. Your profile doesn't tell that you have been programming Pascal for 48 years, so I couldn't possibly have adapted my post to that!

            within the nested WITH statements (regardless of how you write it), it IS POSSIBLE to create an unintentional ambiguity

            Are you saying that it not only is POSSIBLE, but you did it? You claim that there is 'an unintentional ambiguity', but I see none.

            which is NOT guaranteed to be always have the same precedence

            What are you saying here? That you have been working with Pascal compilers that did not create an inner scope for B with the short form ("with A, B do")? Did that compiler claim to be conformant to the language standard? If there were an ambiguity, then there would be two different ways to interpret the semantics of the statement (here: "C:= 1;"). Some Pascal compilers would select one semantic, others would select the other. If you claim that you have seen both, in different Pascal implementations, I would sure like to hear which they are, and which one provides which semantics. Also, I'd like to know if they both claim standard conformance. I am quite sure that they both could!

            it clearly shows that you have not worked with a lot of different Pascal implementations

            Only about half a dozen, on VAX, CDC, ND, IBM, MC68 and x86 machines. It is long ago, so they were all Pascal compilers, and

            calling methods of the same name in different objects, so something like

            with A, B do
            begin
            C ("foo");
            C ("bar");
            end;

            was not a valid syntax - plain Pascal doesn't have objects, only records, and you cannot "call methods in different objects". Original Pascal was a well defined, consistent language, defined by people who knew how to define unambiguous grammars. A couple years later, a more successful competitor, as it turned out, was created by people who certainly did not master formal language gra

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

            You just do not want to understand, so I am just leaving it at that. EOT...

            T 1 Reply Last reply
            0
            • R Ralf Quint

              You just do not want to understand, so I am just leaving it at that. EOT...

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

              I don't see that there is much more to understand. You claim there is an ambiguity that isn't there. You insist that it nevertheless is there, but refuse to explain when, where and why it is there. Your 48 years of Pascal experience is not explanation for why it should be ambiguous. Nor is the number of Pascal compilers you have been using. Your unambiguous code snippet is not made ambiguous by your saying that it is. The only thing that needs explanation is why you claim some sort of ambiguity in these samples. You are the one making the claim, you must provide the arguments for it. You refuse to. Fair enough. Then you are not providing any support for the ambiguity you claim. You are quite right: I do not understand the ambiguity that isn't there :-)

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

              1 Reply Last reply
              0
              • R Ravi Bhavnani

                Not quite. :) While the code sample you posted may look fluent, in reality the functions DoThis(), DoThat() and DoAnother() all operate on a myFluentThing.  A truly fluent piece of code contains chained methods each of which operate on the context of the previous returned type. So unless I misunderstood what you wrote, I don't think you'd be able to simulate this in Pascal:

                int i = new FooBuilder()
                .Build()
                .DoSomethingToFoo()
                .DoSomethingElseToFoo()
                .GetAnEnumerableIntPropertyOfFoo()
                .Sum();

                /ravi

                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

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

                Of course you are right, nitpicker :laugh:

                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;

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #28

                  So stringing things together is great? Then you should try Lisp.

                  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