Should we add `a * b` for vectors?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Oct 6 21:16:45 UTC 2017


On Friday, October 06, 2017 21:05:05 jmh530 via Digitalmars-d wrote:
> On Friday, 6 October 2017 at 20:36:47 UTC, Jonathan M Davis wrote:
> > It forces them to actually be designed with the type and be
> > easily located with the type. Would you want a programmer to be
> > able to go and implement opBinary!"+" for strings? I sure
> > wouldn't. And I don't want anyone doing that for user-defined
> > types that they didn't define either.
>
> Tangentially (I admit), there's nothing stopping you from below:
>
> struct MyString
> {
>      string mystring;
>      alias mystring this;
>      string opBinary(string op)(string rhs)
>          if(op == "+")
>      {
>          return mystring ~ rhs;
>      }
> }
>
> void main()
> {
>      MyString x = MyString("foo");
>      string y = "bar";
>      auto z = x + y;
>      assert(z == "foobar");
> }

Yes, but then at least it's a type that you've defined to be dumb like that
rather that built-in's or someone else's type being hijacked to behave
badly. It's not like we're going to stop every stupid thing that someone
could do (and if we did, it would severely limit our ability to do
intelligent things), but I see no value in allowing 3rd parties to tack on
overloaded operators onto types that they do not control.

- Jonathan M Davis



More information about the Digitalmars-d mailing list