Skip to content

Operator Overloading #871

@nicolasessisbreton

Description

@nicolasessisbreton

I know operator overloading is considered out of Zig scope.
But no issue seems to make the following case so here it is.

Operator overloading matter in scientific computing

Consider writing polynomial addition with function

p = 2*x*y + 3*z + 5y - 8*x;

assume x, y and z are some other complex polynomials.
With functions, this becomes

p = sub(add(add(mul(2, mul(x,y)), mul(3,z)), mul(5,y)), mul(8,y))
Is the intent clear in the previous line?
Isn't forcing the previous line on user breaking Zig simplicity principles?

Zig promises to make scientific computing easier and fun

When writing numerical code in C++, memory leak is a pain.
With Zig focus on no 'undefined behavior', writing fast numerical can be made much easier.
C++ is currently a big entry barrier in high performance numerical computing.

Why 'printf' and not '+'

Zig brings a new paradigm where function previously hidden in the compiler internal are now in userland.
Why limit this paradigm to standard function call?

Zig offers an all encompassing approach that offers a build system and a package manager.
There are many domain where operator overloading is crucial.
For example, in Fortran '+' is overloaded in the compiler to support vector addition.
If Zig doesn't offer operator overloading, users will either:

  • write some complex preprocessor to get it,
  • stick with C++.

These two solutions break many Zig principles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions