Curl/Example 4

From Wikibooks, open books for an open world
Jump to navigation Jump to search
{curl 5.0, 6.0 applet}
|| Simple OO example - I know simple OO is never sufficient
|| Lets sub-class an ellipse and define a Circle class (width=height=diameter)
{define-class CircleGraphic {inherits EllipseGraphic}
  field diameter:Distance 
  {constructor {default diameter:Distance = 1cm, ...}
    set self.diameter = diameter
    {construct-super ..., width=diameter, height=diameter}
  }
}

|| Declarative is sooo easy to display stuff lets combine them here
{HBox
    {CircleGraphic diameter=2cm},
    {CircleGraphic diameter=3cm, fill-color="red"},
    {CircleGraphic diameter=5cm}
}


This Example shows some OO capability along with Declarative layout.

Lines 1-3 are as described above.

Line four starts the definition of a new OO class called Circle that inherits from Ellipse (isa relationship).

Line 5 defines a field for the circle to hold the value of the diameter. Didn't really need it could just reference the width and height that is already stored for the ellipse. Likewise we should keep other programmers from changing the value of Width and Height independent of diameter. That's what I mean by simple OO examples are never enough.

Line 6 declares the constructor for the new class. A Little description on the parameters. the first parameter to Circle is a keyword argument that has a default value. Curl supports positional and keyword parameters. The second parameter is the ellipsis "...", also called 'rest arguments'. This is a construct for passing in an arbitrary number of arguments and you get to parse them. It is very helpful and usually isn't too much work.

Line 7 Saves in the diameter value in the objects field called diameter.

Line 8 calls the constructor for Ellipse but overwrites any mention of width and height with the diameter given.

The rest of the program closes out constructor and class definition then creates a simple display to hold a triple of circles. Note that other information is passed to the ellipse (fill-color) by way of the rest arguments. This is a screen capture of the output:


The dynamic nature of Curl can be used to create composite applications. Here comes a simple composite program that includes a chat capability and a web browser. Just save the script below as a file


say "c:\webbrowser.curl" and type the following line into your "Run..." field

"c:\Program Files\Curl Corporation\Surge\4\bin\curl.exe" c:\webbrowser.curl

Requirement is that you have Internet Explorer and Curl 3 installed.

You can also show the address bar, automagically fill in forms etc.