Thread overview
Functional oriented programming in D
Jul 16, 2015
ponce
Jul 16, 2015
Meta
July 16, 2015
Hi

After a couple of years using and keeping an eye on D I have grown into a functional oriented programmer. I began with JavaScripts semi pseudo functional style, then Clojures dynamic functional style, Haskells burritos and now Scala.

I have began to dislike Haskell for the academic approach to programming. To do anything you must think and think again. The knowledge you need to code haskell is more than the syntax and rules.

Then i thought about D some days ago. Thought Id give it a new try. Take a new look. See if something had changed from my side or the language. Well certainly from my side given the fact that I am no longer an OO programmer. Well i do OO stuff everyday, but despise it and remind myself and other devs how this could have be done better.

I found that there is std.algorithm, std.functional and lambda expressions in D. Thats very interesting. It gives a promise that I can use those tools to create (almost) class less programs. I initiated a vibe.d project and tried to change the listen handler to a lamdba expression:

listenHTTP(settings, (req, res) => res.writeBody("Hello, World!"));

And it worked obviously well.

But when I changed it to multiline lambda:

listenHTTP(settings, (req, res) => {
	res.writeBody("Hello, World!");
});

It gave me an error:

[jarl@galifrey vibed-app] $ dub
Target vibe-d 0.7.23 is up to date. Use --force to rebuild.
Building vibed-app ~master configuration "application", build type debug.
Compiling using dmd...
source/app.d(8,12): Error: None of the overloads of 'listenHTTP' are callable using argument types (HTTPServerSettings, void), candidates are:
../../.dub/packages/vibe-d-0.7.23/source/vibe/http/server.d(71,6):        vibe.http.server.listenHTTP(HTTPServerSettings settings, void delegate(HTTPServerRequest req, HTTPServerResponse res) request_handler)
../../.dub/packages/vibe-d-0.7.23/source/vibe/http/server.d(95,6):        vibe.http.server.listenHTTP(HTTPServerSettings settings, void function(HTTPServerRequest req, HTTPServerResponse res) request_handler)
../../.dub/packages/vibe-d-0.7.23/source/vibe/http/server.d(100,6):        vibe.http.server.listenHTTP(HTTPServerSettings settings, HTTPServerRequestHandler request_handler)
FAIL .dub/build/application-debug-linux.posix-x86_64-dmd_2067-006B89616986857CBBFCCB1EAEA3E85E/ vibed-app executable
Error executing command run:
dmd failed with exit code 1.

Which I belive means that a single line lambda is a different object type than multiline lambda?

Why?
July 16, 2015
On Thursday, 16 July 2015 at 09:49:03 UTC, Jarl André Hübenthal wrote:
> Why?

The syntax for delegate literals with braces is

> listenHTTP(settings, (req, res) {
> 	res.writeBody("Hello, World!");
> });


July 16, 2015
On Thursday, 16 July 2015 at 09:52:59 UTC, ponce wrote:
> On Thursday, 16 July 2015 at 09:49:03 UTC, Jarl André Hübenthal wrote:
>> Why?
>
> The syntax for delegate literals with braces is
>
>> listenHTTP(settings, (req, res) {
>> 	res.writeBody("Hello, World!");
>> });

Thanks. Those small details you forget to test before asking is a bit embarrassing. I blame Scala for it :) In Scala its (req,res) => { ... } consistently.
July 16, 2015
On Thursday, 16 July 2015 at 09:57:55 UTC, Jarl André Hübenthal wrote:
> On Thursday, 16 July 2015 at 09:52:59 UTC, ponce wrote:
>> On Thursday, 16 July 2015 at 09:49:03 UTC, Jarl André Hübenthal wrote:
>>> Why?
>>
>> The syntax for delegate literals with braces is
>>
>>> listenHTTP(settings, (req, res) {
>>> 	res.writeBody("Hello, World!");
>>> });
>
> Thanks. Those small details you forget to test before asking is a bit embarrassing. I blame Scala for it :) In Scala its (req,res) => { ... } consistently.

The difference in D is that (req, res) => { ... } is still valid syntax, but it doesn't do what you'd expect. { /* some code */ } is a valid delegate literal in D, specifying a delegate literal that takes no arguments. Thus, (req, res) => { /* some code */ } actually creates a delegate literal than returns *another* delegate literal.