Coding in HCL
Introduction to the syntax of HCL
Last updated
Introduction to the syntax of HCL
Last updated
Every programming language has its own syntax and semantics, and HCL too has them. Being a Turing-Complete language, this language supports
General Input/Output
Type Conversion
Conditionals and Loops
Functions
Object-Oriented Programming
Modular Programming
and many more.... Let's see each of them in detail
Statements in HCL ends with a ;
(semicolon) character.
Blocks in HCL are enclosed within {
and }
characters.
Function are called with its name followed by ()
(parentheses) and contains arguments separated by ,
(comma)
Other than these, HCL supports the following operators:
Operator | Name |
---|---|
Strings can be concatenated using +
operator and strings can be repeated n
times using the *
operator
HCL supports single and Multi-line comments.
Single-line comments start with a #
character
Multi-line comments starts with #[
character and ends with ]
There is also support for nested comments, wherein the user can nest comments in their program. Note that while nesting, there is no need in using #
before the [
. Instead directly use the []
characters.
Proper usage of comments is encouraged in HCL. Most of the code is self-explanatory. Thus, programs must not be filled up with comments. Use comments before functions and blocks of code where explanation is necessary. This makes your code neat and readable for other programmers.
HCL has 4 simple data types:
String
data type: This data type stores strings and supports Unicode encoding.
Boolean
data type: This data type stores two values true
and false
. This data type is returned during comparison operations
The Null
value: Although it is not an data type, this value is returned when a uninitiated object's value is requested.
Other than these, there are many other Complex data types which are available at structures
module.
The above datatypes are stored in a constant form called literals. These values need to be stored in computer's memory to use them. These are stored in memory as Variables.
Variables must be declared using the let
keyword in HCL to use them.
By default, the declared variable carried null
. To assign a value to the declared variable, we use the :=
(assignment) operator.
Note that the variable can be reassigned to a new value using the same operator
As HCL is a dynamically-typed language, a variable can carry value of any type(like in Python but not as in C/Java). As the declaration and assignment is so common, this can be combined to a single line.
Number
data type: This data type stores integers as well as decimals. This data type can store values in the range to
+
Addition
-
Subtraction
*
Multiplication
/
Division
^
Exponent
=
Equals
!=
Not Equals
>
Greater than
>=
Greater than or Equal to
<
Less than
<=
Less than or Equal to