What type of `print` formatting do you prefer?
bauss
jj_1337 at live.dk
Tue Dec 21 11:17:47 UTC 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:
>
> ```d
> 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:
```d
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);
```
More information about the Digitalmars-d
mailing list