Menu

Declaring factories, creating a class instance

Mark Anthony Taylor (Shyreman)

Class instances (objects) are created by use of factory functions, which are public. So while interfaces and factories are public objects, the class implementation is kept private.

(factory <fully-qualified-factory-name> <fully-qualified-interface-name>
     (<input_type1> <input_name1>) ...   (<input_typeN> <input_nameN>)
                                    :
                                    body
)

where fully-qualified-factory-name is a namespace prefixed factory name, the token on the right
separated by the namespace with a period character. The token on the right must begin with a capital
letter A-Z and is followed by alphanumerics A-Z a-z and 0-9.

(Example:

(factory Sys.CreateDog Sys.IDog (Int32 age)(Int32 breed): (construct Dog age breed))

The above example creates a factory CreateDog in the Sys namespace. Factory functions map to no output
and we see unlike methods and normal functions that they do not use the mapping symbol.

The body must contain an expression

(construct <class-name>)

which invokes method <class-name>.Construct with the given arguments.

In our case

(Dog.Construct (Int32 age)(Int32 breed)) is called.

If the list of arguments exactly matches the type and order of the Construct method, then the factory
is inlined, a type of optimization by the compiler that reduces the code necessary to invoke the
Construct method.


Related

Wiki: Content