Printing in D

jessekphillips

Jesse Phillips

Posted on December 15, 2019

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.

💖 💪 🙅 🚩
jessekphillips
Jesse Phillips

Posted on December 15, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Memory Free Programming in D
dlang Memory Free Programming in D

February 11, 2020

Regular Expressions in D
regex Regular Expressions in D

December 30, 2019

Printing in D
dlang Printing in D

December 15, 2019

List Comprehension in D
dlang List Comprehension in D

December 13, 2019

Compile-Time Reflection
dlang Compile-Time Reflection

December 9, 2019