www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checking template parameter types of class

reply "tcak" <tcak gmail.com> writes:
Is there any syntax for something like that:

class Resource(T) if( is(T: FileResource) ){
}


I tried it as above, but it is not accepted. Maybe I am following 
a wrong syntax.

I tried

class Resource(T: FileResource){
}

But it is not accepted as well.
May 24 2015
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Monday, May 25, 2015 03:19:29 tcak via Digitalmars-d-learn wrote:
 Is there any syntax for something like that:

 class Resource(T) if( is(T: FileResource) ){
 }


 I tried it as above, but it is not accepted. Maybe I am following
 a wrong syntax.

 I tried

 class Resource(T: FileResource){
 }

 But it is not accepted as well.
What about it isn't accepted? This code compiles just fine class FileResource { } class SubFileResource : FileResource { } class Resource(T) if(is(T : FileResource)) { } void main() { Resource!SubFileResource foo; } - Jonathan M Davis
May 24 2015
parent reply "tcak" <tcak gmail.com> writes:
On Monday, 25 May 2015 at 03:35:22 UTC, Jonathan M Davis wrote:
 On Monday, May 25, 2015 03:19:29 tcak via Digitalmars-d-learn 
 wrote:
 Is there any syntax for something like that:

 class Resource(T) if( is(T: FileResource) ){
 }


 I tried it as above, but it is not accepted. Maybe I am 
 following
 a wrong syntax.

 I tried

 class Resource(T: FileResource){
 }

 But it is not accepted as well.
What about it isn't accepted? This code compiles just fine class FileResource { } class SubFileResource : FileResource { } class Resource(T) if(is(T : FileResource)) { } void main() { Resource!SubFileResource foo; } - Jonathan M Davis
Well, if I do not check the line number of error, this happens. It was giving error on the line of creating a new instance. Line 243: auto fileResourceList = new shared FileResourceList( 2 ); main.d(243): Error: class main.FileResourceList(T) if (is(T : FileResource)) is used as a type
May 24 2015
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Monday, May 25, 2015 03:42:22 tcak via Digitalmars-d-learn wrote:
 On Monday, 25 May 2015 at 03:35:22 UTC, Jonathan M Davis wrote:
 On Monday, May 25, 2015 03:19:29 tcak via Digitalmars-d-learn
 wrote:
 Is there any syntax for something like that:

 class Resource(T) if( is(T: FileResource) ){
 }


 I tried it as above, but it is not accepted. Maybe I am
 following
 a wrong syntax.

 I tried

 class Resource(T: FileResource){
 }

 But it is not accepted as well.
What about it isn't accepted? This code compiles just fine class FileResource { } class SubFileResource : FileResource { } class Resource(T) if(is(T : FileResource)) { } void main() { Resource!SubFileResource foo; } - Jonathan M Davis
Well, if I do not check the line number of error, this happens. It was giving error on the line of creating a new instance. Line 243: auto fileResourceList = new shared FileResourceList( 2 ); main.d(243): Error: class main.FileResourceList(T) if (is(T : FileResource)) is used as a type
shared(FileResourceList) is not implicitly convertible to FileResource. It may be implicitly convertible to shared(FileResource), but not FileResource). Code that uses shared usually has to be written specifically for shared. Also, any time that you construct a templated type, you have to provide the template argument. IFTI (implicit function template instantiation) doesn't work with constructors. To make that work, you need a wrapper function - e.g. auto resource(T)(T t) { return new Resource!T(t); } So, I suspect that you're either running into problems, because you're using shared, and Resource's template constraint requires non-shared, and/or because you aren't explicitly instantiating Resource with a template argument. - Jonathan M Davis
May 24 2015
parent reply "tcak" <tcak gmail.com> writes:
On Monday, 25 May 2015 at 04:07:06 UTC, Jonathan M Davis wrote:
 On Monday, May 25, 2015 03:42:22 tcak via Digitalmars-d-learn 
 wrote:
 Well, if I do not check the line number of error, this happens.
 It was giving error on the line of creating a new instance.

 Line 243: auto fileResourceList = new shared FileResourceList( 
 2
 );

 main.d(243): Error: class main.FileResourceList(T) if (is(T :
 FileResource)) is used as a type
shared(FileResourceList) is not implicitly convertible to FileResource. It may be implicitly convertible to shared(FileResource), but not FileResource). Code that uses shared usually has to be written specifically for shared. Also, any time that you construct a templated type, you have to provide the template argument. IFTI (implicit function template instantiation) doesn't work with constructors. To make that work, you need a wrapper function - e.g. auto resource(T)(T t) { return new Resource!T(t); } So, I suspect that you're either running into problems, because you're using shared, and Resource's template constraint requires non-shared, and/or because you aren't explicitly instantiating Resource with a template argument. - Jonathan M Davis
Nah. Its all about giving the T value on object creation. auto fileResourceList = new shared FileResourceList!FileResource( 2 ); Then it works. The error message is not indicating directly this, though logically it is still correct.
May 24 2015
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/24/2015 09:14 PM, tcak wrote:

 Line 243: auto fileResourceList = new shared FileResourceList( 2
 );

 main.d(243): Error: class main.FileResourceList(T) if (is(T :
 FileResource)) is used as a type
struct and class templates do not have automatic type deduction; function templates do. The solution is to provide a convenience function template alongside the struct or class.
 auto fileResourceList = new shared FileResourceList!FileResource( 2 );

 Then it works. The error message is not indicating directly this, though
 logically it is still correct.
'if (is(T : FileResource))' part means "if T can implicitly be converted to FileResource". You still provide T so that it gets checked. Ali
May 25 2015
prev sibling parent "thedeemon" <dlang thedeemon.com> writes:
On Monday, 25 May 2015 at 04:15:00 UTC, tcak wrote:

 main.d(243): Error: class main.FileResourceList(T) if (is(T :
 FileResource)) is used as a type
 The error message is not indicating directly this, though 
 logically it is still correct.
Compiler means "template is used as a type" which is an error since templates are not types. I guess adding the word "template" to that message would make it more clear.
May 25 2015