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);
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
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
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
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
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)
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 :)
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
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
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
With working i mean that
Text X;
Doesnt compile!
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
Hello,
i wanted to ask why this isnt working:
struct Text(T : char)
{
size_t _len;
T* _ptr;
}
Thanks :)
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
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
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
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())
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
36 matches
Mail list logo