forked from chapel-lang/chapel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello2-module.chpl
47 lines (40 loc) · 1.74 KB
/
hello2-module.chpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// "Production-grade" hello world
/* This program is conceptually very similar to :ref:`hello.chpl
<primers-hello>`, but it uses a more structured programming style,
explicitly defining a module, a configuration constant, and a
main() procedure.
*/
//
// The following statement declares a module named 'Hello'. If a
// source file contains no module declarations, the filename minus its
// ``.chpl`` extension serves as the module name for the code it
// contains. Thus, 'hello' would be the automatic module name for the
// previous :ref:`hello.chpl <primers-hello>` example.
//
module Hello {
//
// This next statement declares a `configuration constant` named
// `message`. The type is inferred to be a string since the
// initializing expression is a string literal. Users may override
// the default values of configuration constants and variables on the
// executable's command-line. For example, we could change the
// default message for a given run using the command line: ``./hello
// --message="hiya!"``.
//
config const message = "Hello, world!";
// Any top-level code in a module is executed as part of the module's
// initialization when the program begins executing. Thus, in the
// previous one-line :ref:`hello.chpl <primers-hello>`, the presence
// of a `writeln()` at the file scope formed the implicit `hello`
// module's initialization and would be executed at program startup.
// Since there was no explicit `main()` function or any other
// top-level code, that's all that the program would do.
//
// In this program, we define an entry point for the program by
// defining a procedure named `main()`. This will be invoked after
// this module and all the modules it uses are initialized.
//
proc main() {
writeln(message);
}
}