repeat

bearophile bearophileHUGS at lycos.com
Mon Jan 17 12:44:51 PST 2011


Jonathan M Davis:

> Considering that D add the ~ operator for concatenation because + was too 
> ambiguous (e.g. what should "2" + "3" do?), there's no way that overloading * 
> for a function in std.algorithm is going to fly.

I think D has used the ~ operator to do that, despite the + is much more common for this operation and the ~ character is not present on some keyboards, because the + is normally meant to be a commutative op, while the array/string concatenation is not commutative (3+5==5+3 but "ab"~"cd"!="cd"~"ab"). In the case of * for string/array multiplication it's associative, commutative, distributive and it has both identity and zero elements, so I don't think there are the same problems.


> However, I'm not sure that I've ever had to use such an operation in 
> my entire life.

This little Python2 program shows a normal usage of the string multiplication:

n = 6
s = "+---" * n + "+"
print s
for i in xrange(5):
    print "|   " * n + "|"
    print s

It prints:

+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+
|   |   |   |   |   |   |
+---+---+---+---+---+---+

Bye,
bearophile


More information about the Digitalmars-d mailing list