digitalmars.D.learn - Checking template parameter types of class
- tcak (9/9) May 24 2015 Is there any syntax for something like that:
- Jonathan M Davis via Digitalmars-d-learn (17/26) May 24 2015 What about it isn't accepted? This code compiles just fine
- tcak (7/41) May 24 2015 Well, if I do not check the line number of error, this happens.
- Jonathan M Davis via Digitalmars-d-learn (17/65) May 24 2015 shared(FileResourceList) is not implicitly convertible to FileResource. ...
- tcak (6/41) May 24 2015 Nah. Its all about giving the T value on object creation.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/15) May 25 2015 struct and class templates do not have automatic type deduction;
- thedeemon (4/8) May 25 2015 Compiler means "template is used as a type" which is an error
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
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
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: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 typeIs 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
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: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 DavisOn Monday, May 25, 2015 03:19:29 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 typeIs 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
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: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.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 typeshared(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
On 05/24/2015 09:14 PM, tcak wrote: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.Line 243: auto fileResourceList = new shared FileResourceList( 2 ); main.d(243): Error: class main.FileResourceList(T) if (is(T : FileResource)) is used as a typeauto 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
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 typeThe 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