Re: Structs cannot contain explicit parameterless constructors

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Nick Hounsome <News@xxxxxxxxxxxxxxxxxx> wrote:

I don't know what the real reason is but it may have something
to do with version compatibility - Normally you can replace a
dll with a new version using the same method signatures and
everything will work OK but this would not be possible with
default constructors because the compiler does not actually
call a default constructor - it just makes the memory 0 for
efficiency reasons - consequently, If you added a default
constructor it would not be called unless you recompiled.

Makes some sort of sense, though I'm not at ease with
this kind of deep-tech implementation detail having too
much influence on the language design.

Regarding the original post - I think that the design is
misguided

Original poster disagrees, but this might be a matter of
slightly different programming styles, yours perhaps
being tuned by experience with C# to stay away from
its weak spots, while I - as a functional programming
freak involved with language desing - have trained
myself to question the language.

- If you need a unique identity in a class then
assign it in the class (or better a base class)
You can use an IdentityStamp class to get it if
you want using a static method [eg this.id =
IdentityStamp.GetNext();]

That's what I did, to get things runnig, not because I
deemed it reasonable.

Assuming that you could do what the OP wanted
it would not behave as expected:

// IdentityStamp.seed == 0
Obj obj = new Obj(); // class usng IdentityStamp
IdentityStamp id = obj.Id;
Foo(id); // void Foo(IdentityStamp id)
// IdentityStamp.seed is now at least 3.

This would create a new stamp then overwrite it
with the one from obj which is probably not what
you intended.

C++ gets around this with copy constructors (even
the line that looks like assignment!). In C# there is
no copy constructor to fix the problem for you.

This still seems a bit obscure.

Are you implying that the line

IdentityStamp id = obj.Id;

is treated as being equivalent of

IdentityStamp id = new IdentityStamp();
id = obj.Id;

and

Foo(id)

is treated as something like:

Foo(new IdentityStamp() = id)

???

Considering how C# handles classes, I find these
implicit constructor calls quite unnecessary - why
can't the compiler just zero the struct and flag it as
uninitialized, just as a class ref would be nulled and
flagged uninitialized?

Is this because the clr uses default constructors to
ensure floats are properly zeroed or something like
that?

Regards/Ole N.


.



Relevant Pages

  • Re: Structs cannot contain explicit parameterless constructors
    ... I don't know what the real reason is but it may have something ... constructor it would not be called unless you recompiled. ... much influence on the language design. ... IdentityStamp id = obj.Id; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Structs cannot contain explicit parameterless constructors
    ... I'd be less prone to make this error if I knew the reason. ... public IdentityStamp() ... parameterless constructor. ... The reason is that, for a value type, compilers ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Implicit object constructor misinterpretation
    ... specified for the language that is being parsed. ... sub-set of the syntax for object literals. ... document that will teach anyone how to program with javascript. ... Consider the issue in your OP; you can speculate about "broken switch-case" or "not a constructor but a block of statements", but an examination of the spec shows that the observed behaviour corresponds with the specified behaviour, and explains the specified behaviour. ...
    (comp.lang.javascript)
  • Re: Is C# any good?
    ... "Neelakantan Krishnaswami" escreveu na mensagem ... > idea stolen from John Reppy's Moby language. ... > new syntactic class for constructor functions, ... You can write field> initializer expressions in the constructor, and the compiler does an> easy check that all of the fields in the class get initialized, or it> rejects the constructor. ...
    (comp.programming)
  • Re: Verbose and flexible args and kwargs syntax
    ... possible meaning for "equals" in the context of a programing language. ... That python is not the only language to not get this quite as right as ... this constructor for me', any more than **kwargs says 'please call a ... I am talking about type constraints, but as seems to be the usual ...
    (comp.lang.python)