Am I evil for this?

H. S. Teoh hsteoh at qfbox.info
Fri Oct 14 21:15:12 UTC 2022


On Fri, Oct 14, 2022 at 08:24:47PM +0000, jmh530 via Digitalmars-d wrote:
[...]
> So for instance, you can make a function
> ```
> `%foo%` <- function(x, y) { #does something }
> ```
> and use it like `x %foo% y`
> 
> The surrounding `%` lets the user know that anything could be
> happening in there. However, it doesn't have to just be some string,
> it could be anything. For instance, R comes with %*% built-in for
> matrix multiplication and a few others.

IMO, %*% is extremely ugly to read.


> From D's perspective, it doesn't really make sense to use a string
> since we have UFCS and it is just as easy to use `x.foo(y)` [in R you
> would have to do foo(x, y) if this feature didn't exist]. However, it
> is useful when it is some symbol that would otherwise have a longer
> name. Code that is full of `X.matmul(Y)` is harder to read than `X %*%
> Y`.

It's just a matter of formatting; just write it like this:

	X .mul (Y)

and it will read just fine.  The mandatory parentheses are actually a
good thing to prevent confusion with operator precedence. (Making
operator precedence user-configurable may not be a good idea -- it
forces you to do semantic analysis before the syntax can be parsed, and
will add far more complexity to the parser than is justifiable by the
marginal benefits.)

For this specific example, though, I'd say just overload *. Matrix
multiplication is called "multiplication" because it generally *does*
behave like a multiplicative arithmetic operator. As opposed to like
array manipulation or I/O.  So I'd call it an appropriate use of
operator overloading in this case.


T

-- 
One Word to write them all, One Access to find them, One Excel to count them all, And thus to Windows bind them. -- Mike Champion


More information about the Digitalmars-d mailing list