Var And Var! In Microsoft Bosque Programming Language

This article talks about the declaration of var and var! variables in Bosque programming language.

Var And Var! In Microsoft Bosque Programming Language 

 
All programming languages have one central area that is used to declare the variables. In this article, we are going to learn how to declare a variable in Bosque programming.
 
var is used to declare the variable and the var keyword can be divided into two different types:
  1. var
  2. var!
  • var statement is used to declare a const variable
  • var! is used to declare a non-const variable.

var variable

 
We can declare the variable in two different ways.
  1. var Identifier = exp;    
  2. var identifier : Datatype = exp;   
The example declaration code is given below.
  1. var x:Int = 3; // variable declare as int and initialized.    
  2. var x =” Hello World”; // variable introduced and inferred to be of type string     
Once a variable has initialized, it can't be changed at a later point. In the below sample code, a variable is initialized as “Hello World” and is later changed to “Welcome Bosque.”
  1. namespace NSMain;  
  2. entrypoint  
  3. function main(): String {  
  4.  var x2 = "Hello World";  
  5.  x2 = "Welcome bosque";  
  6.  return x2;  
  7. }   
Compile the program. The Bosque compiler throws a parse error “variable defined as const.”
 
 

var! variable

 
In the case of var! the syntax can be modified by assignment statements. We can declare the var! variable in three different types. Two types are the same as var declaration while the third one is only a datatype declaration without initialization.
  1. var !identifier: Type;  
  2. var !Identifier = exp;  
  3. var !identifier: Datatype = exp;   
Below is the sample code using var! 
  1. namespace NSMain;  
  2. entrypoint  
  3. function main(): String {  
  4.  var !x2 = "Hello World";  
  5.  x2 = "Welcome bosque";  
  6.  return x2;  
  7. }   
var! must define the datatype or should be initialized otherwise compiler throws an error. In the below code, we have declared only a variable without defining the datatype or without initialization.
  1. namespace NSMain;  
  2. entrypoint  
  3. function main(): String {  
  4.  var !x;  
  5.  return x;  
  6. }   
Compile the program. The Bosque compiler throws an error.
 
 

var and var! assignment

 
var! is a variable that can be assigned to the var variable. The only rule is that var! must be initialized before assigning to var otherwise compiler throws an exception.
 
The below code shows that var! only is declared not assigned any value and it is assigned to var variable “x”. 
  1. namespace NSMain;  
  2. entrypoint  
  3. function main(): String {  
  4.  var !x: String;  
  5.  var x1 = x;  
  6.  return x;  
  7. }   
Compile the program. It throws an error.
 
 
Before assigning to var x1 = x, initialize the x variable, then it will work out.
  1. namespace NSMain;  
  2. entrypoint  
  3. function main(): String {  
  4.  var !x: String;  
  5.  x = "Difference between var and var!";  
  6.  var x1 = x;  
  7.  return x1;  
  8. }   
Run the program.
 
 

Conclusion

 
I hope you can understand the use of var and var! and the differences between them.
 
Happy Coding!!!