Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-18 Thread james.p.leblanc via Digitalmars-d-learn
On Wednesday, 18 August 2021 at 06:53:51 UTC, Tejas wrote: void funcTemplate(T:int)(T a){ writeln("argument is int"); } void funcTemplate(T : long)(T a){ writeln("argument is long integer"); } void main(){ int a; long b; func(a); func(b); funcTemplate(a);

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-18 Thread Tejas via Digitalmars-d-learn
On Wednesday, 18 August 2021 at 05:33:13 UTC, james.p.leblanc wrote: On Tuesday, 17 August 2021 at 20:28:20 UTC, Alexandru Ermicioi wrote: On Tuesday, 17 August 2021 at 19:53:52 UTC, james.p.leblanc wrote: Wow! That is absolutely beautiful ... I had never seen (or even imagined) a recursive

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-18 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
On Wednesday, 18 August 2021 at 05:33:13 UTC, james.p.leblanc wrote: If I wanted to ensure that a function accepts only arguments of byte, int, uint, long, etc. (i.e. integer-like types). Is the accepted way to do this like so?: **auto foo( T : long )(T a, T b){ ... }** I very much prefer

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread james.p.leblanc via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 20:28:20 UTC, Alexandru Ermicioi wrote: On Tuesday, 17 August 2021 at 19:53:52 UTC, james.p.leblanc wrote: Wow! That is absolutely beautiful ... I had never seen (or even imagined) a recursive template! This expands my mind in a good way ... and is going into

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Bastiaan Veelo via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 20:29:51 UTC, james.p.leblanc wrote: So, below is my code: import std.stdio; import std.meta : AliasSeq; template isAmong(T, S...) { static if (S.length == 0) enum isAmong = false; else enum isAmong = is(T == S) || isAmong(T, S[1..$]); } alias

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Aug 17, 2021 at 07:53:52PM +, james.p.leblanc via Digitalmars-d-learn wrote: > On Tuesday, 17 August 2021 at 19:44:29 UTC, H. S. Teoh wrote: > > You could use a helper template and an AliasSeq for this: > > > > template isAmong(T, S...) { > > static if (S.length == 0)

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread james.p.leblanc via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 20:13:59 UTC, Paul Backus wrote: FYI: in this particular case, you can use std.meta.staticIndexOf instead of writing the recursion out yourself: import std.meta: staticIndexOf; enum isAmong(T, S...) = staticIndexOf!(T, S) >= 0; Docs:

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 19:53:52 UTC, james.p.leblanc wrote: Wow! That is absolutely beautiful ... I had never seen (or even imagined) a recursive template! This expands my mind in a good way ... and is going into my toolbox immediately. Best Regards, James Just don't over rely on

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 19:53:52 UTC, james.p.leblanc wrote: On Tuesday, 17 August 2021 at 19:44:29 UTC, H. S. Teoh wrote: You could use a helper template and an AliasSeq for this: template isAmong(T, S...) { static if (S.length == 0)

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread james.p.leblanc via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 19:44:29 UTC, H. S. Teoh wrote: You could use a helper template and an AliasSeq for this: template isAmong(T, S...) { static if (S.length == 0) enum isAmong = false; else enum

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Aug 17, 2021 at 07:22:54PM +, james.p.leblanc via Digitalmars-d-learn wrote: [...] > auto moo(T : (int || float || mySpecialStruct )(T myMoo) {•••} > > When re-using any such sets, it would be nice to define the set as > follows: > > S = (int || float || mySpecialStruct) > > and

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread james.p.leblanc via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 18:28:53 UTC, Steven Schveighoffer wrote: On 8/17/21 2:11 PM, james.p.leblanc wrote: Evening All, [Template constraints](https://dlang.org/spec/template.html#template_constraints). -Steve Dear All, Thanks! I was aware of, and have used template constraints

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/17/21 2:11 PM, james.p.leblanc wrote: Evening All, Eponymous templates allow a nice calling syntax.  For example, "foo" here can be called without needing the exclamation mark (!) at calling sites.  We see that foo is restricting a, and b to be of the same type ... so far, so good.

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Ali Çehreli via Digitalmars-d-learn
On 8/17/21 11:11 AM, james.p.leblanc wrote: > auto foo(T)(T a, T b) { ... } > > Now, suppose I need to restrict "T" only certain subsets of variable types. There are template constraints: import std.traits; auto foo(T)(T a, T b) if (isArray!T) { // ... } auto foo(T)(T a, T b) if

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Aug 17, 2021 at 06:11:56PM +, james.p.leblanc via Digitalmars-d-learn wrote: > Evening All, > > Eponymous templates allow a nice calling syntax. For example, "foo" > here can be called without needing the exclamation mark (!) at calling > sites. We see that foo is restricting a,

Re: simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Tuesday, 17 August 2021 at 18:11:56 UTC, james.p.leblanc wrote: Is there a more elegant way, to do this? Regards, James PS Any violations should be caught at compile time. That is template specialization: ``` auto moo(T : YourSpecialClassOrDType)(T myMoo) {•••} ``` You can also declare

simple (I think) eponymous template question ... what is proper idimatic way ?

2021-08-17 Thread james.p.leblanc via Digitalmars-d-learn
Evening All, Eponymous templates allow a nice calling syntax. For example, "foo" here can be called without needing the exclamation mark (!) at calling sites. We see that foo is restricting a, and b to be of the same type ... so far, so good. auto foo(T)(T a, T b) { ... } Now, suppose I