Sections
|
<( Constructors )>
If you've gotten this far, you can relax because all of the hard stuff is out of the way. Constructors are simply a shorthand way of creating/initializing as well as assigning an object all in 1 simple step.
A constructor can take 3 different forms: a property constructor, an object constructor, or an array constructor. A property constructor is something you've already been using, but you didn't know it. Just surround the desired text with quotation marks and you've got a property constructor.
Constructors (since they're made for convenience) can be applied on the same line as a variable definition; just add white space, an equals sign, more whitespace, and then a constructor. The use of constructors is usually very intiutive, but here's an example of a property constructor anyway:
val someProperty = "Declaration and assignment in one step!" val anotherProperty = someProperty
|
! | Note:
Remember to use \" when you need to use a quotation mark inside of a property constructor.
|
The second line there isn't actually a constructor, but it demonstrates that you can do an ordinary assignment on the same line as a declaration. In fact, you can do assignments wherever you can use constructors.
The next type of constructor is the array constructor. These can quickly become unwieldy but they are very convenient if you need to define several lines of text in 1 assignment. Just put a list of constructors, separated by commas, inside of a pair of braces ({}). An example would probably help here:
val[] arrayOfProps = {"First element", "Second element", someProperty} val[] moreProps = arrayOfProps
|
The final type of constructor that you can use in DML2 is the object constructor. Remember when you learned how to define object types and you typed in the members line-by-line? That order now matters. To make an object constructor just type the keyword new followed by white space and then the type of object you are creating (e.g. Date). Follow this by a set of comma-separated constructors enclosed in parentheses (()). To help visualize this, I will show an extended example that encompasses all 3 constructor types.
object TextContent { val[] lines }
object Page { val title TextContent content }
Page hello = new Page("Hello World", new TextContent({"If this makes sense, you're a master of constructors!", "Oh yeah, forgot...", "Hello World!"}))
|
To make understanding this example easier, try breaking it down into smaller parts. For example, look only inside of the new Page constructor and you'll see 2 constructors inside of it, the first being a property constructor, and the second another object constructor. Next, try looking only in the new TextContent constructor, and you'll see 1 more constructor, an array constructor. Inside of the array constructor (which corresponds to Page.TextContent.lines) there are several property constructors that define the individual lines.
Lastly, you can use constructors to make default values for objects. For example, most of the pages in this documentation are named doc# where # is the number of the page. Rather than typing this every time for each section I add, I just set up a default constructor like this:
object Section { val title = "New Section" val file = "doc<(&index)>.html" val[] content = {"Content will go here"} }
|
With the default constructor, all I have to do is not set the file and it will automatically be set to the default of doc<(&index)>.html.Of course, for the first page of documentation, the file name is index.html. To override the default constructor, just make a simple assignment to whatever value you'd like.
! | Note:
<(&index)> is an automatic tag that inserts the current object's place in an array (starting with 1, not zero). All of the automatic tags are listed in their own section.
|
|
|