Difference between revisions of "Getting Started"

From D Wiki
Jump to: navigation, search
(Added a 'Your first program' link to Ali Cehreli's book's Hello World section; which IMO provides a better 'Getting Started' for a beginner than the tips on this page. (perhaps it could be further integrated here?))
m (Using Coedit)
(12 intermediate revisions by 8 users not shown)
Line 3: Line 3:
 
== Your first program ==
 
== Your first program ==
  
For a beginner-friendly, step-by-step introduction on how to build your first D program see the [http://ddili.org/ders/d.en/hello_world.html hello world] section of Ali Çehreli online book [http://ddili.org/ders/d.en/index.html Programming in D].
+
For a beginner-friendly, step-by-step introduction on how to build your first D program see the [http://ddili.org/ders/d.en/hello_world.html hello world] section of Ali Çehreli's online book [http://ddili.org/ders/d.en/index.html Programming in D].
  
 
== Choosing a compiler ==
 
== Choosing a compiler ==
As you probably already know, D is a [http://en.wikipedia.org/wiki/Compiled_language compiled language], so you have to make your first choice - a compiler.
+
As you probably already know, D is a [http://en.wikipedia.org/wiki/Compiled_language compiled language], so you have to make your first choice: a compiler. There are [[Compilers|several compilers to choose from]].
In fact, you have from what to choose - there are couple of [[Compilers|them]].
 
  
Every compiler has its own strengths and weaknesses as they differ in:
+
They differ in:
 
* Installation procedure
 
* Installation procedure
 
* Ease of building from source
 
* Ease of building from source
Line 17: Line 16:
 
* Popularity
 
* Popularity
  
Whichever you will choose, you shouldn't have bigger problems with changing it. Options might differ, but most code should be compatible with all of them (if you see any problem, just contact with maintainer).
+
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 ==
 
== Running D code like a script ==
After you have your compiler installed, you'll probably want to do some coding.
+
After you have your compiler installed you'll want to do some coding.
  
For small projects (and your first experiments with language won't be probably too big) it might be useful to merge compilation, and running your code into one phase.
+
For small projects it's handy to compile and run in a single step. A few solutions exist.
  
Most (if not all) compiler packages, contain a tool that's named rdmd/gdmd/ldmd or similar. For instructional purposes we'll call it rdmd.
+
=== Using RDMD ===
  
Just fill your file (i.e. main.d) with a sample application:
+
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:
 
<syntaxhighlight lang="D">
 
<syntaxhighlight lang="D">
 
import std.stdio;
 
import std.stdio;
Line 34: Line 35:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
and type in your commandline
+
and run the command line
 
<pre>rdmd main.d</pre>
 
<pre>rdmd main.d</pre>
  
If you properly installed your compiler, you should see 'Hello, world!' on the terminal.
+
If you properly installed the compiler you should see 'Hello, world without explicit compilations!' on the terminal.
 
Isn't that simple?
 
Isn't that simple?
 +
 +
On Unix/Linux systems, you can even use the traditional #! for scripting and set your main.d file to be executable:
 +
<pre> chmod +x main.d</pre>
 +
<syntaxhighlight lang="D">
 +
#!/usr/bin/env rdmd
 +
import std.stdio;
 +
void main()
 +
{
 +
    writeln("Hello, world without explicit compilations!");
 +
}
 +
</syntaxhighlight>
 +
and run the command line just:
 +
<pre>./main.d</pre>
  
 
For more information about this tool you might look at http://dlang.org/rdmd.html or your compiler documentation.
 
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' in your source code properly. The only limiting factor is compilation time, which should be fast in D.
+
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 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;
 +
    import std.experimental.color.hsx;
 +
 
 +
    auto yellow = RGBf32(1.0, 1.0, 0.0);
 +
    writefln("Yellow in HSV: %s", cast(HSV!())yellow);
 +
}
 +
</syntaxhighlight>
 +
 
 +
can be executed with
 +
<pre>dub a.d</pre>
 +
 
 +
=== Using Coedit ===
  
When your programs get larger, you might consider using a [[build systems|build system]]
+
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 [http://bbasile.github.io/Coedit/features_runnables official documentation].
  
 
== Choose your code editor ==
 
== Choose your code editor ==
Whether you're an IDE guy, or you like minimalistic text-editor approach, with D you have something to choose.
+
See [[IDEs]] and [[Editors|Text Editors]] to learn about D editing tools.
Please, take a look on [[IDEs]] or [[Editors|Text Editors]] sections to learn something about it.
 
  
== Using D language specific in your field ==
+
== Using the D language for your domain ==
As you probably didn't come here out of nowhere, you have specific needs. Maybe you want to develop a game, web application, desktop application, or you want to use D to drive your robot.
+
Do you want to develop a game, web application, desktop application, or use D to drive your robot?
You can find more specialized information about different disciplines at [[Development_With_D|Development with D]] section.
+
You can find more specialized information for different disciplines at [[Development_With_D|Development with D]].
  
 
== Troubleshooting ==
 
== Troubleshooting ==
Every complex software system might fail in its task. The same rule applies to DMD toolchain and maybe your application. It's important to identify and resolve problems as fast as it's possible.
+
Every complex software system might fail in its task. The same rule applies to DMD toolchain and maybe your application.
  
If you're in the unfortunate position of having some kind of compilation/runtime problems and you have no idea where is it come from, you don't have to worry, because there are some tools that will help you bear with that.
+
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 error messages aren't enough, take a look at [[Code Troubleshooting]] that might save you some time.
+
If your program doesn't compile and the error messages aren't enough, see [[Code Troubleshooting]].
  
On the other hand, if your program compiled, but fails from some unexpected reason, you can take a look on [[Debuggers]] section.
+
If your program compiled but doesn't do what you want, see [[Debuggers]].
  
If nothing helps, don't give up! [[Main Page#Community|D Community]] is very helpful and can ease your pain.
+
Beyond that, [[Main Page#Community|the D Community]] is very helpful.
  
And remember, if your problem has something to do with D toolchain or any existing D project - don't forget save some time for others by [http://d.puremagic.com/issues/ reporting it]!
+
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 [http://d.puremagic.com/issues/ reporting it]!
  
 
== Searching with search engines==
 
== Searching with search engines==
  
Googling for "D" is useless in looking for D related web sites. But Googling for "D programming" or "D programming language" works rather well.
+
===General considerations===
 +
Googling for "D" is useless in looking for D related web sites. But Googling for [[https://www.google.com/search?q=d%20programming&rct=j D programming]] or [[https://www.google.com/search?q=d%20language&rct=j D language]] works well. Using '''dlang''' in combination with a search helps D in the TIOBE[http://www.tiobe.com/index.php/content/paperinfo/tpci/programminglanguages_definition.html] 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:
 +
 
 +
  <code>indexOf site:dlang.org</code>
 +
 
 +
Or add ''"dlang.org"'' to your query:
 +
 
 +
  <code>indexOf "dlang.org"</code>
 +
 
 +
* Yahoo search: add ''domain:dlang.org'' to your search string. You'll get results from the specified domain:
 +
 
 +
  <code>indexOf domain:dlang.org</code>
 +
 
  
So, for everyone who maintains 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 both with your page rank and general findability for those interested in D.
+
[[Category:Tutorials]]

Revision as of 14:51, 31 March 2017

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?

On Unix/Linux systems, you can even use the traditional #! for scripting and set your main.d file to be executable:

 chmod +x main.d
#!/usr/bin/env rdmd
import std.stdio;
void main()
{
    writeln("Hello, world without explicit compilations!");
}

and run the command line just:

./main.d

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;
    import std.experimental.color.hsx;

    auto yellow = RGBf32(1.0, 1.0, 0.0);
    writefln("Yellow in HSV: %s", cast(HSV!())yellow);
}

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