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 –

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.

#radix  

#bar

Let’s understand the following example.

Example –

library foo_lib;     

// libarary name can be a symbol     

  

class Foo {           

   // class name can be a symbol    

   m1() {          

      // method name can be a symbol   

      print("Inside m1");   

   }   

   m2() {   

      print("Inside m2");   

   }   

   m3() {   

      print("Inside m3");   

   }   

} 

    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

    import 'dart:core';   
    
    import 'dart:mirrors';   
    
    import 'Foo.dart';    
    
      
    
    main() {   
    
       Symbol lib = new Symbol("foo_lib");     
    
       //library name stored as Symbol   
    
         
    
       Symbol clsToSearch = new Symbol("Foo");    
    
       // class name stored as Symbol    
    
         
    
       if(checkIf_classAvailableInlibrary(lib, clsToSearch))    
    
       // searches Foo class in foo_lib library   
    
          print("class found..");   
    
    }    
    
         
    
    bool checkIf_classAvailableInlibrary(Symbol libraryName, Symbol className) {   
    
       MirrorSystem mirrorSystem = currentMirrorSystem();   
    
       LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName);   
    
            
    
       if (libMirror != null) {   
    
          print("Found Library");   
    
          print("checkng...class details..");   
    
          print("No of classes found is : ${libMirror.declarations.length}");   
    
          libMirror.declarations.forEach((s, d) => print(s));    
    
               
    
          if (libMirror.declarations.containsKey(className)) return true;   
    
          return false;   
    
       }   
    
    }  
    
    </pre></div>  
    
    <p>The above code will show the following output.</p>  
    
    <p><strong>Output:</strong></p>  
    
    <div class="codeblock"><pre>  
    
    Found Library  
    
    checkng...class details..  
    
    No of classes found is : 1  
    
    Symbol("Foo") // Displays the class name  
    
    class found..  
    
    </pre></div>  
    
    <p><strong>Example - 2 : Print the number of instance methods of class</strong></p>  
    
    <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>  
    
    <p><strong>Example -</strong></p>  
    
    <div class="codeblock"><textarea name="code" class="java">  
    
    import 'dart:core';   
    
    import 'dart:mirrors';   
    
    import 'Foo.dart';    
    
      
    
    main() {   
    
       Symbol lib = new Symbol("foo_lib");   
    
       Symbol clsToSearch = new Symbol("Foo");    
    
       reflect_InstanceMethods(lib, clsToSearch);   
    
    }    
    
    void reflect_InstanceMethods(Symbol libraryName, Symbol className) {   
    
       MirrorSystem mirrorSystem = currentMirrorSystem();   
    
       LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName);   
    
         
    
       if (libMirror != null) {   
    
          print("Found Library");   
    
          print("checkng...class details..");   
    
          print("No of classes found is : ${libMirror.declarations.length}");   
    
          libMirror.declarations.forEach((s, d) => print(s));    
    
            
    
          if (libMirror.declarations.containsKey(className)) print("found class");  
    
          ClassMirror classMirror = libMirror.declarations[className];   
    
            
    
          print("No of instance methods found is ${classMirror.instanceMembers.length}");  
    
          classMirror.instanceMembers.forEach((s, v) => print(s));   
    
       }   
    
    }     

      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 –

      import 'dart:mirrors';   
      
      void main(){   
      
         Symbol lib = new Symbol("foo_lib");   
      
         String name_of_lib = MirrorSystem.getName(lib);   
      
           
      
         print(lib);   
      
         print(name_of_lib);   
      
      }

      Output:

      Symbol("foo_lib")
      foo_lib
      

      Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *