Optional Type annotations in Dart Language.

Introduction
One very interesting feature of Google's dart language is it's optional type annotations. 

Mandatory type annotation
Java language provides us with intrinsic types and gives us the ability to define our own types ( User Defined Types). It is mandatory to mention type when creating a variable. In Java language, types annotations are not Optional...they are Mandatory. Mandatory type annotations buy us benefits in static analysis and tooling, but they also bring their own set of constraints and verbosity to code.

No type Annotations
On the other hand in Javascript there are intrinsic types and really no first class notion for User Defined types similar to that in Java.  Javascript  developers resort to idioms and patterns to emulate type systems. Absence of annotations bring brevity and a lot of freedom but lose several benefits gained from static analysis and tooling.

Optional type Annotations
Dart makes type annotations optional. For smaller scripts you can skip type annotations. You can introduce type annotations when the code needs some structure. Infact, you can even pull back annotations after introducing them. So you get the benefits of types when you want them and you can avoid them or pull them back when they come in your way. 


The following program illustrates  how a function type can be created in Dart. A function type called CallBack is created using typedef keyword. Type annotations are optional as you can observe that  server1 uses type annotation whereas server2 function  works without type annotation.This program also illustrates that dart supports lambda functions and closures.



Program in Google's Dart Language

typedef void CallBack();

void main() {
  print("Entering main");
  client1();
  client2();
  client3();
  print("Leaving main");
}

// Client1 and it's custom code
void client1(){
  print("Entering client1");
  var name = "client1";
  
  // Innerfunction accessing captured variable name
  
  client1CustomCode() => print("${name} Custom Code");
          

  server1(client1CustomCode);
  server2(client1CustomCode);
  
  print("Leaving client1");
}


// Client2 and it's custom code
void client2(){
  print("Entering client2");
  var name = "client2";
  
  server1(()=>print("${name} Custom Code"));
  server2(()=>print("${name} Custom Code"));
  
  print("Leaving client2");
}

// Client3 and it's custom code
void client3(){
  print("Entering client3");
  server1(client3CustomCode);
  server2(client3CustomCode);
  print("Leaving client3");
}
void client3CustomCode(){
  print("Client3 Custom Code");
}


// Customizable Server with type annotation
void server1(CallBack callback){
    print("Entering Server1");
    callback();
    print("Leaving Server1");
}

// Customizable Server without type annotation
void server2(callback){
    print("Entering Server2");
    callback();
    print("Leaving Server2");
}

Output

Entering main
Entering client1
Entering Server1
client1 Custom Code
Leaving Server1
Entering Server2
client1 Custom Code
Leaving Server2
Leaving client1
Entering client2
Entering Server1
client2 Custom Code
Leaving Server1
Entering Server2
client2 Custom Code
Leaving Server2
Leaving client2
Entering client3
Entering Server1
Client3 Custom Code
Leaving Server1
Entering Server2
Client3 Custom Code
Leaving Server2
Leaving client3
Leaving main

copyright ©Rajesh Patkar, All rights reserved.

Comments

Popular posts from this blog

Introduced Java 10 in Java Technology Course.

Potter Metaphor.