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-17 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-17 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 m

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 My

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: https://phobos.dpldo

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 it.

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) enum

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 the

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. auto

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 (isFloati

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, and

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 o

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 n

Re: variable template question

2018-01-14 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 14 January 2018 at 16:23:18 UTC, kdevel wrote: Why does this compile while both of the commented lines give a compile error. The code boils down to this: struct decimal32 { this(int x) {} } immutable decimal32 c = 3; /* works */ void main () { immutable decimal32 i = 1

variable template question

2018-01-14 Thread kdevel via Digitalmars-d-learn
vartmpl.d ``` import std.stdio : writeln; import decimal : decimal32; template F(T) { immutable T c = 3; } void foo (T) () { immutable T t = 1; } void main () { // immutable decimal32 i = 1; // Error: none of the overloads of '__ctor' are callable using a immutable object // foo!dec

Re: Template specialisation, "Generic type locking", offline stdlib docs and case-based template question

2017-11-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, December 01, 2017 03:39:12 helxi via Digitalmars-d-learn wrote: > 1. Template specialisation. > Why is this useful?: > T getResponse(T = int)(string question); And how does it differ > from > int getResponse(string question); ? > > Context: http://ddili.org/ders/d.en/templates.html : Sec

Re: Template specialisation, "Generic type locking", offline stdlib docs and case-based template question

2017-11-30 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 1 December 2017 at 03:39:12 UTC, helxi wrote: 1. Template specialisation. Why is this useful?: T getResponse(T = int)(string question); And how does it differ from int getResponse(string question); ? Because you can change the T at the call site. Let me give you a real world exam

Re: Template specialisation, "Generic type locking", offline stdlib docs and case-based template question

2017-11-30 Thread user1234 via Digitalmars-d-learn
On Friday, 1 December 2017 at 03:39:12 UTC, helxi wrote: 1. Template specialisation. Why is this useful?: T getResponse(T = int)(string question); And how does it differ from int getResponse(string question); ? Good Q. Without thinking more it looks like a pointless example. The only differe

Template specialisation, "Generic type locking", offline stdlib docs and case-based template question

2017-11-30 Thread helxi via Digitalmars-d-learn
1. Template specialisation. Why is this useful?: T getResponse(T = int)(string question); And how does it differ from int getResponse(string question); ? Context: http://ddili.org/ders/d.en/templates.html : Section: "Default template parameters" 2. "Generic locking". Is it possible to specia

Re: Template Question

2017-11-20 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/19/17 2:41 PM, Adam D. Ruppe wrote: On Sunday, 19 November 2017 at 19:31:53 UTC, Jiyan wrote: Text X; You still need to instantiate it, even with default args. Text!() X; will work If that's the case, then he needs to use Text(T = char) (default type) vs. Text(T : char) (specializa

Re: Template Question

2017-11-19 Thread Jiyan via Digitalmars-d-learn
On Sunday, 19 November 2017 at 19:42:02 UTC, Jonathan M Davis wrote: On Sunday, November 19, 2017 19:25:40 Jiyan via Digitalmars-d-learn wrote: [...] Okay. For starters, [...] Ah ok thanks very much, this helped me a lot :)

Re: Template Question

2017-11-19 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 19 November 2017 at 19:31:53 UTC, Jiyan wrote: Text X; You still need to instantiate it, even with default args. Text!() X; will work

Re: Template Question

2017-11-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, November 19, 2017 19:25:40 Jiyan via Digitalmars-d-learn wrote: > With working i mean that > Text X; > Doesnt compile! Okay. For starters, struct Text(T : char) { size_t _len; T* _ptr; } is a template specialization, which means that it's only going to compile if T is char or

Re: Template Question

2017-11-19 Thread Jiyan via Digitalmars-d-learn
On Sunday, 19 November 2017 at 19:28:37 UTC, Jonathan M Davis wrote: On Sunday, November 19, 2017 19:22:51 Jiyan via Digitalmars-d-learn wrote: Hello, i wanted to ask why this isnt working: struct Text(T : char) { size_t _len; T* _ptr; } Thanks :) What about it isn't working? I think th

Re: Template Question

2017-11-19 Thread Jiyan via Digitalmars-d-learn
With working i mean that Text X; Doesnt compile!

Re: Template Question

2017-11-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, November 19, 2017 19:22:51 Jiyan via Digitalmars-d-learn wrote: > Hello, > > i wanted to ask why this isnt working: > > struct Text(T : char) > { > size_t _len; > T* _ptr; > } > > Thanks :) What about it isn't working? I think that you need to explain what you're trying to do and wh

Template Question

2017-11-19 Thread Jiyan via Digitalmars-d-learn
Hello, i wanted to ask why this isnt working: struct Text(T : char) { size_t _len; T* _ptr; } Thanks :)

Re: Real naive template question

2017-08-13 Thread Nicholas Wilson via Digitalmars-d-learn
On Monday, 14 August 2017 at 00:44:05 UTC, WhatMeForget wrote: module block_template; void main() { template BluePrint(T, U) { T integer; U floatingPoint; } BluePrint!(int, float); } // DMD returns // template.d(13): Error: BluePrint!(int, float) has no effect

Real naive template question

2017-08-13 Thread WhatMeForget via Digitalmars-d-learn
module block_template; void main() { template BluePrint(T, U) { T integer; U floatingPoint; } BluePrint!(int, float); } // DMD returns // template.d(13): Error: BluePrint!(int, float) has no effect // I was expecting something like the following to be created a

Re: mixin template question

2015-04-11 Thread Paul D Anderson via Digitalmars-d-learn
On Sunday, 12 April 2015 at 04:04:43 UTC, lobo wrote: On Sunday, 12 April 2015 at 03:51:03 UTC, Paul D Anderson wrote: I don't understand why the following code compiles and runs without an error: import std.stdio; mixin template ABC(){ int abc() { return 3; } } mixin ABC; int abc() { retu

Re: mixin template question

2015-04-11 Thread lobo via Digitalmars-d-learn
On Sunday, 12 April 2015 at 03:51:03 UTC, Paul D Anderson wrote: I don't understand why the following code compiles and runs without an error: import std.stdio; mixin template ABC(){ int abc() { return 3; } } mixin ABC; int abc() { return 4; } void main() { writefln("abc() = %s", abc())

mixin template question

2015-04-11 Thread Paul D Anderson via Digitalmars-d-learn
I don't understand why the following code compiles and runs without an error: import std.stdio; mixin template ABC(){ int abc() { return 3; } } mixin ABC; int abc() { return 4; } void main() { writefln("abc() = %s", abc()); } Doesn't the mixin ABC create a function with the same signatu