View Issue Details
ID | Project | Category | View Status | Date Submitted | Last Update |
---|---|---|---|---|---|
0000645 | LDMud 3.3 | LPC Language | public | 2009-06-01 04:15 | 2011-02-23 22:02 |
Reporter | _xtian_ | Assigned To | |||
Priority | normal | Severity | minor | Reproducibility | always |
Status | new | Resolution | open | ||
Product Version | 3.3.717 | ||||
Target Version | 3.3.721 | ||||
Summary | 0000645: casting structs (to 'inherited struct') does nothing | ||||
Description | LPC 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 Information | Note 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) | ||||
Tags | No tags attached. | ||||
|
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. |
|
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. |
|
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. |
|
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(). |
|
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. |
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 |