August 16, 2021

On Monday, 16 August 2021 at 06:36:02 UTC, james.p.leblanc wrote:

>

On Monday, 16 August 2021 at 06:20:11 UTC, Tejas wrote:

>

Maybe just write T[] in code rather than making it happen via the colon?

I also have similar troubles, removing the default value always helped me.

Can you show the code where you're trying to apply this?

Hej Tejas,

Sure, here is the code snippet, it resides within a struct
along with various operator overloads.

To be honest, I am not exactly sure what is happening here. I
am unfamiliar with the "(T : T[])" syntax ... need to read
up a bit on that. (However, not so eary searching for a ":")

BR,
jpl

// --- cast overloading -------------------------------------------
// T[] opCast(T : T[])(){ // this works
T[] opCast(T[])(){ // yields: "Error: identifier expected for template value param"
T[] x;
x.length = this.length;
for( int i = 0 ; i< this.length ; i++){
x[i] = this.ptr[i];
}
return x;
}

Oh, you're writing T[] in the code anyways.

I was saying to just remove the :T[] from the function parameter and write T[] in the function body itself, which you're already doing.

If the code works, what's the problem?

August 16, 2021

On Monday, 16 August 2021 at 06:42:48 UTC, Tejas wrote:

>

If the code works, what's the problem?

Hej Again,

I was able to construct the working code shown above from help I
obtained here in the forum and other resources.

My original code was not working ... but updated code is working
fine ... so there are no further problems. (Actually, I learned
a fair amount in this endeavor!)

Cheers!
jpl

August 16, 2021

On Monday, 16 August 2021 at 06:49:08 UTC, james.p.leblanc wrote:

>

On Monday, 16 August 2021 at 06:42:48 UTC, Tejas wrote:

>

If the code works, what's the problem?

Hej Again,

I was able to construct the working code shown above from help I
obtained here in the forum and other resources.

My original code was not working ... but updated code is working
fine ... so there are no further problems. (Actually, I learned
a fair amount in this endeavor!)

Cheers!
jpl

Cool

Just for completeness, here's what my solution looks like:

import std;

struct S{
    char[] a=['a','v','d'];
    T opCast(T)(){
        return a.dup ;
    }
}
void main()
{
    writeln("Hello D");
    S s = S();
    char[] d = cast(char[])s;
}
August 16, 2021
On Monday, 16 August 2021 at 06:36:02 UTC, james.p.leblanc wrote:
> To be honest, I am not exactly sure what is happening here. I
> am unfamiliar with the "(T : T[])" syntax ... need to read

That is template argument secialization. You're saying that T can be accept only types that are arrays of T, where : reads as 'extends'.

Now T : T[] might introduce cyclic reference, so more correct would be: foo(T : Z[], Z)(...)

Here you say that T might accept only types that are arrays of Z elements.

Regards,
Alexandru.
August 16, 2021
On Monday, 16 August 2021 at 10:48:19 UTC, Alexandru Ermicioi wrote:
> On Monday, 16 August 2021 at 06:36:02 UTC, james.p.leblanc wrote:
>> To be honest, I am not exactly sure what is happening here. I
>> am unfamiliar with the "(T : T[])" syntax ... need to read
>
> That is template argument secialization. You're saying that T can be accept only types that are arrays of T, where : reads as 'extends'.
>
> Now T : T[] might introduce cyclic reference, so more correct would be: foo(T : Z[], Z)(...)
>
> Here you say that T might accept only types that are arrays of Z elements.
>
> Regards,
> Alexandru.

Alexandru,

That is very helpful information.  It also answers a nagging
question I have been worried about.  Specifically, that "T : T[]"
bothered me even though I was unfamiliar with the syntax ... now
I understand the situation better.

Best Regards,
James

1 2
Next ›   Last »