MN8 Tutorial
Useful things to know.
previous :: contents ::

Back to > Overview > MN8 Tutorial

Useful things to know.

Author: Remus Pereni <remus@nolimits.ro>
Table of Contents
1. Navigation in concept instances.
    1.1. Attributes
    1.2. Elements
    1.3. Methods
    1.4. Variables
    1.5. Series and Maps navigation


1. Navigation in concept instances.

In the examples presented in the previous chapters you could see all kind of symbols used in our scripts, symbols like: $, @, /, .

Because we have many ingredients in an concept, ingredients like element, attributes among the usual ones like variables and methods we had to produce a naming convention in order to be able to tell the MN8 interpreter that we are talking about the name attribute and not the name element, variable or method. So, this is the naming convention we where able to came up with:

1.1. Attributes

We use the @ (at) character to denote an attribute's value in any MN8 based script or concept. I deliberately said value because even if internally the attributes are instances of the Attribute concept, concept which as everything in MN8 is an full blown object with constructors, methods and fields, the @ notation always refers to the attribute's value. If we need the attribute concept then we have to use reflection and the getConceptAttribute or getConceptAttributes method.

Example:

		
		#---- Person.mn8 ----
		define Person [
			@name
			
			: create [
				@name =	"Remus Pereni"	  
			]
			
			static : main ($args typeof	Series)	[
				$this typeof Person
				
				print $this@name
				print $this@name.getConceptType
			  
				print $this.getConceptAttribute("name").getConceptType				
			]	
		]
		
Running this concept we'll produce:
		Remus Pereni
		String
		Attribute		 
		

As you could see, getting the concept type of $this@name produces String, which is exactly the type of the attributes value. To get the actual attribute we use $this.getCoceptAttribute with the desired attribute name as parameter.

top

1.2. Elements

We use slash "/" as element reference. In contrast with the attribute a slash before the element name refers to the actual element concept, another slash after the element's name transforms the element reference into a reference to its value.

Let's see this in an example:

		
		#---- Person.mn8 ----
		define Person [
			name
			
			: create [
				/name/ = "Remus	Pereni"	   
			]
			
			static : main ($args typeof	Series)	[
				$this typeof Person
				
				print "The element value $this/name/ is: ",	$this/name/
				print "The element $this/name is: ", $this/name
				print "The element value's type	$this/name/.getConceptType is: ", $this/name/.getConceptType
				print "The element type	$this/name.getConceptType is: ", $this/name.getConceptType	  
				print "Same	as above but using reflection $this.getConceptElement(\"name\").getConceptType is: ", $this.getConceptElement("name").getConceptType				
			]	
		]
		
And the result of the above concept:
		The	element	value $this/name/ is: Remus	Pereni
		The	element	$this/name is: name
			Remus Pereni
		The	element	value's	type $this/name/.getConceptType	is:	String
		The	element	type $this/name.getConceptType is: Element
		Same as	above but using	reflection $this.getConceptElement("name").getConceptType is: Element
		

As you can see in the above example $this/name/ will get us the value of the name element, to get the actual element we use simply $this/name without the last slash.

top

1.3. Methods

As in most programming languages the method delimiter is dot (.). In contrast with the other programming languages any method at any time starts with a dot.

		
		#---- Person.mn8 ----
		define Person [
			@firstName = "Remus"
			@lastName =	"Pereni"
			
			: getFirst [
				return @firstName	 
			] typeof String
			
			: getFullName [
				return .getFirst + " " + @lastName	  
			] typeof String
		
			static : sayHello [
				print "Hello"	 
			]
		
			static : main ($args typeof	Series)	[
				$this typeof Person
				
				print $this.getFullName
				
				.sayHello()
				# or
				Person.sayHello
			]		
		]
		
By executing the concept we'll get:
		Remus Pereni
		Hello
		Hello
		

Another thing you could notice is that you don't have to use the left and right parentheses if the method has no arguments.

top

1.4. Variables

In MN8 script any variable begins with a dollar ($) sign, just like in Basic. There is nothing more to be said about variables except, maybe, that their usage is allowed only inside methods you cannot mix variables with attribute and elements definition.

top

1.5. Series and Maps navigation

Series and Maps present some special navigation facilities. Among accessing their content through the conventional methods: elementAt and getValue($key) both simulates multi elements.

The simplest way to use this multi element simulation is accessing the map and series content using the position as element name. The best way to exemplify this is through an simple example.

		
		#---- NavigationTest.mn8 ----
		print "The series example"
		
		$i typeof Series
		$i.add("Red")
		$i.add("Yellow")
		$i.add("Blue")
		
		print $i.elementAt(1)
		print $i/1/
		
		print $i.elementAt(3)
		print $i/3/
		
		print "The map example"
		
		$m typeof Map
		$m.add("firstColor", "red")
		$m.add("secondColor", "yellow")
		$m.add("thirdColor", "blue")
		
		print $m.getKeys/1/, " ",  $m.getValues/1/
		print $m.getKeys/3/, " ",  $m.getValues/3/
		
With the result:
		The	series example
		Red
		Red
		Blue
		Blue
		The	map	example
		firstColor red
		thirdColor blue
		

Sure this goes as deep as you want, as exposed in the example bellow.

		
		#---- NavigationTest.mn8 ----
		$roFlag	typeof Series
		$roFlag.add("Red")
		$roFlag.add("Yellow")
		$roFlag.add("Blue")
		
		$huFlag	typeof Series
		$huFlag.add("Red")
		$huFlag.add("White")
		$huFlag.add("Green")
		
		$flags typeof Series
		$flags.add(	$roFlag)
		$flags.add(	$huFlag)
		
		print $flags/1/2/
		print $flags/2/2/
		
With the result:
		Yellow
		White
		

Among the position there is another possible navigation, by name or type. What happens is that any concept that is added to a Series is checked first for a getName method. If there is such a method then the string returned by the method can also be used in the navigation.

		
		#---- Flag.mn8 ----
		define Flag	[
			@countryName
			@countryCode
			
			color* typeof Element 
			
			: getName [
			   return @countryName 
			] typeof String
			
			static : main ($args typeof	Series)	[
				$roFlag	typeof Flag
				$roFlag@countryName	= "Romania"
				$roFlag@countryCode	= "ro"
				$roFlag/color.createNewEntry 
				$roFlag/color.createNewEntry
				$roFlag/color.createNewEntry
				$roFlag/color/1/ = "Red"		
				$roFlag/color/2/ = "Yellow"		   
				$roFlag/color/3/ = "Blue"
				
				print $roFlag.toXML
				
				$flags typeof Series
				$flags.add($roFlag)
				print $flags/Romania/	 
			]	
		]
		
The result of the above script is:
		
		<Flag countryName="Romania"	countryCode="ro">
		<color>Red</color>
		<color>Yellow</color>
		<color>Blue</color>
		</Flag>
		Flag
			countryName: Romania	countryCode: ro
		color
			Red
		color
			Yellow
		color
			Blue		   
		

In case the concept does not have a getName method then the concept type is taken in consideration. This behavior can be observed in the script bellow:

		
		#---- NavigationTest.mn8 ----
		$ser typeof	Series
		$ser.add("red")
		$ser.add(1)
		$ser.add("yellow")
		$ser.add(2)
		$ser.add(true)
		
		print "The series contains:	"
		print $ser
		
		print "$ser/String/1/ produces:"
		print $ser/String/1/
		
		print "$ser/String/	produces:"
		print $ser/String/
		
		print "$ser/Logical/ produces:"
		print $ser/Logical/
		
		print "$ser/Integer/2/ produces:"
		print $ser/Integer/2/
		
And the result:
		The	series contains: 
		red
		1
		yellow
		2
		true
		$ser/String/1/ produces:
		red
		$ser/String/ produces:
		red
		yellow
		$ser/Logical/ produces:
		true
		$ser/Integer/2/	produces:
		2
		

top


© 2001