D Language

Diciptakan di tahun 1999, dan belum juga bergaung sampai tahun 2003: D Language. Mirip C++, dan memang merupakan satu alternatif lagi untuk memperbaiki C dan C++, dalam arti memberikan kekuatan baru atas C, dan memberikan kesederhanaan buat C++. Tapi sengaja nggak dibikin 100% portabel dengan dua bahasa itu. Penciptanya belajar bahwa C++ jadi rumit justru karena pernah mau 100% portabel dengan C, dulunya. Udah rumit, akhirnya nggak portabel juga dengan C99.

Anyway, kerumitan C++ justru mengasyikkan, terutama kalau kita lagi punya waktu. Kalau ada pihak2 yang bener2 merasa C++ itu provoking dan frustrating, feel happy-lah bahwa masih ada alternatif bahasa serupa C++ yang nggak harus lekat dengan VM (kayak C# dan Java) , nggak berasa beginner (kayak Basic dan Java), bukan scripting (kayak Perl atau Ruby), masih punya template (tapi lebih simpel daripada C++).

Gini kalimat pembuka di websitenya: It seems to me that most of the “new” programming languages fall into one of two categories: Those from academia with radical new paradigms and those from large corporations with a focus on RAD and the web. Maybe its time for a new language born out of practical experience implementing compilers.

Contoh program D:

 import std.stdio;

void main(char[][] args)
{
writefln("Hello World, Reloaded");

// auto type inference and built-in foreach
foreach (argc, argv; args)
{
// Object Oriented Programming
CmdLin cl = new CmdLin(argc, argv);
// Improved typesafe printf
writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv);
// Automatic or explicit memory management
delete cl;
}

// Nested structs and classes
struct specs
{
// all members automatically initialized
int count, allocated;
}

// Nested functions can refer to outer
// variables like args
specs argspecs()
{
specs* s = new specs;
// no need for '->'
s.count = args.length;		   // get length of array with .length
s.allocated = typeof(args).sizeof; // built-in native type properties
foreach (argv; args)
s.allocated += argv.length * typeof(argv[0]).sizeof;
return *s;
}

// built-in string and common string operations
writefln("argc = %d, " ~ "allocated = %d",
argspecs().count, argspecs().allocated);
}

class CmdLin
{
private int _argc;
private char[] _argv;

public:
this(int argc, char[] argv)	// constructor
{
_argc = argc;
_argv = argv;
}

int argnum()
{
return _argc + 1;
}

char[] argv()
{
return _argv;
}

char[] suffix()
{
char[] suffix = "th";
switch (_argc)
{
case 0:
suffix = "st";
break;
case 1:
suffix = "nd";
break;
case 2:
suffix = "rd";
break;
default:
break;
}
return suffix;
}
}

“Great, just what I need.. another D in programming.” — Segfault

%d bloggers like this: