D D Language kasa study 이민우. What is D? Why D? Major Design Goals of D Features To Keep...
-
Upload
jonathan-foster -
Category
Documents
-
view
212 -
download
0
Transcript of D D Language kasa study 이민우. What is D? Why D? Major Design Goals of D Features To Keep...
D Language
kasa study이민우
What is D?
Why D?
Major Design Goals of D
Features To Keep
Features To Drap
Who D is For
Major Features of D
Content
Visual DD with Game Programming
What is D?
- purpose systems and application.- high level language, but interface directly os api and hardware.- job done quickly, reliably, and leave behind maintainable, easy to understand code.- draws inspiration from those other languages and tempers it with experience and real world practicality.
Walter Bright Andrei Alexandrescu
C++JAVA C#
What is D?
C++JAVA C#
What is D?
Why D?- software industry has come a long way since the c language was invented.
- many new concepts were added -> c++.
- compatibility with c was maintained. -> weaknesses of the original design.
- new features must be carefully fitted into the exisiting structure without requiring rewriting old code. -> very complicated.
Why D?- c standard : 500 pages- c++ standard : 750 pages- c++ implements things like resizable arrays and string concatenation as part of the standard library.
In c++ - const char abc[5] = "world";string str = "hello" + abc;
In d -const char[5] abc = "world";char[] str = "hello" ~ abc;
D string vs C++ string
c : 500 page
c++ : 750 page
Can the power and capability of c++ be extracted,Redesigned, and recast into a language that is Simple,Orthogonal, and practical?
Why D?
Major Design Goals of D
- memory safe programming.- multi paradigm programming. . imperative . object oriented . generic - short learning time for programmers.- provide low level bare metal access as required.- context free grammar.- incorporate contract programmig and unit testing methodology
Features To Keep
- look and feel of c/c++- c style function-and data or c++ style object-oriented, template metaprogramming, or mix.- compile/link/debug.- operator overloading.- Runtime Type Identification.- maintains function link compatibility with the C calling conventions.- Template Metaprogramming.- RAII- Down and dirty programming
Features To Drop
- c source code compatibility.- link compatibility with c++.- c perprocessor.- multiple inheritance.- name space.- forward declarations.- include files- support for 16 bit computers.- mutual dependance of compiler passes.- compiler complexity.- template overloading of < and > symbols.
Who D is For
- eliminate bugs before the code is even compiled.- people who compile with maximum warring levels.- enjoy c++, but are frustrated by the need to expend much effort explicitly managing memory and finding pointer bugs. - should provide enough features to obviate the continual necessity to manipulate pointers directly.- Numerical programmers.- half their application in scripting languages like Ruby and Python, and the other half in c++ to speed up the bottlenecks.
Major Features of D
Object Oriented Prograpping
Functional
Productivity
Resource Management
Performance
Reliability
Project Management
Array
Object Oriented Programming - classclass A{ this () { foo (); }
class B : A{ this () { super (); }
int main ( char [] [] args ) { A a = new A (); // writes "foo from A" B b = new B (); // writes "foo from B" return 1;}
Productiviy - modulesIn c++#paragma …#ifndef…#endif
In Dimport Lookup; import std.stdio; import std.string;
Productiviy – declaration vs definitionD :class ABC{ int func() { return 7; } static int z = 7;}int q;
C++ :int ABC::func() { return 7; } int ABC::z = 7; extern int q;
C++ :class Foo{ int foo(Bar *c){ return c->bar(); }};
class Bar{ public: int bar() { return 3; }};
D :class Foo{ int foo(Bar c) { re-turn c.bar; }}
class Bar{ int bar() { return 3; }}
Productiviy – templatetemplate factorial(T){ T factorial(T n) { if (n <= 1) return cast(T)1; else return cast(T)( n * factorial(n-1)); }}
int main ( char [] [] args ) { for(int i=0; i<8; i++) writefln("%s! = %s", i, facto-rial(i)); return 1;}
Productiviy – associative arraychar[][char[]] aA;printf("Fill in the array.\n");aA["a"] = "Apple";aA["b"] = "Book";aA["c"] = "Car";
C++ implements asociative array as part ofStandard library, not as part of the core language.
Function – nested functionIn Dint addSquares(int a, int b){ int squareIt() { return (a * a) + (b * b); } return squareIt();}
In c++void outer(){ static int v1 = 5; int v2 = 5; struct thru { static void inner() { std::cout << v1 << std::endl; //std::cout<<v2<<std::endl; } }; thru::inner();}
ArrayC arrays have serveral faults :- when an array is passed to function, all array type information gets lost.- c arrays connot be resized.- c array cannot be bounds checked.- arrays are declared with the [] after the identifier. -> int (*array)[3];- in D. the [] for the array go on the left. -> int[3]* array; long[] func(int x);
Resource Management- D memory allocation is fully garbage colected. -> language gets much simpler.- eliminates the tedious, error prone memory alloca-tion tracking code -> much faster development time, lower mainte-nance costs.- garbage collectors can be used with c++. -> impeding the effectiveness of it. -> much of the runtime library code can’t be used with collectors.- D support RAII in a controlled, predictable manager that is independent of the garbage collection cycle.
Performance – inline assemblervoid main(){ int len; uint zCode; … asm { cld; mov ECX,len; mov EAX,zCode; mov EDI,pss; repne; scasb; mov EBX,len; sub EBX,ECX; dec EBX; mov foundOffset,EBX; }}
Relibility – contract programming-DbC(Design by Contract)
double sqrt( double x)// Preconditionin { assert( x >= 0); }// Postconditionout(result) { assert( result >= 0 && approxEqual(result * result, x) ); }body { // sqrt implementation double y; . . . return y; }
Relibility – unit test- unit tests can be addded to a class, such they are automatically run upon program startup.
void main() {}
class Foo { int x, y; }
unittest{ Foo foo = new Foo; foo.x = 2; foo.y = 4; assert(foo.x != foo.y, "this assert passes"); assert(foo.x < foo.y);}
Relibility- synchronization ( D provides primitives to build multithreaded program ) -> synchronized int func(){…}- support for robust techniques -> dynamic arrays instead of pointers. -> reference variables instead of pointers. -> reference objects instead of pointers. -> garbage collection instead of explicit memory management. -> built in primitives for thread synchronization. -> no macros to inadvertently slam code. -> inline functions instead of macros. -> vastly reduced need for pointers. -> no more uncertainty about the singed-ness of chars. -> no need to duplicate declarations in source and header files.
Relibility - check- compile time check -> stronger type checking. -> no empty ; for loop bodies.- runtime checking -> assert() expressions. -> array bounds checking. -> undefined case in switch exception. -> out of memory exception. -> in, out, and class invariant Contract Program-ming support.
Project Management - versioningversion(XX){ // code here gets compiled if version XX is active}
version(2){ // code here only gets compiled if the version has been // set to 2 or higher.}
version(none) { // this code is now commented out }
Visual D
Game Programming?
YearFirst Prize
Second Prize
Third Prize
Light-ning
1998 Cilk OCaml
1999 OCaml Haskell [2]
2000 OCaml OCaml
2001 Haskell Dylan
2002 OCaml C [2]
2003 C++ C++ OCaml
2004 HaskellHaskell and C++
Java and C++
2005 Haskell Dylan Haskell
2006 2D[3] D Assembly
2007 C++ Perl [4]
2008 Java ML[5]
2009 C++ Java ML[6]
ICFP Programming Contest