Difference between revisions of "Getting Started"

From D Wiki
Jump to: navigation, search
(Running D code like a script)
(Running D code like a script)
Line 21: Line 21:
 
After you have your compiler installed you'll want to do some coding.
 
After you have your compiler installed you'll want to do some coding.
  
For small projects it's handy to compile and run in a single step. The rdmd tool, distributed with dmd or available separately here: https://github.com/D-Programming-Language/tools/blob/master/rdmd.d makes this simple.
+
For small projects it's handy to compile and run in a single step. A few solutions exist.
 +
 
 +
=== Using RDMD ===
 +
 
 +
The rdmd tool, distributed with dmd or available separately here: https://github.com/D-Programming-Language/tools/blob/master/rdmd.d makes this simple.
  
 
Just create your source file, e.g. main.d:
 
Just create your source file, e.g. main.d:
Line 42: Line 46:
  
 
When your programs get larger, consider using a [[build systems|build system]].
 
When your programs get larger, consider using a [[build systems|build system]].
 +
 +
=== Using DUB ===
 +
 +
Since version 1.0, DUB supports single file packages. The DUB properties stand in a DDOC comment located after the script line and before the ''moduleDeclaration''.
 +
For example the file ''a.d''
 +
 +
<syntaxhighlight lang="D">
 +
#!/usr/bin/env dub
 +
/+ dub.sdl:
 +
    name "colortest"
 +
    dependency "color" version="~>0.0.3"
 +
+/
 +
 +
void main()
 +
{
 +
    import std.stdio : writefln;
 +
    import std.experimental.color.conv;
 +
    import std.experimental.color.hsx;
 +
    import std.experimental.color.rgb;
 +
 +
    auto yellow = RGB!("rgb", float)(1.0, 1.0, 0.0);
 +
    writefln("Yellow in HSV: %s", yellow.convertColor!(HSV!()));
 +
}
 +
</syntaxhighlight>
 +
 +
can be executed with
 +
<pre>dub a.d</pre>
 +
 +
=== Using Coedit ===
 +
 +
The [https://www.github.com/BBasile/Coedit D IDE Coedit] proposes a system of runnable modules. A runnable module is a simple D source that can be compiled and executed in one step. Compilers options can be specified in the script line (which is not used since the file is handled by the IDE). Read more in the [https://github.com/BBasile/Coedit/wiki#runnable-module official documentation].
  
 
== Choose your code editor ==
 
== Choose your code editor ==

Revision as of 22:53, 11 August 2016

So, when you have enough reasons to try D, let's get your hands dirty.

Your first program

For a beginner-friendly, step-by-step introduction on how to build your first D program see the hello world section of Ali Çehreli's online book Programming in D.

Choosing a compiler

As you probably already know, D is a compiled language, so you have to make your first choice: a compiler. There are several compilers to choose from.

They differ in:

  • Installation procedure
  • Ease of building from source
  • License
  • Performance
  • Reliability
  • Popularity

Whichever you will choose, you shouldn't have problems changing it. Options might differ but most code should be compatible with all of them. (If you find problems, please file an issue.)

Running D code like a script

After you have your compiler installed you'll want to do some coding.

For small projects it's handy to compile and run in a single step. A few solutions exist.

Using RDMD

The rdmd tool, distributed with dmd or available separately here: https://github.com/D-Programming-Language/tools/blob/master/rdmd.d makes this simple.

Just create your source file, e.g. main.d:

import std.stdio;
void main()
{
    writeln("Hello, world without explicit compilations!");
}

and run the command line

rdmd main.d

If you properly installed the compiler you should see 'Hello, world without explicit compilations!' on the terminal. Isn't that simple?

For more information about this tool you might look at http://dlang.org/rdmd.html or your compiler documentation.

You can use exactly the same command for building programs that are made of separate modules. Just 'import' the other modules.

When your programs get larger, consider using a build system.

Using DUB

Since version 1.0, DUB supports single file packages. The DUB properties stand in a DDOC comment located after the script line and before the moduleDeclaration. For example the file a.d

#!/usr/bin/env dub
/+ dub.sdl:
    name "colortest"
    dependency "color" version="~>0.0.3"
+/

void main()
{
    import std.stdio : writefln;
    import std.experimental.color.conv;
    import std.experimental.color.hsx;
    import std.experimental.color.rgb;

    auto yellow = RGB!("rgb", float)(1.0, 1.0, 0.0);
    writefln("Yellow in HSV: %s", yellow.convertColor!(HSV!()));
}

can be executed with

dub a.d

Using Coedit

The D IDE Coedit proposes a system of runnable modules. A runnable module is a simple D source that can be compiled and executed in one step. Compilers options can be specified in the script line (which is not used since the file is handled by the IDE). Read more in the official documentation.

Choose your code editor

See IDEs and Text Editors to learn about D editing tools.

Using the D language for your domain

Do you want to develop a game, web application, desktop application, or use D to drive your robot? You can find more specialized information for different disciplines at Development with D.

Troubleshooting

Every complex software system might fail in its task. The same rule applies to DMD toolchain and maybe your application.

If you're having compilation/runtime problems and you have no idea why, don't worry. There are some tools that will help.

If your program doesn't compile and the error messages aren't enough, see Code Troubleshooting.

If your program compiled but doesn't do what you want, see Debuggers.

Beyond that, the D Community is very helpful.

And remember: If your problem has to do with the D toolchain or existing D projects, don't forget save some time for others by reporting it!

Searching with search engines

General considerations

Googling for "D" is useless in looking for D related web sites. But Googling for [D programming] or [D language] works well. Using dlang in combination with a search helps D in the TIOBE[1] index.

If you maintain a web page on D, please refer to it at least once per page as "D programming language" rather than just "D". This should help significantly with your page rank and findability for those interested in D.

Specific search engine

Let's say we want to search for the identifier indexOf using...

  • Bing, Google, or DuckDuckGo: add site:dlang.org to your query. You'll get only the results which belong to the specified site:
 indexOf site:dlang.org

Or add "dlang.org" to your query:

 indexOf "dlang.org"
  • Yahoo search: add domain:dlang.org to your search string. You'll get results from the specified domain:
 indexOf domain:dlang.org