Announcement

Collapse
No announcement yet.

? Inconsistent treatment of optional parameters

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ? Inconsistent treatment of optional parameters

    In the first wrapper/Sub pair PBMAIN
    makes a "call " to the wrapper with and without a parameter and
    compiles fine..

    In the 2nd wrapper/Sub pair PBMAIN fails on the first "call " to the
    wrapper.. the one without the 2nd parameter..

    Am I missing something?

    Thanks

    Code:
    #COMPILE EXE
    #DIM ALL
    
    GLOBAL B AS LONG
    
    MACRO Wrapper1(AAA)
          CALL Func1(AAA)
    END MACRO
    
    SUB Funct1(OPT B AS LONG)
        LOCAL Passed AS LONG
        Passed=IIF(VARPTR(B)=0,0,-1)
        IF Passed<>0 THEN
            ? "B="+STR$(B)
        ELSE
            ? "Wrapper1 2nd parameter not passed"
        END IF
    END SUB
    
    MACRO Wrapper2(AAA,BBB)
          CALL Func1(AAA,BBB)
    END MACRO
    
    SUB Funct2(BYVAL A AS LONG, OPT B AS LONG)
        LOCAL Passed AS LONG
        Passed =IIF(VARPTR(B)=0,0,-1)
        
        IF Passed<>0 THEN
            ? "B="+STR$(B)
        ELSE
            ? "Wrapper1 2nd parameter not passed"
        END IF
    
    END SUB
    
    FUNCTION PBMAIN () AS LONG
    
           LOCAL I, J AS LONG
           B=6
           I=1
           J=2
           K=3
           
           Wrapper1()     'compiles - no optional parameter passed
           Wrapper1(I)    'compiles parameter passed
           
           '---- comment out the next 2 lines and it compiles and works
           Wrapper2(I)    'Mismatch with prior definition
           Wrapper2(I,J)
    
    END FUNCTION
    Nathan Maddox

  • #2
    >Wrapper1() 'compiles - no optional parameter passed

    MACROs do not have "optional parameters," they are strictly "pre-compilation" text replacement tools.

    Too bad there's no way to "see" the macro expansion; but frankly I think this should have errored out at compile time on something like "Incorrect number of tokens in MACRO replacement"
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      The first problem is that your Wrapper2 function is defined as having two required parameters and the first time you call it you are only passing one parameter. By the way, I have no idea why you didn't get an error when calling Wrapper1 with no parameter as it is defined as having one required parameter.

      You might run into another problem as well seeing as your Wrapper2 function is calling Func1 with two parameters but Func1 is defined with only one optional parameter. I think you meant for Wrapper2 to call Func2.

      I'm not sure why you are doing these wrappers instead of calling the functions directly but if you have a need for the wrappers, I'd think you would want the parameter list to be the same as the functions you are calling. This means making optional parameters from the functions be optional parameters in your wrappers. If you need to pass more parameters to the wrapper, then add them to the beginning of the parameter list (ie. Wrapper1(ExtraParam AS LONG, BYVAL A AS LONG, OPT B AS LONG)) so that you can preserve any optional parameters that might be required. Remember, all parameters after the first OPTIONAL keyword are optional regardless of whether they have the keyword added or not.

      By the way, by using those wrappers, you will never see the "parameter not passed" message because you are always providing the optional parameter.
      Jeff Blakeney

      Comment


      • #4
        Right, I had missed the macro bit. So the MACROs would expand to:

        Wrapper1() = Func1() which is a valid statement.

        Wrapper1(I) = Func1(I) which is a valid statement.

        Wrapper2(I) = Func1(I,) which tells the compiler there is two parameters but only one has been defined for Func1. If you change the Wrapper2 to call Func2 instead it still isn't a valid statement because you don't provide a name for the second parameter. You would need to drop the comma somehow in the wrapper macro to get it to compile.

        Wrapper2(I,J) = Func1(I,J) which is also wrong because it is calling Func1. If it were calling Func2 it would be fine.
        Jeff Blakeney

        Comment


        • #5
          ...

          When trying to reproduce my error I introduced an error in the example.

          The 2nd wrapper should have called Func2()

          Code:
          #COMPILE EXE
          #DIM ALL
          
          GLOBAL B AS LONG
          
          MACRO Wrapper1(AAA)
                CALL LongandNastyNameofFunction1(AAA)
          END MACRO
          
          SUB LongandNastyNameofFunction1(OPT B AS LONG)
              LOCAL Passed AS LONG
              Passed=IIF(VARPTR(B)=0,0,-1)
              IF Passed<>0 THEN
                  ? "B="+STR$(B)
              ELSE
                  ? "Wrapper1 2nd parameter not passed"
              END IF
          END SUB
          
          MACRO Wrapper2(AAA,BBB)
                CALL LongandNastyNameofFunction2(AAA,BBB)
          END MACRO
          
          SUB LongandNastyNameofFunction2(BYVAL A AS LONG, OPT B AS LONG)
              LOCAL Passed AS LONG
              Passed =IIF(VARPTR(B)=0,0,-1)
          
              IF Passed<>0 THEN
                  ? "B="+STR$(B)
              ELSE
                  ? "Wrapper1 2nd parameter not passed"
              END IF
          
          END SUB
          
          FUNCTION PBMAIN () AS LONG
          
                 LOCAL I, J, K AS LONG
                 B=6
                 I=1
                 J=2
                 K=3
          
                 Wrapper1()     'compiles - no optional parameter passed
                 Wrapper1(I)    'compiles
          
                 Wrapper2(I)    'Mismatch with prior definition
                 Wrapper2(I,J)
          
          END FUNCTION
          Nathan Maddox

          Comment


          • #6
            What version are you using again? I see no way how it would even compile (much less run) because you call your macro with
            Code:
                    Wrapper2(I)    'Mismatch with prior definition
            but the Macro takes 2 parameters (even though the end function uses one parameter optionally)
            Code:
            MACRO Wrapper2(AAA,BBB)
                  CALL LongandNastyNameofFunction2(AAA,BBB)
            END MACRO
            So in compile-time your macro replacement goes from
            Code:
                    Wrapper2(I)    'Mismatch with prior definition
            into
            Code:
            CALL LongandNastyNameofFunction2(AAA,BBB)    'Mismatch with prior definition
            Which should and DOES pop up the error that you did not pass 2 parameters so it did not expand correctly

            On a side note MCM did give me another "Mad-Scientist" idea for use with my ErrorHandler when he made the comment of
            Too bad there's no way to "see" the macro expansion; but frankly I think this should have errored out at compile time on something like "Incorrect number of tokens in MACRO replacement"
            Maybe....jussssst mayyyyyybeeeee there is a way
            Engineer's Motto: If it aint broke take it apart and fix it

            "If at 1st you don't succeed... call it version 1.0"

            "Half of Programming is coding"....."The other 90% is DEBUGGING"

            "Document my code????" .... "WHYYY??? do you think they call it CODE? "

            Comment

            Working...
            X