View Issue Details

IDProjectCategoryView StatusLast Update
0000645LDMud 3.3LPC Languagepublic2011-02-23 22:02
Reporter_xtian_ Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Product Version3.3.717 
Target Version3.3.721 
Summary0000645: casting structs (to 'inherited struct') does nothing
DescriptionLPC and the mysteries of casting ...

struct a {};
struct b (a) {
  int test;
};

printf("%O", (struct a) () );

... shows that the cast "(struct a)" on the struct "()" does nothing: It keeps the struct type, even preserving the additional element test.

to_struct() is also useless for this and I will make another entry for that.

So there is no way to cast one struct to another struct of different type, even if these are related.
Additional InformationNote that:

to_struct( mkmapping( () ), (<a>) );

does work as expected. (But it would work on every two struct definitions that have common element names and thus has not a bit of typesafety, which is one of the good reasons to use structs in the first place)
TagsNo tags attached.

Relationships

related to 0000646 resolvedzesstra LDMud 3.3 to_struct( STRUCT ) does nothing but could cast to other template 
related to 0000250 resolvedGnomi LDMud 3.6 Casts should just check types, not convert them 

Activities

zesstra

2009-06-01 05:34

administrator   ~0001175

Well...
(type) often does nothing. ;-)
e.g.
closure cl=function void (int i) {int b=(int)i; printf("%O\n",b);};
funcall(cl, 4.5);
prints 4.5 instead of 4.
A similar issue is described in 0000072.
Casting in LPC mostly assures the compiler "I know, that the value coming now is of type x" and is mostly useless, if you actually want to convert something.

Gnomi

2009-06-01 06:57

manager   ~0001180

Declarative castings are done with ({int}). (int)x should cast x into an integer unless the compiler knows that x already is an integer. In xtian's example the compiler knows that the value is of struct b and therefore should insert something like to_struct. But currently ignores any typecasts to struct, even (<struct a>)10 is legal.

_xtian_

2009-06-02 08:01

reporter   ~0001183

This should probably also work (think typedef):

struct d {
  // ... any elements ...
};
struct e (d) {
 // empty!
};

... = (struct d) (<e>);

As above this does nothing. But since e doesnt add new elements to d, it is clear what the cast should do: It only needs to change the struct type.

zesstra

2009-06-02 08:33

administrator   ~0001184

Structs may be a special case, that they are never converted, while other types are converted if the compiler already knows their value. But it does not change much in respect to the usability of casts.

Actually, I would even like to either fix the casts to insert to_*() in all cases or remove them altogether, because the inconsistency is really bad for most wizards. We usually give the recommendation to never use (type) but always to_type().

_xtian_

2009-06-02 09:19

reporter   ~0001185

Then there could still be a declarative cast for the last example. Atm ({ }) casts only work for a very selected few types. It could work for this last case, since it is trivial.

Issue History

Date Modified Username Field Change
2009-06-01 04:15 _xtian_ New Issue
2009-06-01 05:34 zesstra Note Added: 0001175
2009-06-01 05:38 zesstra Project LDMud => LDMud 3.3
2009-06-01 06:57 Gnomi Note Added: 0001180
2009-06-02 08:01 _xtian_ Note Added: 0001183
2009-06-02 08:33 zesstra Note Added: 0001184
2009-06-02 09:19 _xtian_ Note Added: 0001185
2009-11-05 14:06 zesstra Relationship added related to 0000250
2009-11-05 14:08 zesstra Relationship added related to 0000646
2011-02-23 22:02 zesstra Target Version => 3.3.721