Thread overview
Define a new custom operator in D Language.
Oct 02, 2023
BoQsc
Oct 02, 2023
Imperatorn
Oct 02, 2023
BoQsc
Oct 02, 2023
Imperatorn
Oct 02, 2023
bachmeier
Oct 08, 2023
IchorDev
Oct 10, 2023
Jesse Phillips
October 02, 2023

Here is my issue: I've found a formula on Wikipedia.

It's called Hashing by division.


As you can see it uses mod keyword to achieve the modulus operation.

In D language we use modulus operator % and it might look more like this:

h(x) M % m

This clearly introduces confusion between the source (wikipedia) and the implementation (dlang version).

I would like to know how we could define/alia ourselves a mod operator in D Language.

h(x) M mod m

This might lead to less gaps between math formulas and the implementation.

Or at the very least would allow to define a formula in the source code for further implementation and introduce some consistency.

October 02, 2023

On Monday, 2 October 2023 at 18:34:13 UTC, BoQsc wrote:

>

Here is my issue: I've found a formula on Wikipedia.

It's called Hashing by division.


As you can see it uses mod keyword to achieve the modulus operation.

In D language we use modulus operator % and it might look more like this:

h(x) M % m

This clearly introduces confusion between the source (wikipedia) and the implementation (dlang version).

I would like to know how we could define/alia ourselves a mod operator in D Language.

h(x) M mod m

This might lead to less gaps between math formulas and the implementation.

Or at the very least would allow to define a formula in the source code for further implementation and introduce some consistency.

https://dlang.org/spec/operatoroverloading.html#binary

October 02, 2023

On Monday, 2 October 2023 at 18:39:41 UTC, Imperatorn wrote:

>

On Monday, 2 October 2023 at 18:34:13 UTC, BoQsc wrote:

>

Here is my issue: I've found a formula on Wikipedia.

It's called Hashing by division.


As you can see it uses mod keyword to achieve the modulus operation.

In D language we use modulus operator % and it might look more like this:

h(x) M % m

This clearly introduces confusion between the source (wikipedia) and the implementation (dlang version).

I would like to know how we could define/alia ourselves a mod operator in D Language.

h(x) M mod m

This might lead to less gaps between math formulas and the implementation.

Or at the very least would allow to define a formula in the source code for further implementation and introduce some consistency.

https://dlang.org/spec/operatoroverloading.html#binary

Overloading seems to only overload behaviour of existing operator, like:

+	-	*	/	%	^^	&
|	^	<<	>>	>>>	~	in

I'm unable to see how the operator overloading would allow to define a new custom operator.

October 02, 2023

On Monday, 2 October 2023 at 19:28:32 UTC, BoQsc wrote:

>

On Monday, 2 October 2023 at 18:39:41 UTC, Imperatorn wrote:

>

On Monday, 2 October 2023 at 18:34:13 UTC, BoQsc wrote:

>

[...]

https://dlang.org/spec/operatoroverloading.html#binary

Overloading seems to only overload behaviour of existing operator, like:

+	-	*	/	%	^^	&
|	^	<<	>>	>>>	~	in

I'm unable to see how the operator overloading would allow to define a new custom operator.

I guess I don't understand your confusion. % is the modulus operator, you can overload it if you want to instead do what you want according to your needs.

October 02, 2023

On Monday, 2 October 2023 at 19:28:32 UTC, BoQsc wrote:

>

Overloading seems to only overload behaviour of existing operator, like:

+	-	*	/	%	^^	&
|	^	<<	>>	>>>	~	in

I'm unable to see how the operator overloading would allow to define a new custom operator.

And I don't expect that to change. This has come up many times. For example, https://forum.dlang.org/post/mailman.178.1688747876.3523.digitalmars-d@puremagic.com

>

Operator overloading in general is provided so that user-defined types can be used like built-in types and work with generic code that uses those operators. Domain-specific language stuff should probably be left to either a domain-specific language or just use properly named functions.

October 08, 2023

On Monday, 2 October 2023 at 21:37:56 UTC, bachmeier wrote:

>

On Monday, 2 October 2023 at 19:28:32 UTC, BoQsc wrote:

>

I'm unable to see how the operator overloading would allow to define a new custom operator.

And I don't expect that to change. This has come up many times.

With a parameter that has a symbol-like name you can use it like x.mod(y), which looks alright too.

October 10, 2023

On Monday, 2 October 2023 at 18:34:13 UTC, BoQsc wrote:

>

This might lead to less gaps between math formulas and the implementation.

Or at the very least would allow to define a formula in the source code for further implementation and introduce some consistency.

You could write a parser with pegged https://code.dlang.org/packages/pegged

Could probably support unicode math symbols.