www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to define property type to Array!struct?

reply zoujiaqing <zoujiaqing gmail.com> writes:
My code:

```D
module http.HttpRequest;

import std.container;
import std.array : Appender;

struct HttpRequest
{
     struct Header()
     {
         Appender!string name;
         Appender!string value;
     }

     string method;
     string uri;
     int versionMajor = 0;
     int versionMinor = 0;
     Array!Header headers;
     ubyte[] content;
     bool keepAlive = false;
}
```

Error code:
```D
source/http/HttpRequest.d(18,5): Error: struct 
`std.container.array.Array` does not match any template 
declaration
```
Dec 14 2021
parent reply WebFreak001 <d.forum webfreak.org> writes:
On Tuesday, 14 December 2021 at 08:12:04 UTC, zoujiaqing wrote:
 My code:

 ```D
 module http.HttpRequest;

 import std.container;
 import std.array : Appender;

 struct HttpRequest
 {
     struct Header()
     {
         Appender!string name;
         Appender!string value;
     }

     string method;
     string uri;
     int versionMajor = 0;
     int versionMinor = 0;
     Array!Header headers;
     ubyte[] content;
     bool keepAlive = false;
 }
 ```

 Error code:
 ```D
 source/http/HttpRequest.d(18,5): Error: struct 
 `std.container.array.Array` does not match any template 
 declaration
 ```
the problem is that your header is a template, so you need to instantiate it: ```d Array!(Header!()) headers; ``` the error message is kinda poor here. Alternatively, remove the template `()` from your `struct Header`
Dec 14 2021
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
On Tuesday, 14 December 2021 at 08:28:01 UTC, WebFreak001 wrote:
[...]
 Alternatively, remove the template `()` from your `struct 
 Header`
What is the semantic sense of a template having no parameters? Although the documentation declares such a template to be syntactically correct, not a single example suggests a meaning.
Dec 15 2021
next sibling parent WebFreak001 <d.forum webfreak.org> writes:
On Wednesday, 15 December 2021 at 11:36:41 UTC, Manfred Nowak 
wrote:
 On Tuesday, 14 December 2021 at 08:28:01 UTC, WebFreak001 wrote:
 [...]
 Alternatively, remove the template `()` from your `struct 
 Header`
What is the semantic sense of a template having no parameters? Although the documentation declares such a template to be syntactically correct, not a single example suggests a meaning.
For functions there is a use-case: they will not be compiled/included in the executable until they are actually instantiated once. Additionally for functions that means for libraries they aren't usually compiled into the resulting library file, unless they are used in the library themself. (only really meaningful when you don't distribute the D source or when interoping with other languages) For types similarly, if you have any template instantiations inside your templated type, they will not be instantiated or compiled, until that template is used at least once in code anywhere. So: ```d struct Header() { Appender!string name; } ``` unless Appender!string is used anywhere else in the code, that Appender struct will never be initiated, thus no member functions will be emitted or compile time will be used, until you instantiate the Header struct once.
Dec 15 2021
prev sibling next sibling parent bauss <jj_1337 live.dk> writes:
On Wednesday, 15 December 2021 at 11:36:41 UTC, Manfred Nowak 
wrote:
 On Tuesday, 14 December 2021 at 08:28:01 UTC, WebFreak001 wrote:
 [...]
 Alternatively, remove the template `()` from your `struct 
 Header`
What is the semantic sense of a template having no parameters? Although the documentation declares such a template to be syntactically correct, not a single example suggests a meaning.
In this case, nothing. In other cases to only compile said "object" in specific conditions.
Dec 15 2021
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/15/21 6:36 AM, Manfred Nowak wrote:
 On Tuesday, 14 December 2021 at 08:28:01 UTC, WebFreak001 wrote:
 [...]
 Alternatively, remove the template `()` from your `struct Header`
What is the semantic sense of a template having no parameters? Although the documentation declares such a template to be syntactically correct, not a single example suggests a meaning.
It's just a template with no parameters, like a function with no parameters. It provides a separate namespace for items. A struct with empty template parameters is equivalent to: ```d template T() { struct T {} } ``` -Steve
Dec 15 2021
prev sibling parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Wednesday, 15 December 2021 at 11:36:41 UTC, Manfred Nowak 
wrote:
 On Tuesday, 14 December 2021 at 08:28:01 UTC, WebFreak001 wrote:
 [...]
 Alternatively, remove the template `()` from your `struct 
 Header`
What is the semantic sense of a template having no parameters? Although the documentation declares such a template to be syntactically correct, not a single example suggests a meaning.
To add to other responses, certain features are only available for templates, such as constraints and `auto ref` parameters.
Dec 15 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/15/21 9:41 AM, Stanislav Blinov wrote:
 On Wednesday, 15 December 2021 at 11:36:41 UTC, Manfred Nowak wrote:
 On Tuesday, 14 December 2021 at 08:28:01 UTC, WebFreak001 wrote:
 [...]
 Alternatively, remove the template `()` from your `struct Header`
What is the semantic sense of a template having no parameters? Although the documentation declares such a template to be syntactically correct, not a single example suggests a meaning.
To add to other responses, certain features are only available for templates, such as constraints and `auto ref` parameters.
Oh yeah, that reminds too -- a templated struct will have all member function attributes inferred. -Steve
Dec 15 2021