DEV Community

Printing in D

Jesse Phillips on December 15, 2019

Another excellent article on Python has me questioning why. Python’s Print() Does What!?!? Alamar ・ D...
Collapse
 
erebos-manannan profile image
Erebos Manannán

Your examples make D look like a pretty unreadable language.

with(File("text.txt", "w"))
    write(["my" , "name" , "is" , "Jesse"].join(","), "!");
Enter fullscreen mode Exit fullscreen mode

There's no clear indication that you're working on the text.txt inside the with, and write just magically changes target.

Collapse
 
jessekphillips profile image
Jesse Phillips

Yeah, D programmers rearly utilize with even with limited scope it can hide the origin. The main thing here is to create a scope to automatically call close() like in Python.

{
auto f = File("text.txt", "w");
f.write(["my" , "name" , "is" , "Jesse"].join(","), "!");
} 
Enter fullscreen mode Exit fullscreen mode

But D also provides an explicit cleanup since, not all cleanup is the same, and not everything is expected to clean up at scope exit

{
auto f = File("text.txt", "w");
scope(exit) f.close();
f.write(["my" , "name" , "is" , "Jesse"].join(","), "!");
} 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alamarw profile image
Alamar

Nice post! I haven’t really seen much of D before this post, and I have to say it looks surprisingly readable!