Handlers - Optional parameters syntax

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Handlers - Optional parameters syntax

Post by Zryip TheSlug » Thu Apr 02, 2015 10:18 pm

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?
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

PBH
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 129
Joined: Sun Feb 20, 2011 4:26 pm
Location: Vancouver Island, BC, Canada. ex.UK
Contact:

Re: Handlers - Optional parameters syntax

Post by PBH » Fri Apr 03, 2015 8:13 pm

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

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1212
Joined: Thu Apr 11, 2013 11:27 am

Re: Handlers - Optional parameters syntax

Post by LCMark » Sat Apr 04, 2015 2:54 pm

@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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9857
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Handlers - Optional parameters syntax

Post by FourthWorld » Sat Apr 04, 2015 3:50 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1212
Joined: Thu Apr 11, 2013 11:27 am

Re: Handlers - Optional parameters syntax

Post by LCMark » Sat Apr 04, 2015 4:58 pm

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

Post Reply

Return to “LiveCode Builder”