25
edits
Changes
→Language Basics
//https://chapel-lang.org/docs/primers/
=== Language Basics ===
==== Variable: ====
Variables are declared with the '''var''' keyword. Variable declarations must have a type, initializer, or both.
'''const''' and '''param''' can be used to declare runtime constants and compile-time constants respectively. A '''const''' must be initialized in place, but can have its value generated at runtime. A '''param''' must be known at compile time.
All three variable kinds can be qualified by the '''config''' keyword. This allows the initial value to be overridden on the command line.
config var myVariable2: bool = false; //./variable --myVariable=true
==== Procedures: ====
A procedure groups computations that can be called from another part of the program.
proc factorial(x: int) : int
{
if x < 0 then
halt("factorial -- Sorry, this is not the gamma procedure!");
return if x == 0 then 1 else x * factorial(x-1);
}
==== Classes: ====
A class is a type that can contain ''variables'' and ''constants'', ''called fields'', as well as ''functions'' and ''iterators'' called methods. A new class type is declared using the '''class''' keyword.
class C {
var a, b: int;
proc printFields() {
writeln("a = ", a, " b = ", b);
}
}
The '''new''' keyword creates an instance of a class by calling an initializer.
var foo = new C(1, 3);
foo.printFields();
==== Records: ====
Records are similar to classes, but there are several important differences:
=== Iterators ===