Jump to page: 1 2
Thread overview
What type of `print` formatting do you prefer?
Dec 21, 2021
rempas
Dec 21, 2021
Salih Dincer
Dec 21, 2021
rempas
Dec 21, 2021
bauss
Dec 21, 2021
rempas
Dec 21, 2021
russhy
Dec 21, 2021
russhy
Dec 22, 2021
rempas
Dec 21, 2021
user1234
Dec 21, 2021
rempas
Dec 22, 2021
Paul Backus
Dec 22, 2021
rempas
December 21, 2021

Before I make my library, I want to first ask what type of formatting you guys prefer between the two I'm going to show you.

The first one is Rust-like curly brackets as the placeholder. If you leave them empty the format is automatically chosen based one the type. Or you can specify a different format inside the curly brackets. Examples:

print("My name is {}! Nice to meet you!\n", "Maria");
print("I am {} years old! And in binary, that is: {b:b}\n", 19, 19);

In the first case, "Maria" is a string so it will get formatted automatically as a string. In the second case, 19 is a number so it will automatically get formatted as a signed number. But in the second placeholder is is specified that we want the value to be formatted as a signed number in the binary (2) base.

The second format type is C-like format using the "%" character and then one more character to specify the type. This is simple and pretty much everybody knows it so I'm not going to waste you time and make any examples.

Personally, I prefer the first way of doing things. The first time a learned about Rust (I don't use Rust tho just to be clear), I LOVED IT!!! It is so much clean and another things is that in pretty much any keyboard layout curly brackets will be closer and easier to press than the percent sign and to add on that, most people have curly brackets to automatically add the closing and so it will be blast to use!!!

Now I want to make clear that I'm mostly on this approach but I just wanted to ask in case someone can convince me that the C-like way is better. Well... I can see that one thing that is better with the C-like way is that you always see the formatting type so it may be better to some people. Also the design of the Rust-like way will be harder to implement (not that I mind) and it will need discussion. For example we are using "t: type" to specify the type, "b: base" to format as a signed number and specify the base, "f: number" to format as a float and specify the number of floating points etc.

Thoughts?

December 21, 2021

On Tuesday, 21 December 2021 at 10:07:56 UTC, rempas wrote:

>

...it will need discussion. For example we are using "t: type" to specify the type, "b: base" to format as a signed number and specify the base, "f: number" to format as a float and specify the number of floating points etc.

Thoughts?

The letter B reminded me of a boolean number at first and it may stay that way in my memory.

December 21, 2021

On Tuesday, 21 December 2021 at 10:07:56 UTC, rempas wrote:

>

Before I make my library, I want to first ask what type of formatting you guys prefer between the two I'm going to show you.

The first one is Rust-like curly brackets as the placeholder. If you leave them empty the format is automatically chosen based one the type. Or you can specify a different format inside the curly brackets. Examples:

print("My name is {}! Nice to meet you!\n", "Maria");
print("I am {} years old! And in binary, that is: {b:b}\n", 19, 19);

In the first case, "Maria" is a string so it will get formatted automatically as a string. In the second case, 19 is a number so it will automatically get formatted as a signed number. But in the second placeholder is is specified that we want the value to be formatted as a signed number in the binary (2) base.

The second format type is C-like format using the "%" character and then one more character to specify the type. This is simple and pretty much everybody knows it so I'm not going to waste you time and make any examples.

Personally, I prefer the first way of doing things. The first time a learned about Rust (I don't use Rust tho just to be clear), I LOVED IT!!! It is so much clean and another things is that in pretty much any keyboard layout curly brackets will be closer and easier to press than the percent sign and to add on that, most people have curly brackets to automatically add the closing and so it will be blast to use!!!

Now I want to make clear that I'm mostly on this approach but I just wanted to ask in case someone can convince me that the C-like way is better. Well... I can see that one thing that is better with the C-like way is that you always see the formatting type so it may be better to some people. Also the design of the Rust-like way will be harder to implement (not that I mind) and it will need discussion. For example we are using "t: type" to specify the type, "b: base" to format as a signed number and specify the base, "f: number" to format as a float and specify the number of floating points etc.

Thoughts?

Personally I prefer the C# way where it's explicit what "index" the argument comes from:

print("My name is {0}! Nice to meet you!\n", "Maria");

// C# doesn't have a binary format, but does hex
// However due to being explicit about the index we can remove the second argument for the formatting.
print("I am {0} years old! And in hex, that is: 0x{0:X}\n", 19);
December 21, 2021

On Tuesday, 21 December 2021 at 10:07:56 UTC, rempas wrote:

>

Before I make my library, I want to first ask what type of formatting you guys prefer between the two I'm going to show you.

The first one is Rust-like curly brackets as the placeholder. If you leave them empty the format is automatically chosen based one the type. Or you can specify a different format inside the curly brackets. [...]
Thoughts?

{} is like D's %s. D has chosen to follow the tradition of the printf function and that's it.

December 21, 2021

On Tuesday, 21 December 2021 at 12:19:55 UTC, user1234 wrote:

>

{} is like D's %s. D has chosen to follow the tradition of the printf function and that's it.

Yeah, I know how D's std phobos does it. But what about YOUR thoughts about my ideas of implementing formatting for my library?

December 21, 2021

On Tuesday, 21 December 2021 at 10:52:05 UTC, Salih Dincer wrote:

>

The letter B reminded me of a boolean number at first and it may stay that way in my memory.

Well, see something a lot of times and you get used to it ;)

Tho it can also be "s" for system but I think "b" for base makes more sense. At least a little bit.

December 21, 2021

On Tuesday, 21 December 2021 at 11:17:47 UTC, bauss wrote:

>

Personally I prefer the C# way where it's explicit what "index" the argument comes from:

print("My name is {0}! Nice to meet you!\n", "Maria");

// C# doesn't have a binary format, but does hex
// However due to being explicit about the index we can remove the second argument for the formatting.
print("I am {0} years old! And in hex, that is: 0x{0:X}\n", 19);

That's actually awesome and Rust supports this too! However, this will makes things even more complicated and less readable. It will also make the parser slower and it will require more code to implement (and more code = more chances to create bugs especially for inexperienced programmers like me). However, other than been good on the eyes, this can also be very useful in some cases (that will be rare tho). Let's consider the following example:

print("My name is {} and my mother's name is also {} and my father is called {}\n", "Maria", "Maria", "Steve");

In this case we are using the string "Maria" twice which is both inefficient and silly (and the design is bad in general). So in this case the following will be better:

print("My name is {} and my mother's name is also {0} and my father is called {}\n", "Maria", "Steve");

We will call this positional placeholders. Now watch carefully the behavior. The first placeholder will get the first ("Maria") value then the current argument will be the second one ("Steve") for the next placeholder that will be used. However, positional placeholders can use any argument they want (in debug mode there will also be boundschecking) and they won't use and change the current argument so the third placeholder will use the second argument automatically. You can think of positional placeholders as they are "skipping" the current argument (and choose whatever they want) and pass their turn to the next one.

Like I said, this will rarely be useful but I still want to implement them and this behavior is the best I could think that will also not be super complicated to implement and use. Also to me the cases were this will be needed, there won't be a need for special formatting so the default automatic formatting will be enough. Keep in mind that my approach of doing things is to implement only the absolutely (well maybe a little bit more in some cases) necessary. Fully featured but not complicated if you get me.

December 21, 2021

no need for the index imo, just {}, and inside you can but specifics like mentioned above

that's cleaner, easier to read, and easier to write and manage

if someone passes a struct, then you could check if it has a function with __traits print(void* filep) and call it for custom prints

December 21, 2021

this is how string interpolation should be done in D

no need complex things, i like OP's idea, i'd use it (if it remains nogc :p)

December 22, 2021

On Tuesday, 21 December 2021 at 10:07:56 UTC, rempas wrote:

>

Before I make my library, I want to first ask what type of formatting you guys prefer between the two I'm going to show you.

I personally find the Python/Rust style (with {...}) to be more readable than the C style (with %...), but both are ok.

>

Also the design of the Rust-like way will be harder to implement (not that I mind) and it will need discussion. For example we are using "t: type" to specify the type, "b: base" to format as a signed number and specify the base, "f: number" to format as a float and specify the number of floating points etc.

One of the big advantages of C's format-string syntax is that its conventions are largely shared across programming languages. For example, if you know what %d means in C, then you also know what it means in Java, Go, D, and any other language that uses C-style format strings.

If you are planning on designing a new {}-style format-string syntax, I would strongly recommend aiming for consistency with other languages like Python, Rust, and C# that use {} for string formatting. For example: all of those languages use {0} to insert the first parameter, {1} to insert the second, and so forth, so your new library should do the same.

« First   ‹ Prev
1 2