N9I - 6/19/2010

Written on Sun, 20 Jun 2010 20:07:07 +0000 - Last updated on Sun, 20 Jun 2010 20:15:37 +0000

Main Function Examples

Traditional

public void main(string[] args) { ... }Example invocation: example arg1 arg2 "arg3 with spaces"

Named arguments

public void main(string[string] args) { ... } Example invocation: example --arg1:foo --arg2:bar "--arg3:arg with spaces"

Specific arguments

public void main(string name, int age) { ... }Example invocation: example --name:Fred --age:30Or: example Fred 30 (Note: When declared in this way, the main function can be overloaded (see below). If the main function is overloaded, the main function that best matches the given arguments is called. This could be used, for example, to create a no-arg main function that prints out usage information and an "actual" main function that takes arguments. If the closest matching function does not perfectly match the number of args given, the unspecified args will be null)

Default main function

... If no main function exists, one will be created using all of the free-floating statements in the source file. If this is done, no arguments can be passed in.

On Named Parameters

Named parameters can be passed into the program any of the following ways: * --name=value * --name:value * --name value * --name (for boolean flags) Additionally, for specific arguments, arguments without a name are assigned to the main function arguments sequentially, like an ordinary function call. Arguments that are not specified are given a value of null (for objects), zero (for primitives), or a default value given in the function signature.

Function Syntax

[public|private] [asynchronous] [pure] [void|type] name(type var[=defaultValue],type var2...) [throws Exception] { .... } * Default visibility is private. * Default return type is void. * Functions can have optional/default arguments. Since these arguments can be omitted, they are not considered part of the function signature for the purpose of overloading (see below).

Overloading

Functions can be overloaded. When a function is overloaded, the one that most matches the given arguments and the expected return type is used. Note that optional arguments are not considered when overloading a function.

Class Syntax

[public|private] [final|abstract] class Name [extends AClass] [implements Interface1, Interface2...] [reimplements Class1,Class2...] { [public|private] [static] [final] type var1[=defaultValue]; // method syntax is the same as function syntax } For the most part, class syntax is identical to Java's. However: * default access level for everything is private. This is to ensure that the programmer only exposes methods and variables that are necessary. There is no "protected" or package-level access. * Inheritance and interface implementation work similar to how they do in Java, as do the final/abstract designations. A class can implement any number of interfaces but can only inherit from one class. However... * The reimplements keyword allows a class to "implement" another class (even a class marked final) as if it were an interface. This means that all of the public methods of the reimplemented class must be reimplemented. This is not inheritance and does not cause the reimplementing class to inherit anything other than public method declarations.

Array Syntax

type[size] arr = {element1,element2,...}; The size is optional. If not given, the array is created as a dynamic list. Example of fixed size array: string[3] people = {"Fred","Homer","Peter"}; Example of dynamic array: string[] foods = {"apples","oranges","bananas"}; Example of accessing an array element:foods[2]; // "bananas" Example of adding a new entry to a dynamic array: foods.push("pizza");

Dictionary Syntax

valueType[keyType[size]] name = {key: value, key2: value2}; A dictionary is a mapping of keys to values. When declaring a dictionary, the types of both keys and values must be given. As with an array, size is optional. However, unlike the array, if no size is given then the square brackets around the size should be removed rather than simply made empty. Example of fixed size dictionary: string[string[3]] spanishWords = {"Dog": "Perro", "Cat": "Gato", "Man": "Hombre"}; Example of dynamic dictionary: string[string] users = {"Izwzyzx": "Jordan", "Abwayax": "Gregory", "Zowayix": "Andrew"}; Example of accessing a dictionary element: users["Zowayix"]; // "Andrew" Example of adding a new entry to a dynamic dictionary:users["Wa"] = "Madeleine";

String Syntax

(note: in the below examples, where a symbol is attached to the beginning of a string, there may be no space between that symbol and the string; however, in all but the blockquote string, parentheses may be put around the string; e.g. @"foo" can be written as @("foo"). Parentheses must be used if strings are being concatenated; e.g. @"foo" + "bar" actually causes the resource referred to by "foo" to be added to the literal string "bar"; @("foo + "bar") obtains the resource "foobar") "string literal" This is the basic string literal syntax. @"http://example.com/foo.html" This is a resource string. Whenever one of these is encountered, it is turned into a reference to the resource pointed to by the given URI. Resources are simply variable-like objects that exist outside the program; they can be local files or files on a server accessed through HTTP, FTP, or some other internet medium. They can be simple, like text files; or they can be complex files like XML documents or ZIP archives or programmer-defined classes. Resources can be read like normal variables and, where supported, they can be assigned too. For example: @"textfile.txt" = "This text is being stored in a text file!" $"foo and ${bar}" This is a string that contains interpolated expressions. This allows a programmer to insert expressions directly into a string without needing to use a printf-like utility or concatenation. The $ must be in front of the string for the compiler to recognize it as interpolated. %{ ... } This is a blockquote string. These are special strings that may span multiple lines. Within such a string, the only characters that must be escaped are the { } characters, unless they are used as a pair (i.e. the number of left-braces matches the number of right-braces). This is because blockquotes are parsed like other brace blocks. Letters may be used between the % and the opening brace to control aspects of the string; the only supported one as of this draft is the letter i, for interpolation (see above).

Internationalization of strings

The language inherently supports internationalized strings. For every string literal encountered in source code, the compiler checks for any internationalized versions of the string. If any are found, the compiler invisibly wraps gettext-like functionality around that string. This applies to all strings; therefore, one could internationalize even resource files using this technique.

Access level hierarchy

* Packages: folders in a project. Packages may contain: ** Packages: packages may be nested in each other. ** Modules: individual files. A module may contain: *** Functions: blocks of code that take in any number of arguments and return a value. *** Variables: names associated with pieces of data that can be changed at any time. *** Classes: programmer-defined types. An instance of a class is called an object. Classes may contain: **** Methods: like functions, but are "owned" by an instance of the class and are invoked on that instance. **** Variables: data held by an instance of the class. **** Classes: internal classes usually defined only for use within the containing class Note: It is possible for a package and a module to have the same name, to simulate nested modules. In this case, there can be no name conflicts between members of the package and members of the module.

Access modifiers

The language supports two basic levels of access: public and private. Anything that can be put in a module can be given an access modifier. Public means that the function, class, method, or variable can be accessed from anywhere in the entire program. Private means that access to it is limited only to the immediate containing context and anything contained within that context. For example, a private function can only be invoked by functions inside the same module, or by methods of an instance of a class inside that module. Private methods on a class, however, can only be invoked by methods of that class, and not by anything outside the class (even in the same module). However, for nested packages where a package shares the name of a module, nothing in the package can access anything private in the module. For example, consider module Foo, package Foo, and module Foo.Bar. Even though it appears that Foo.Bar is somehow a member of module Foo, Foo.Bar is prohibited from accessing private members of module Foo. This is because the module Foo and the package Foo are different entities with the same name.