Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

D Characters

d

import std.stdio;
import std.uni;

void main() { 
   writeln("Is ğ lowercase? ", isLower('ğ')); 
   writeln("Is Ş lowercase? ", isLower('Ş'));  
   
   writeln("Is İ uppercase? ", isUpper('İ')); 
   writeln("Is ç uppercase? ", isUpper('ç')); 
   
   writeln("Is z alphanumeric? ",       isAlpha('z'));  
   writeln("Is new-line whitespace? ",  isWhite('\n')); 
   
   writeln("Is underline whitespace? ", isWhite('_'));  
   
   writeln("The lowercase of Ğ: ", toLower('Ğ')); 
   writeln("The lowercase of İ: ", toLower('İ')); 
   
   writeln("The uppercase of ş: ", toUpper('ş')); 
   writeln("The uppercase of ı: ", toUpper('ı')); 
}

D Property Functions

d

import std.stdio;

struct Rectangle { 
   double width; 
   double height;  

   double area() const @property {  
      return width*height;  
   } 

   void area(double newArea) @property {  
      auto multiplier = newArea / area; 
      width *= multiplier; 
      writeln("Value set!");  
   } 
}

void main() { 
   auto rectangle = Rectangle(20,10); 
   writeln("The area is ", rectangle.area);  
   
   rectangle.area(300); 
   writeln("Modified width is ", rectangle.width); 
}

D Inout Functions

d

import std.stdio;

inout(char)[] qoutedWord(inout(char)[] phrase) { 
   return '"' ~ phrase ~ '"';
}

void main() { 
   char[] a = "test a".dup; 

   a = qoutedWord(a); 
   writeln(typeof(qoutedWord(a)).stringof," ", a);  

   const(char)[] b = "test b"; 
   b = qoutedWord(b); 
   writeln(typeof(qoutedWord(b)).stringof," ", b); 

   immutable(char)[] c = "test c"; 
   c = qoutedWord(c); 
   writeln(typeof(qoutedWord(c)).stringof," ", c); 
} 

D Variadic Function

d

import std.stdio;
import core.vararg;

void printargs(int x, ...) {  
   for (int i = 0; i < _arguments.length; i++) {  
      write(_arguments[i]);  
   
      if (_arguments[i] == typeid(int)) { 
         int j = va_arg!(int)(_argptr); 
         writefln("\t%d", j); 
      } else if (_arguments[i] == typeid(long)) { 
         long j = va_arg!(long)(_argptr); 
         writefln("\t%d", j); 
      } else if (_arguments[i] == typeid(double)) { 
         double d = va_arg!(double)(_argptr); 
         writefln("\t%g", d); 
      } 
   } 
}
  
void main() { 
   printargs(1, 2, 3L, 4.5); 
}

D Auto Functions

d

import std.stdio;

auto add(int first, double second) { 
   double result = first + second; 
   return result; 
} 

void main() { 
   int a = 1; 
   double b = 2.5; 
   
   writeln("add(a,b) = ", add(a, b)); 
}

D Ref Functions

d

import std.stdio;

ref int greater(ref int first, ref int second) { 
   return (first > second) ? first : second; 
} 
 
void main() {
   int a = 1; 
   int b = 2;  
   
   greater(a, b) += 10;   
   writefln("a: %s, b: %s", a, b);   
}

D Pure Functions

d

import std.stdio; 

int x = 10; 
immutable int y = 30; 
const int* p;  

pure int purefunc(int i,const char* q,immutable int* s) { 
   //writeln("Simple print"); //cannot call impure function 'writeln'
   
   debug writeln("in foo()"); // ok, impure code allowed in debug statement 
   // x = i;  // error, modifying global state 
   // i = x;  // error, reading mutable global state 
   // i = *p; // error, reading const global state
   i = y;     // ok, reading immutable global state 
   auto myvar = new int;     // Can use the new expression: 
   return i; 
}

void main() { 
   writeln("Value returned from pure function : ",purefunc(x,null,null)); 
}

D Nested Switch Statement

d

import std.stdio;

int main () { 
   /* local variable definition */ 
   int a = 100; 
   int b = 200;  
   
   switch(a) {
      case 100: 
         writefln("This is part of outer switch", a ); 
         switch(b) { 
            case 200: 
               writefln("This is part of inner switch", a ); 
            default: 
               break; 
         } 
      default: 
      break; 
   } 
   writefln("Exact value of a is : %d", a ); 
   writefln("Exact value of b is : %d", b ); 
  
   return 0; 
}

D Switch Statement

d

import std.stdio;
 
int main () { 
   /* local variable definition */ 
   char grade = 'B';
   switch(grade) { 
      case 'A' : 
         writefln("Excellent!" ); 
         break; 
      case 'B' : 
      case 'C' : 
         writefln("Well done" ); 
         break; 
      case 'D' : 
         writefln("You passed" ); 
         break; 
      case 'F' : 
         writefln("Better try again" ); 
         break; 
      default : 
         writefln("Invalid grade" ); 
   } 
   writefln("Your grade is %c", grade ); 
  
   return 0; 
} 

D Nested If Statement

d

import std.stdio;
 
int main () { 
   /* local variable definition */ 
   int a = 100; 
   int b = 200; 
  
   /* check the boolean condition */ 
   if( a == 100 ) { 
      /* if condition is true then check the following */ 
      if( b == 200 ) { 
         /* if condition is true then print the following */ 
         writefln("Value of a is 100 and b is 200" ); 
      } 
   } 
   writefln("Exact value of a is : %d", a ); 
   writefln("Exact value of b is : %d", b ); 
  
   return 0; 
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.