N9I - 2/2/2010

Written on Wed, 03 Feb 2010 07:10:15 +0000 - Last updated on Sun, 11 Apr 2010 17:57:14 +0000A journal entry in the development of the N9I programming language, dated February 2, 2010.

At operator changes

The at operator (@) will be expanded to deal with 2 identifiable sources of data: "resources" and "providers". Also, strings marked with the at operator cannot contain escape sequences (such as \n) and unescaped backslashes are allowed.

Resource

A resource is an item, located locally or remotely, that can be accessed with a name, usually a Uniform Resource Identifier. A resource can be read: String fileContents = @"http://eightzees.net/file.txt"; and it can be assigned: @"file.txt" = fileContents; Resources may be of any variable type and/or file type. The at operator, in this instance, is pointing at the locationof the resource that is being accessed. Resources have an associated magic functionality, in that the resource can be used anywhere a program variable can. N9I will attempt to convert the resource into the needed representation. For example, if the context calls for a string, N9I will attempt to convert the resource into a string. Conversion may fail; this will raise an exception. If the programmer wishes to force a resource to be read as a specific type, she may cast it: (Employee) @"employees/dave.xml"; // forces the XML file to be parsed as an Employee object N9I contains 3 methods for (un)serializing objects - binary, JSON, and XML. JSON and XML are automatically picked if the resource name has the requisite extension; else, the binary method is used. Note that XML documents may also be interpreted as plain XML and that JSON data may also be interpreted as a dictionary. A resource may be aliased to an object of type ResourceAlias.

Provider

A provider, on the other hand, is a source of data. Providers are nearly identical to the concept of streams or pipes in that data is read in from them and written out into them. Providers on their own don't represent items with any value. The programmer, however, can read in values from the provider: SomeObject object <- @"eightzees.net:27700" // assume port 27700 returns objects in XML format Providers can also be written to: anotherObject -> @"abwayax.com:27700"; // assume this service accepts objects Note that providers have their own set of operators instead of the assignment operator. This is distinct from the resource because the service running on port 27700 doesn't represent an object in and of itself; rather, it provides objects to the client. Whereas subsequent references to @"file.txt" should return the same data (assume file.txt is not being touched by another process), repeatedly reading in an object from @"eightzees.net:27700"should not necessarily return the same result. Likewise, String foo = @"foo.txt"; @"foo.txt" = foo; should result in no change to foo.txt, however, String foo <- @"eightzees.net:27800"; // assume port 27800 returns strings foo -> @"eightzees.net:27800"; // ... and accepts themmay alter the state of the service running on port 27800. Providers have the same magic functionality accorded to resources; i.e. using the provider alone in an expression results in it being read from. Generally, whereas a resource usually points to a file, a provider represents an I/O stream or a network socket. N9I applications expose one provider, @"n9i://console", which is aliased to the IO.Console member variable. Reading from this provider reads in from standard in, while writing to it writes to standard out. Standard error has its own provider, @"n9i://error" (aliased to IO.Error). A provider may be aliased to an object of type ProviderAlias. (See N9I - 2/3/2010for how to use a Provider to create a server socket)

Native JSON support

N9I supports the JavaScript Object Notation natively. In N9I, JSON represents dictionaries (associative arrays): Dictionary words = { "cat": "gato", "dog": "perro", "man": "hombre" }; Dictionaries and objects may also be serialized in JSON: @"words.json" = words;JSON can also be used to create a typed object, like so: Employee dave = (Employee) { "name": "Dave", "position": "Janitor", "salary": 20000 }; This has the effect of creating a new object and then traversing through the JSON dictionary and applying each key to a "setter" method on the new object (i.e. "Dave" is passed to dave.setName() and so on - see N9I - 2/4/2010 for information on get/set methods).

Variable interpolation

N9I natively supports variable interpolation, that is, the automatic substitution of variables and expressions within strings. To mark a string for interpolation use the $ operator. Example: $"A variable's value is ${variable}. Pi is ${Math.PI}. The area of a circle is ${Math.PI * (r^2)}." To use $ in conjunction with the at sign, place the at sign first: @$"http://${domain}/${page}?${variable}=${value}"