This Week in D January 18, 2015

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.

It hasn't been a full week since the last edition because it was late. From here on, we'll be shooting for a weekend write-up each week.

This Week in D is in the process of being hosted at dlang.org. Starting soon, you'll be able to find this publication there.

Events happening in the coming week

Walter Bright will be presenting at NWCPP on Jan 21 at Microsoft!

D: What's Cooking in 2015 by Andrei Alexandrescu on Thursday, January 22

Berlin Meetup: Friday the 23rd of January from 17:00 to 19:30 at the Melbourne Cantee

Statistics

Major Changes

In the community

DConf 2015

Chuck Allison to deliver keynote

Call for Submissions is still open!

DConf 2015 discounted hotel rooms now available

Thanks to p0nce for a nicer DConf logo!

Hope to see you there!

Community announcements

See more at digitalmars.D.announce.

Significant Forum Discussions

Since the last issue of This Week in D, the forum has been dominated by discussions on website and associated process improvements.

However, that wasn't everything - there were also discussions on new DIPs (D Improvement Proposals), targeting game consoles, and other items.

See more at forum.dlang.org and keep up with community blogs at Planet D.

From the past

In March 2014, Cybershadow wrote a post about Functional Image Processing in D. This post demonstrates how to use D's templates and other features to make efficient and elegant code for working with images of various color layouts.

FAQs

I wrote about HTML parsing on Stack Overflow again this week, demonstrating how to use my dom.d library to do web scraping.

The D Idioms List got new entries:

Tip of the week

This week, we'll look at UFCS, Uniform Function Call Syntax to write extension methods to types and help improve encapsulation.

D, when encountering obj.func(args...), will first attempt a member lookup. Member lookup includes direct methods, inherited methods, alias this members, and opDispatch, and opDot rewriting. If none of those succeed, the compiler will attempt to rewrite the expression into func(obj, args...) and compile that.

UFCS does not look at nested functions:
	void foo() {
		void bar(Object obj) { }
		obj.bar(); // won't work because bar is a local symbol
	}
To use a function with UFCS, it must be defined at module scope:
	void bar(Object obj) {}
	void foo() {
		obj.bar(); // will work
	}

If the rewrite succeeds unambiguously, the code will compile. Any type may be extended in this fashion:

	float divideBy(float divisor, float dividend) {
		return divisor / dividend;
	}

	void main() {
		float a = 5.divideBy(2); // works on built-in types too!
		// rewrites into a = divideBy(5, 2);
		assert(a == 2.5);
	}

UFCS is used in D for three main reasons:

private in D means something slightly different than private in C++: in D, protection works at module (source file) boundaries. All functions inside a single module can see all other symbols in that module, regardless of protection level, even if they are private members from different classes. If you want a member to only be visible to a particular class in D, put that class in its own file / module.
D also supports the package protection level, which allows sharing of symbols in the same package - sharing the same name up until the last dot - but not outside.
	module foo.bar;
	package int a;

	module foo.baz;
	import foo.bar;
	// int a IS accessible

	module something.else;
	import foo.bar;
	// int a is not accessible

Find more D tips at the D idioms list or buy my D Cookbook for a more in-depth examination of many D tricks.

If you'd like to submit a tip, email me.

Upcoming events

Learn more about D

To learn more about D and what's happening in D: