digitalmars.D.learn - Define a new custom operator in D Language.
- BoQsc (23/23) Oct 02 2023 Here is my issue: I've found a formula on Wikipedia.
- Imperatorn (2/25) Oct 02 2023 https://dlang.org/spec/operatoroverloading.html#binary
- BoQsc (9/42) Oct 02 2023 Overloading seems to only overload behaviour of existing
- Imperatorn (4/17) Oct 02 2023 I guess I don't understand your confusion. % is the modulus
- bachmeier (4/17) Oct 02 2023 And I don't expect that to change. This has come up many times.
- IchorDev (3/7) Oct 08 2023 With a parameter that has a symbol-like name you can use it like
- Jesse Phillips (4/10) Oct 10 2023 You could write a parser with pegged
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
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
On Monday, 2 October 2023 at 18:39:41 UTC, Imperatorn wrote:On Monday, 2 October 2023 at 18:34:13 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.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
On Monday, 2 October 2023 at 19:28:32 UTC, BoQsc wrote:On Monday, 2 October 2023 at 18:39:41 UTC, Imperatorn wrote: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.On Monday, 2 October 2023 at 18:34:13 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.[...]https://dlang.org/spec/operatoroverloading.html#binary
Oct 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.comOperator 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
On Monday, 2 October 2023 at 21:37:56 UTC, bachmeier wrote:On Monday, 2 October 2023 at 19:28:32 UTC, BoQsc wrote:With a parameter that has a symbol-like name you can use it like `x.mod(y)`, which looks alright too.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.
Oct 08 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.
Oct 10 2023