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