Welcome to This Week in D! Each week, we'll summarize what's been going on in the D community and write brief advice columns to help you get the most out of the D Programming Language.
The D Programming Language is a general purpose programming language that offers modern convenience, modeling power, and native efficiency with a familiar C-style syntax.
This Week in D has an RSS feed.
This Week in D is edited by Adam D. Ruppe. Contact me with any questions, comments, or contributions.
Release D 2.070.1 came out this week and LDC 1.0.0-alpha1 has been released! Please help testing!
See more at the announce forum.
This week's tip is courtesy of Mahdi Mohammadi and is How to create and use a D package.
To create a D package, you first need to have dub utility (plus D compiler, dmd). You can download pre-compiled dub binaries from here.
Creating a D package
cd ~ mkdir dlibs cd dlibs dub init mypack #this will create a skeleton for your package: mypack cd mypack cat dub.sdl #below is contents of your package name "mypack" description "A minimal D application." copyright "Copyright © 2016, mahdix" authors "root" version "1.0.0" targetType "library" #the package will have only one source file, located inside source directory cat source/myfile.d int add(int x,int y) { return x+y; } #this will compile your package #although its not necessary but ensures that everything is allright. dub Performing "debug" build using dmd for x86_64. mypack ~master: target for configuration "library" is up to date. Target is a library. Skipping execution.
Using a D package
To use a D package, we will create an application (which is itself a D package) and define the package we need as it’s dependency. But before that, we need to inform dub where the packages are (which is inside ~/dlibs).
dub add-path ~/dlibs dub list #this will list all known packages to dub, which will include mypack too
Now lets create a basic project that uses the package:
cd ~ dub init project1 cd project1 cat dub.json #config file can be either sdl, they are equivalent { "name": "myproject", "description": "A little web service of mine.", "authors": ["Mahdi Mohammadi"], "homepage": "http://myproject.example.com", "license": "GPL-2.0", "targetType": "executable", "dependencies": { "mypack": "*" } } #as you notice, we specify "*" as version number of mypack which means: any version cat source/test.d #this is our only source file which uses add method of mypack import std.stdio; import myfile; void main() { int x = add(1,4); writefln("%d", x); } #now lets build project1 pwd ~/project1 dub Performing "debug" build using dmd for x86_64. mypack ~master: target for configuration "library" is up to date. myproject ~master: target for configuration "application" is up to date. To force a rebuild of up-to-date targets, run again with --force. Running ./myproject 5 #5 is output of the call to add function in mypack
See the author's blog for more from him.
To learn more about D and what's happening in D: