Difference between revisions of "Getting Started"
(→Troubleshooting) |
WebFreak001 (talk | contribs) (Add "going further" section linking to Tutorials and Books) |
||
(21 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
So, when you have [[Why_switch_to_D|enough reasons]] to try D, let's get your hands dirty. | So, when you have [[Why_switch_to_D|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 [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 | + | 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]]. |
− | |||
− | + | They differ in: | |
* Installation procedure | * Installation procedure | ||
* Ease of building from source | * Ease of building from source | ||
Line 13: | Line 16: | ||
* Popularity | * Popularity | ||
− | Whichever you will choose, you shouldn't have | + | 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 | + | 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 | + | Just create your source file, e.g. main.d: |
<syntaxhighlight lang="D"> | <syntaxhighlight lang="D"> | ||
import std.stdio; | import std.stdio; | ||
Line 30: | Line 35: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | and | + | and run the command line |
<pre>rdmd main.d</pre> | <pre>rdmd main.d</pre> | ||
− | If you properly installed | + | 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 | + | 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> | ||
== Choose your code editor == | == Choose your code editor == | ||
− | + | See [[IDEs]] and [[Editors|Text Editors]] to learn about D editing tools. | |
− | |||
− | |||
− | == Using D language | + | == 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 | + | You can find more specialized information for different disciplines at [[Development_With_D|Development with D]]. |
+ | == Going further == | ||
+ | For tutorials and information about more advanced topics, refer to [[Tutorials]] and [[Books]]. | ||
== Troubleshooting == | == Troubleshooting == | ||
− | Every complex software system might fail in its task. The same rule applies to DMD toolchain and maybe your application | + | Every complex software system might fail in its task. The same rule applies to DMD toolchain and maybe your application. |
− | If you're | + | 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 | + | 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, [[Main Page#Community|the D Community]] is very helpful. | |
− | And remember | + | 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 | + | ===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> | ||
+ | |||
− | + | [[Category:Tutorials]] |
Latest revision as of 12:58, 16 July 2020
So, when you have enough reasons to try D, let's get your hands dirty.
Contents
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
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.
Going further
For tutorials and information about more advanced topics, refer to Tutorials and Books.
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