On 3/17/20 8:04 AM, rikki cattermole wrote:
On 18/03/2020 12:59 AM, Steven Schveighoffer wrote:
I think he's looking for object pattern matching. i.e. you give it an
Object, and it runs a block of code based on the derived type.
In case this syntax is unknown:
if (MyObject1 myObject = cast
On 18/03/2020 12:59 AM, Steven Schveighoffer wrote:
I think he's looking for object pattern matching. i.e. you give it an
Object, and it runs a block of code based on the derived type.
In case this syntax is unknown:
if (MyObject1 myObject = cast(MyObject1)obj) {
...
} else
implemented the feature), or use
cascaded if statements:
I've just given a fast look at the thread, so maybe I'm wrong, but this
[1] should be ok for pattern matching using plain and simple Phobos ...
[1] https://dlang.org/phobos/std_variant.html#.visit
I think he's looking for object pattern
if statements:
I've just given a fast look at the thread, so maybe I'm wrong,
but this [1] should be ok for pattern matching using plain and
simple Phobos ...
[1] https://dlang.org/phobos/std_variant.html#.visit
I've been playing around with this via inheritance lately (I'm
aware of the sumtype package but specifically wanted to use
objects instead of structs), had some good results.
It involves a bit of boilerplate though. I'm essentially using
the visitor pattern + an anonymous class implementing a
://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching
It is an example from c#.
object is the top type in that language.
https://en.wikipedia.org/wiki/Top_type
D doesn't support this natively. The closest you can get is something
akin to what aliak wrote (you would need to write something, not sure
/pattern-matching
It is an example from c#.
object is the top type in that language.
https://en.wikipedia.org/wiki/Top_type
On 3/14/20 3:04 PM, 12345swordy wrote:
I.E.
switch (object)
case Type1 t1:
case Type2 t2:
case Type3 t3:
Is this a class object and you are trying to determine at runtime which
derived type it is and perform an action based on that? Or are you
trying to switch on the type of
On Saturday, 14 March 2020 at 20:52:30 UTC, aliak wrote:
On Saturday, 14 March 2020 at 19:04:28 UTC, 12345swordy wrote:
[...]
You can use the sumtype package
(https://code.dlang.org/packages/sumtype):
[...]
That simply to much verbiage.
On Saturday, 14 March 2020 at 19:04:28 UTC, 12345swordy wrote:
I.E.
switch (object)
case Type1 t1:
case Type2 t2:
case Type3 t3:
You can use the sumtype package
(https://code.dlang.org/packages/sumtype):
alias T = SumType!(Type1, Type2, Type3);
T(object).match!(
(Type1 t1)
On Saturday, 14 March 2020 at 19:04:28 UTC, 12345swordy wrote:
I.E.
switch (object)
case Type1 t1:
case Type2 t2:
case Type3 t3:
As far as I know, there's no way to do that in a switch.
However, you can do something like this:
---
void main()
{
auto i = new Type1();
I.E.
switch (object)
case Type1 t1:
case Type2 t2:
case Type3 t3:
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
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
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
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
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
On Saturday, 25 June 2016 at 12:35:39 UTC, Lodovico Giaretta
wrote:
On Saturday, 25 June 2016 at 12:30:22 UTC, Lodovico Giaretta
wrote:
If you want this to work, you need your lambdas to take the
casted value as a parameter:
Thanks.
On Saturday, 25 June 2016 at 12:30:22 UTC, Lodovico Giaretta
wrote:
If you want this to work, you need your lambdas to take the
casted value as a parameter:
void test(T)(T value) {
int i;
string s;
match!(value,
int, (val) => i = val,
string, (val) => s = val
On Saturday, 25 June 2016 at 10:39:09 UTC, John wrote:
Thanks for the help, both. This appeared to work, until I
realised the lambda isn't static:
void match(T, cases...)() {
static if (cases.length == 1) cases[0]();
else static if (cases.length > 2) {
static if
On Saturday, 25 June 2016 at 09:12:12 UTC, Lodovico Giaretta
wrote:
On Saturday, 25 June 2016 at 09:07:19 UTC, Lodovico Giaretta
wrote:
Instead of passing functions to match!, pass pairs of
arguments, like this:
match!(T,
int, writeln("Matched int"),
is(T : SomeObject),
On Saturday, 25 June 2016 at 09:07:19 UTC, Lodovico Giaretta
wrote:
Instead of passing functions to match!, pass pairs of
arguments, like this:
match!(T,
int, writeln("Matched int"),
is(T : SomeObject), writeln("Derives from SomeObject");
);
Now, in the
also, there is a subtle bug in matcher. sorry. ;-)
On Saturday, 25 June 2016 at 08:46:05 UTC, John wrote:
Anyone able to improve on it?
q hack:
template tyma(T, Cases...) {
import std.traits;
template GetFunc(size_t idx) {
static if (idx >= Cases.length) {
static assert(0, "no delegate for match");
} else static if
On Saturday, 25 June 2016 at 08:46:05 UTC, John wrote:
Writing a long series of "static if ... else" statements can be
tedious and I'm prone to leaving out the crucial "static" after
"else", so I was wondered if it was possible to write a
template that would resemble the switch statement, but
Writing a long series of "static if ... else" statements can be
tedious and I'm prone to leaving out the crucial "static" after
"else", so I was wondered if it was possible to write a template
that would resemble the switch statement, but for types.
Closest I came up to was this:
void
On 6/5/15 10:15 AM, Dennis Ritchie wrote:
On Friday, 5 June 2015 at 13:13:15 UTC, Steven Schveighoffer wrote:
string foo(string mode, string value)
{
return `writefln(mode ` ~ mode ~ `: %s, ` ~ value ~ `);`;
}
void main()
{
mixin(foo(Y, 3));
mixin(foo(X, 2));
}
Thanks. It looks
have two empty structs named
AddTid and RemoveTid, and I pass those along. I assume some bytes
must be passed in their place to tell of their type, but that's
fine.
It does feel rather hacked, however. I'm aware that D doesn't
support excessively fancy pattern matching, like what is found
A small example:
while (true) {
receive(
(Tid tid, AddTid _) {some code}
(Tid tid, RemoveTid _) {some other code}
(string s) {broadcast stuff}
)
}
struct AddTid {}
struct RemoveTid {}
On Wednesday, 29 January 2014 at 21:50:28 UTC, Casper Færgemand
wrote:
A small example:
while (true) {
receive(
(Tid tid, AddTid _) {some code}
(Tid tid, RemoveTid _) {some other code}
(string s) {broadcast stuff}
)
}
struct AddTid {}
struct RemoveTid {}
From where I sit
template factorial(int n) { const factorial = n * factorial!(n-1); }
template factorial(int n : 1) { const factorial = 1; }
i think this pattern matching or like it, can i do the same thing with regular
function
int factorial(int n) {
return n* factorial(n-1);
return 1 ;
}
int factorial(int n
%u:
is that work?
In D unfortunately there is no pattern matching on the run-time values of
function arguments.
But I don't really understand what you are asking me, sorry.
Bye,
bearophile
On Tuesday, September 06, 2011 21:05:54 %u wrote:
template factorial(int n) { const factorial = n * factorial!(n-1); }
template factorial(int n : 1) { const factorial = 1; }
i think this pattern matching or like it, can i do the same thing with
regular function
int factorial(int n
33 matches
Mail list logo