www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Define a new custom operator in D Language.

reply BoQsc <vaidas.boqsc gmail.com> writes:
Here is my issue: I've found a formula on Wikipedia.

It's called **Hashing by division**.

![](https://i.imgur.com/UJPAWIW.png)
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.
Oct 02 2023
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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**.

 ![](https://i.imgur.com/UJPAWIW.png)
 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
Oct 02 2023
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
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**.

 ![](https://i.imgur.com/UJPAWIW.png)
 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.
Oct 02 2023
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
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.
Oct 02 2023
prev sibling parent reply bachmeier <no spam.net> writes:
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.
Oct 02 2023
parent IchorDev <zxinsworld gmail.com> writes:
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.
Oct 08 2023
prev sibling parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
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.
Oct 10 2023