Hi,
I have the problem, that I want to always construct a struct with a
parameter. To make this more comfortable (e.g. provide a default
parameter I have a factory function to create this struct).
struct S {
int i;
this(int i_) { i = i_; }
}
S createS(int i=5) {
return S(i);
}
My ques
You can hide the constructor from external code by using "private
this()". And you can disable the default constructor via @disable
this();
You may also wish to make your fields private as well.
You don't necessarily have to make createS() a free function, you can
make it a static function inside
On 09/26/2011 10:55 PM, Christian Köstlin wrote:
Hi,
I have the problem, that I want to always construct a struct with a
parameter. To make this more comfortable (e.g. provide a default
parameter I have a factory function to create this struct).
struct S {
int i;
this(int i_) { i = i_; }
}
S c
On 9/26/11, Andrej Mitrovic wrote:
> create() will not occupy any space in any of the S instances.
Sorry for that stupid comment. struct methods don't occupy space in
struct instances regardless if they're static or not.
On 9/26/11 23:18 , Andrej Mitrovic wrote:
You can hide the constructor from external code by using "private
this()". And you can disable the default constructor via @disable
this();
You may also wish to make your fields private as well.
You don't necessarily have to make createS() a free functio
On 09/26/2011 11:15 PM, Timon Gehr wrote:
On 09/26/2011 10:55 PM, Christian Köstlin wrote:
Hi,
I have the problem, that I want to always construct a struct with a
parameter. To make this more comfortable (e.g. provide a default
parameter I have a factory function to create this struct).
struct
In the thread on compiling DSSS and build tools someone mentioned
a format for initializing environment data of a struct:
Environment env = {
tests: true,
verbose: true,
importDirs: ["../deimos"]
}
A followup comment says "isn't that syntax intended to be
deprecated?"
I have a struct where I need to perform default initialization of
some members but the compiler doesn't allow to define a default
constructor which allow optional arguments.
struct Foo(T)
{
private int _bar;
this(int bar = 1)
{
this._bar = bar;
}
}
auto foo = Foo!(stri
Somehow this code works for me:
```D
auto error (int status, string description){
struct Error {
int status;
string description;
}
Error err = {
status,
description
};
return err.serializeToJson;
}
```
which is supposed to be the same as
```D
On Saturday, 1 December 2012 at 18:36:35 UTC, Dan wrote:
In the thread on compiling DSSS and build tools someone
mentioned a format for initializing environment data of a
struct:
Environment env = {
tests: true,
verbose: true,
importDirs: ["../deimos"]
}
A followup co
On Saturday, December 01, 2012 19:36:34 Dan wrote:
> In the thread on compiling DSSS and build tools someone mentioned
> a format for initializing environment data of a struct:
>
>Environment env = {
> tests: true,
> verbose: true,
> importDirs: ["../deimos"]
>}
>
On Sunday, 2 December 2012 at 01:50:08 UTC, Jonathan M Davis
wrote:
On Saturday, December 01, 2012 19:36:34 Dan wrote:
That syntax is from C. There was definitely a push to deprecate
it, and
personally I definitely think that it should go, but I don't
recall that it was
definitively decided t
On Friday, 17 June 2016 at 10:50:55 UTC, Gary Willoughby wrote:
I have a struct where I need to perform default initialization
of some members but the compiler doesn't allow to define a
default constructor which allow optional arguments.
struct Foo(T)
{
private int _bar;
this(int bar
On Friday, 17 June 2016 at 10:53:40 UTC, Lodovico Giaretta wrote:
struct Foo(T)
{
private int _bar = 1;
this(int bar)
{
this._bar = bar;
}
}
auto foo = Foo!(string)();
This should do the trick.
Thanks, I forgot to mention I'm also doing lots of other stuff in
the con
The Factory-Pattern would be a good idea.
On Friday, 17 June 2016 at 10:50:55 UTC, Gary Willoughby wrote:
I have a struct where I need to perform default initialization
of some members but the compiler doesn't allow to define a
default constructor which allow optional arguments.
This is a fairly recent change (2.068->2.069 or 2.070),
On Friday, 17 June 2016 at 11:10:12 UTC, Gary Willoughby wrote:
Thanks, I forgot to mention I'm also doing lots of other stuff
in the constructor to private fields too.
struct Foo(T)
{
private int _bar;
private void* _baz;
this(int bar = 8)
{
this._bar = bar;
th
On Friday, 17 June 2016 at 12:31:33 UTC, David Nadlinger wrote:
Structs cannot have a default constructor; .init is required to
be a valid state (unless you @disable default construction).
Except for nested structs :) They have the default constructor
and their .init is not a valid state: has
On Thursday, 1 June 2017 at 12:04:05 UTC, Daniel Tan Fook Hao
wrote:
If I'm reading this right, in the former, the struct is created
when the function is called in run-time, and the type is then
inferred after that? I don't really understand the behavior
behind this.
The only difference betw
On Thursday, 1 June 2017 at 12:04:05 UTC, Daniel Tan Fook Hao
wrote:
Somehow this code works for me:
```D
auto error (int status, string description){
struct Error {
int status;
string description;
}
Error err = {
status,
description
};
return
On Thursday, 1 June 2017 at 12:04:05 UTC, Daniel Tan Fook Hao
wrote:
Somehow this code works for me:
```D
auto error (int status, string description){
struct Error {
int status;
string description;
}
Error err = {
status,
description
};
return
I'm so sorry, I know that the above example doesn't work:
```d
main(){
Struct PersonDTO {
string name;
string surname;
}
void create(PersonDTO personData){
// ...
}
create( {name: "Peter", surname: "Ustinov"} );
}
```
-Is it there any alternative to initialize "inline" d
On Wednesday, 22 June 2022 at 09:41:55 UTC, Antonio wrote:
I'm so sorry, I know that the above example doesn't work:
```d
main(){
Struct PersonDTO {
string name;
string surname;
}
void create(PersonDTO personData){
// ...
}
create( {name: "Peter", surname: "Ustinov"} );
}
On Wednesday, 22 June 2022 at 11:19:59 UTC, Antonio wrote:
I see now: DIP 1033 will solve this (i.e., using named
arguments in struct constructor... similar to how dart/flutter
works)
That would be DIP 1030:
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1030.md
Max Haughton
24 matches
Mail list logo