[Issue 5931] New: keyword new insists on calling a struct's (non-zero argument) constructor--in other words, it won't allow default initialization; also postblit this(this) constructor dosen't get called from "new <structname>(<struct_instance>)"
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu May 5 12:12:32 PDT 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5931
Summary: keyword new insists on calling a struct's (non-zero
argument) constructor--in other words, it won't allow
default initialization; also postblit this(this)
constructor dosen't get called from "new
<structname>(<struct_instance>)"
Product: D
Version: D2
Platform: x86
OS/Version: Mac OS X
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: ultimatemacfanatic at gmail.com
--- Comment #0 from Christopher the Magnificent <ultimatemacfanatic at gmail.com> 2011-05-05 12:08:38 PDT ---
So I need to do this:
class Tokenizer
{
File file;
this(File f)
{
File f2; // default initialized
// problem 1
this.file = new File; // should pointer to return default initialized
File struct but compiler complains
*this.file = f;
// problem 2
this.file = new File(f); // this doesn't work either, notwithstanding
File has a this(this) post-blitter!
}
}
unfortunately, on the line where I make the call to "new File", it gives me
this error:
Error: constructor std.stdio.File.this (string name, in const(char[])
stdioOpenmode = "rb") is not callable using argument types ()
Shouldn't I be able to allocate a default-initialized struct? Or if not,
shouldn't it be allow to have empty-argument struct constructors?
http://www.digitalmars.com/d/2.0/struct.html says empty parameter-list
constructors are not allowed for structs, but why not? Can someone explain
this language decision to me and make a good argument for why it should not be
changed?
when I try "new File(f)" then I get this error message:
tokenizer.d(199): Error: constructor std.stdio.File.this (string name, in
const(char[]) stdioOpenmode = "rb") is not callable using argument types (File)
tokenizer.d(199): Error: cannot implicitly convert expression (file) of type
File to string
It seems to me that the post-blit constructor should be called automatically
when a struct is "function-called" with an instance of the same struct as the
sole parameter like "new S(s_instance)" Don't you agree? You may think this
is a feature request, but when functionality is a departure from what one would
expect, I would tend to call that a bug instead. :-)
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list