Coding in HCL
Introduction to the syntax of HCL
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
General Syntax
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:
+
Addition
-
Subtraction
*
Multiplication
/
Division
^
Exponent
=
Equals
!=
Not Equals
>
Greater than
>=
Greater than or Equal to
<
Less than
<=
Less than or Equal to
Strings can be concatenated using
+
operator and strings can be repeatedn
times using the*
operator
Comments
HCL supports single and Multi-line comments.
Single-line comments start with a
#
characterMulti-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.
Data types in HCL
HCL has 4 simple data types:
Number
data type: This data type stores integers as well as decimals. This data type can store values in the range toString
data type: This data type stores strings and supports Unicode encoding.Boolean
data type: This data type stores two valuestrue
andfalse
. This data type is returned during comparison operationsThe
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.
Variables
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.
Last updated