Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 06, 2017 Writing pattern matching macros in D. | ||||
---|---|---|---|---|
| ||||
Hi all, I've been reading up on D's metaprogramming features and was wondering if it was possible to use them to add pattern matching to the language as a macro. The template mixin feature seems to require putting the new syntax in strings. I was hoping there's an alternative. Thanks! -deech |
March 06, 2017 Re: Writing pattern matching macros in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deech | On Monday, 6 March 2017 at 02:20:02 UTC, Deech wrote: > Hi all, > I've been reading up on D's metaprogramming features and was wondering if it was possible to use them to add pattern matching to the language as a macro. The template mixin feature seems to require putting the new syntax in strings. I was hoping there's an alternative. > Thanks! > -deech It isn't possible in the same way it is in, say, Rust. This has come up before and Walter has strong opinions against that kind of rewriting macro. This is what's currently implemented: https://dlang.org/phobos/std_variant.html#visit |
March 06, 2017 Re: Writing pattern matching macros in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deech | On Monday, 6 March 2017 at 02:20:02 UTC, Deech wrote:
> […] add pattern matching to the language as a macro.
D doesn't have macros per se. However, template metaprogramming and mixins can replace them in many cases.
Which particular form of pattern matching do you have in mind? You won't get all the way to Haskell (or even Prolog) levels of functionality in a generic way, but for limited use cases, it is definitely possible.
— David
|
March 06, 2017 Re: Writing pattern matching macros in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | On Monday, 6 March 2017 at 08:27:13 UTC, David Nadlinger wrote: > On Monday, 6 March 2017 at 02:20:02 UTC, Deech wrote: >> […] add pattern matching to the language as a macro. > > D doesn't have macros per se. However, template metaprogramming and mixins can replace them in many cases. > > Which particular form of pattern matching do you have in mind? You won't get all the way to Haskell (or even Prolog) levels of functionality in a generic way, but for limited use cases, it is definitely possible. > > — David I was thinking something on the order of Scala's pattern matching using apply/unapply methods. http://www.artima.com/pins1ed/extractors.html. |
March 06, 2017 Re: Writing pattern matching macros in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deech | On 2017-03-06 17:27, Deech wrote: > I was thinking something on the order of Scala's pattern matching using > apply/unapply methods. http://www.artima.com/pins1ed/extractors.html. That should be possible. Although not as a macro and not with the same nice syntax. Something like this should be possible: 1.match( 3, e => writeln("value is 3"), (int e) => writeln("value is an integer"), () => writeln("fallback") ); -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation