digitalmars.D.learn - How to define property type to Array!struct?
- zoujiaqing (27/27) Dec 14 2021 My code:
- WebFreak001 (8/35) Dec 14 2021 the problem is that your header is a template, so you need to
- Manfred Nowak (5/7) Dec 15 2021 What is the semantic sense of a template having no parameters?
- WebFreak001 (25/32) Dec 15 2021 For functions there is a use-case: they will not be
- bauss (5/12) Dec 15 2021 In this case, nothing.
- Steven Schveighoffer (11/19) Dec 15 2021 It's just a template with no parameters, like a function with no
- Stanislav Blinov (4/11) Dec 15 2021 To add to other responses, certain features are only available
- Steven Schveighoffer (4/16) Dec 15 2021 Oh yeah, that reminds too -- a templated struct will have all member
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
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
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
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: [...]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.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
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: [...]In this case, nothing. In other cases to only compile said "object" in specific conditions.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
On 12/15/21 6:36 AM, Manfred Nowak wrote:On Tuesday, 14 December 2021 at 08:28:01 UTC, WebFreak001 wrote: [...]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 {} } ``` -SteveAlternatively, 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
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: [...]To add to other responses, certain features are only available for templates, such as constraints and `auto ref` parameters.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
On 12/15/21 9:41 AM, Stanislav Blinov wrote:On Wednesday, 15 December 2021 at 11:36:41 UTC, Manfred Nowak wrote:Oh yeah, that reminds too -- a templated struct will have all member function attributes inferred. -SteveOn Tuesday, 14 December 2021 at 08:28:01 UTC, WebFreak001 wrote: [...]To add to other responses, certain features are only available for templates, such as constraints and `auto ref` parameters.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