DEV Community

Jesse Phillips
Jesse Phillips

Posted on

Printing in D

Another excellent article on Python has me questioning why.

print("Hello Word”)

writeln("Hello Word");
Enter fullscreen mode Exit fullscreen mode

This is where the similarities end. writeln is not a substitute for print as we see next.

print("my", "name", "is", "Austin")
# my name is Austin

writeln("my", "name", "is", "Jesse");
// mynameisJesse
Enter fullscreen mode Exit fullscreen mode

While multiple arguments can be passed in, no default or explicit separator exists. Instead we can wrap our call in an array to get access to some array ops.

writeln(["my" , "name" , "is" , "Jesse"].join(" "));
// my name is Jesse
Enter fullscreen mode Exit fullscreen mode

print(“my”, “name”, “is”, “Austin”, sep=“,”, end=“!”)
my,name,is,Austin!

write(["my" , "name" , "is" , "Jesse"].join(","), "!");
// my,name,is,Jesse!
Enter fullscreen mode Exit fullscreen mode

In order to replicate the Python behavior, write was chosen because ln stands for line which is placed at the end. The exclamation mark is then included as the last argument.

with open(“text.txt”, “w”) as text:
print(“my”, “name”, “is”, “Austin”, sep=“,”, end=“!”, file=text)

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

Or

append("text.txt", ["my" , "name" , "is" , "Jesse"].join(",") ~ "!");
Enter fullscreen mode Exit fullscreen mode

This uses a tilde (concatenation operator) because append does not take additional parameters like write.

https://run.dlang.io/

Conclusion

Python's choice to place a space between prints arguments likely lead to the choice of giving control of that separator. While the code to replicate Python's behavior is only minor, by making the default behavior meaningful to those wanting something readable printed to the screen Python required less familiarity of other language constructs.

If you instructed a new programmer "just wrap all your arguments in these brackets and call this join like so" as done in D, that new programmer would look at you and say "it's a computer why doesn't it do what I want instead of jumping through hoops.

Where I look at Python and see learning isolated tools. D's approach (also available in Python) is universally applicable. It can do a console and file, and other strings, networking, in some cases datatype.

Python tries to do the right thing but I think that opens the door for confusion because being explicit is not the norm.

Top comments (3)

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!