Page 1 of 1

Handlers - Optional parameters syntax

Posted: Thu Apr 02, 2015 10:18 pm
by Zryip TheSlug
How to use optional parameters in a handler / function ?

For example I want an addRectangle handler accepting 2 parameters and 2 optional parameters:


I tried:

Code: Select all

private handler addRectangle(in pRectangle as Rectangle, in pColor as Color, in optional pBorderWidth as Integer = 0, in optional pBorderColor as Color = [1, 1, 1])

end handler
or

Code: Select all

private handler addRectangle(in pRectangle as Rectangle, in pColor as Color, in optional pBorderWidth as Integer, in optional pBorderColor as Color)
  if (pBorderWidth is undefined) then
      put 0 into pBorderWidth
  end if
  doSomething
end handler
And I would like to call this handler by using:
- addRectangle(myRectangle, myColor)
- addRectangle(myRectangle, myColor, myBorderWidth)
- addRectangle(myRectangle, myColor, myBorderWidth, myBorderColor)

None of the syntax I tried are working so far. Is this possible or what is the right syntax?

Re: Handlers - Optional parameters syntax

Posted: Fri Apr 03, 2015 8:13 pm
by PBH
AFAIK all parameters passed to a handler can be optional, but what is important, is the order that they are passed. If your handler requires a particular parameter then you just need to handle the case where it may be empty.

Here's a couple of button scripts to demonstrate:

Code: Select all

# Place in a Button Script - creates a small rectangle when clicked
# Holding the shift Key down when clicking the button adds a border
# Try Shift+Ctrl or Shift+Alt or Shift+Cmd to see the different parameters being passed

on mouseUp
   if exists(graphic "myRectangle") then delete  graphic "myRectangle" # Clean up
   # Set up the parameters
   put "100,100,200,200" into tRect
   put "255,150,150" into tColor
   if the shiftKey is down then # Adds border parameters
      put 10 into tBorder
      put "255,0,0" into tBorderCol
   end if
   
   switch
      # Note the double comma - Parameters can be missing, 
      # but they still need to be passed in the correct order
      case the shiftKey is down and the ctrlKey is down
         addRectangle tRect, , tBorder, tBorderCol
         break
      case the shiftKey is down and the altKey is down
         addRectangle tRect, tColor, , tBorderCol
         break
      case the shiftKey is down and the cmdKey is down
         # Parameters missing from the end don't require extra commas
         addRectangle tRect, tColor, tBorder
         break
      case the shiftKey is down
         addRectangle tRect, tColor, tBorder, tBorderCol
         break
      default
         addRectangle tRect, tColor
   end switch
end mouseUp

private command addRectangle pRectangle, pColor, pBorderWidth, pBorderColor
   create invisible graphic "myRectangle"
   set the opaque of graphic "myRectangle" to true
   set the rect of grc "myRectangle" to pRectangle
   set the backColor of grc "myRectangle" to pColor
   if pBorderColor is not empty then put 5 into tLineSize # We need a lineSize value to see the coloured border
   set the lineSize of grc "myRectangle" to max(tLineSize,pBorderWidth)
   set the foreColor of grc "myRectangle" to pBorderColor
   set the visible graphic "myRectangle" to true
end addRectangle

Code: Select all

# Place in a Button Script, use shift, ctrl, alt or cmd keys to try the options:

on mouseUp
   put 10 into tA
   put 20 into tB
   put 30 into tC
   put 40 into tD
   
   switch
      # Note the double comma - Parameters can be missing, 
      # but they still need to be passed in the correct order
      case the ctrlKey is down
         answer addNumbers(tA, , tC, tD)
         break
      case the altKey is down
         answer addNumbers(tA, tB, , tD)
         break
      case the cmdKey is down
         # Parameters missing from the end of the list don't require extra commas
         answer addNumbers(tA, tB, tC)
         break
      case the shiftKey is down
         answer addNumbers(tA, tB)
         break
      default
         answer addNumbers(tA, tB, tC, tD)
   end switch
   
end mouseUp

function addNumbers pA, pB, pC, pD
   put pA + pB + pC + pD into tAnswer
   return tAnswer
end addNumbers
Paul

Re: Handlers - Optional parameters syntax

Posted: Sat Apr 04, 2015 2:54 pm
by LCMark
@Zyrip: Optional only applies to the type at the moment, not to whether you have to specify the parameter or not. See this thread - http://forums.livecode.com/viewtopic.php?f=93&t=23678

Re: Handlers - Optional parameters syntax

Posted: Sat Apr 04, 2015 3:50 pm
by FourthWorld
I have a vague recollection that LCB was going to provide support for arguments as name-value pairs, e.g.:

SomeCommand arg1="value1" arg2="value2" arg3="value3"

Was I dreaming? NVPs are more verbose much much easier to remember than fix-order comma-separated args.

Re: Handlers - Optional parameters syntax

Posted: Sat Apr 04, 2015 4:58 pm
by LCMark
@FourthWorld: I think you might have been dreaming ;) We've not considered that yet, although I won't rule anything out at this stage. Remember that with eventual addition of dynamic syntax, you get full flexibility in how to specify handlers will be called - likely eliminating the need to complicate the general call syntax.