1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-17 20:11:57 +02:00

Added modules and main()

Worth noting that this will change the diff of the parallel section quite a bit, since they became the body of the main procedure.
Thus each line in intented.
This commit is contained in:
Ian Bertolacci
2015-08-02 15:37:01 -07:00
parent 1f6f7b7843
commit b27d526822

View File

@@ -673,12 +673,65 @@ var copyNewTypeList = new GenericClass( realList, int );
for value in copyNewTypeList do write( value, ", " );
writeln( );
// Modules are Chapel's way of managing name spaces.
// The files containing these modules do not need to be named after the modules
// (as is with Java), but files implicitly name modules.
// In this case, this file implicitly names the 'learnchapel' module
module OurModule {
// We can use modules inside of other modules.
use Time;
// We'll use this a procedure in the parallelism section.
proc countdown( seconds: int ){
for i in 1..seconds by -1 {
writeln( i );
sleep( 1 );
}
}
// Submodule of Ourmodule
// It is possible to create arbitrarily deep module nests.
module ChildModule {
proc foo(){
writeln( "ChildModule.foo()");
}
}
module SiblingModule {
proc foo(){
writeln( "SiblingModule.foo()" );
}
}
} // end OurModule
// Using OurModule also uses all the modules it uses.
// Since OurModule uses Time, we also use time.
use OurModule;
// At this point we have not used ChildModule or SiblingModule so their symbols
// (i.e. foo ) are not available to us.
// However, the module names are, and we can explicitly call foo() through them.
SiblingModule.foo(); // Calls SiblingModule.foo()
// Super explicit naming.
OurModule.ChildModule.foo(); // Calls ChildModule.foo()
use ChildModule;
foo(); // Less explicit call on ChildModule.foo()
// We can declare a main procedure
// Note: all the code above main still gets executed.
proc main(){
// Parallelism
// In other languages, parallelism is typically this is done with
// complicated libraries and strange class structure hierarchies.
// Chapel has it baked right into the language.
// A begin statement will spin the body of that statement off into one new task.
// A begin statement will spin the body of that statement off
// into one new task.
// A sync statement will ensure that the progress of the main
// task will not progress until the children have synced back up.
sync {
@@ -901,6 +954,7 @@ var runningSumOfValues = + scan listOfValues;
var maxScan = max scan listOfValues;
writeln( runningSumOfValues );
writeln( maxScan );
}
```
Who is this tutorial for?