Dart Symbol

 

Dart Symbol

Symbol object is used to specify an operator or identifier declared in a Dart programming language. Generally, we do not need to use symbols while Dart programming, but they are helpful for APIs. It usually refers to identifiers by name, because identifier names can vary but not identifier symbols.

Dart symbols are dynamic string name that is used to derive the metadata from a library. It mainly accumulates the connection between human-readable strings that are enhanced to be used by computers.

Symbols have a term which is called Reflection; it is a technique that used to the metadata of at run-time, for example - the number of methods used in class, a number of constructors in a class, or numbers of arguments in a function.

The dart:mirrors library has all of the reflection related classes. It can be used with the command-line applications as well as web applications.

Syntax -

The hash(#) symbol, followed by the name is used to define Symbol in Dart

. The syntax is given below.

Syntax -

  1. Symbol obj = new Symbol("name")  

Here, the valid identifier such as function, valid class, public member name, or library name can be used in place of name value.

  1. #radix  
  2. #bar  

Let's understand the following example.

Example -

  1. library foo_lib;     
  2. // libarary name can be a symbol     
  3.   
  4. class Foo {           
  5.    // class name can be a symbol    
  6.    m1() {          
  7.       // method name can be a symbol   
  8.       print("Inside m1");   
  9.    }   
  10.    m2() {   
  11.       print("Inside m2");   
  12.    }   
  13.    m3() {   
  14.       print("Inside m3");   
  15.    }   
  16. }  

In the above code, we have declared a class Foo in a library foo_lib. The class contains the methods m1, m2, and m3. We save the above file as foo.dart.

Now, we are creating new file FooSymbol.dart and run the following code.

FoolSystem.dart

  1. import 'dart:core';   
  2. import 'dart:mirrors';   
  3. import 'Foo.dart';    
  4.   
  5. main() {   
  6.    Symbol lib = new Symbol("foo_lib");     
  7.    //library name stored as Symbol   
  8.      
  9.    Symbol clsToSearch = new Symbol("Foo");    
  10.    // class name stored as Symbol    
  11.      
  12.    if(checkIf_classAvailableInlibrary(lib, clsToSearch))    
  13.    // searches Foo class in foo_lib library   
  14.       print("class found..");   
  15. }    
  16.      
  17. bool checkIf_classAvailableInlibrary(Symbol libraryName, Symbol className) {   
  18.    MirrorSystem mirrorSystem = currentMirrorSystem();   
  19.    LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName);   
  20.         
  21.    if (libMirror != null) {   
  22.       print("Found Library");   
  23.       print("checkng...class details..");   
  24.       print("No of classes found is : ${libMirror.declarations.length}");   
  25.       libMirror.declarations.forEach((s, d) => print(s));    
  26.            
  27.       if (libMirror.declarations.containsKey(className)) return true;   
  28.       return false;   
  29.    }   
  30. }  
  31. </pre></div>  
  32. <p>The above code will show the following output.</p>  
  33. <p><strong>Output:</strong></p>  
  34. <div class="codeblock"><pre>  
  35. Found Library  
  36. checkng...class details..  
  37. No of classes found is : 1  
  38. Symbol("Foo"// Displays the class name  
  39. class found..  
  40. </pre></div>  
  41. <p><strong>Example - 2 : Print the number of instance methods of class</strong></p>  
  42. <p>In the following example, The Dart provides predefine class <strong>ClassMirror</strong> which helps us to display the number of instance methods of class.</p>  
  43. <p><strong>Example -</strong></p>  
  44. <div class="codeblock"><textarea name="code" class="java">  
  45. import 'dart:core';   
  46. import 'dart:mirrors';   
  47. import 'Foo.dart';    
  48.   
  49. main() {   
  50.    Symbol lib = new Symbol("foo_lib");   
  51.    Symbol clsToSearch = new Symbol("Foo");    
  52.    reflect_InstanceMethods(lib, clsToSearch);   
  53. }    
  54. void reflect_InstanceMethods(Symbol libraryName, Symbol className) {   
  55.    MirrorSystem mirrorSystem = currentMirrorSystem();   
  56.    LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName);   
  57.      
  58.    if (libMirror != null) {   
  59.       print("Found Library");   
  60.       print("checkng...class details..");   
  61.       print("No of classes found is : ${libMirror.declarations.length}");   
  62.       libMirror.declarations.forEach((s, d) => print(s));    
  63.         
  64.       if (libMirror.declarations.containsKey(className)) print("found class");  
  65.       ClassMirror classMirror = libMirror.declarations[className];   
  66.         
  67.       print("No of instance methods found is ${classMirror.instanceMembers.length}");  
  68.       classMirror.instanceMembers.forEach((s, v) => print(s));   
  69.    }   
  70. }     

Output:

Found Library
checkng...class details..
No of classes found is : 1
Symbol("Foo")
found class
No of instance methods found is 8
Symbol("==")
Symbol("hashCode")
Symbol("toString")
Symbol("noSuchMethod")
Symbol("runtimeType")
Symbol("m1")
Symbol("m2")
Symbol("m3")

Dart Convert Symbol to String

We can convert the Dart symbol into the string by using a built-in class MirrorClass, which is provided by the dart:mirror package. Let's understand the following example.

Example -

  1. import 'dart:mirrors';   
  2. void main(){   
  3.    Symbol lib = new Symbol("foo_lib");   
  4.    String name_of_lib = MirrorSystem.getName(lib);   
  5.      
  6.    print(lib);   
  7.    print(name_of_lib);   
  8. }  

Output:

Symbol("foo_lib")
foo_lib

Comments