digitalmars.D - Automatic Casting
- Trevor Parscal (15/15) Dec 30 2005 I am working on a class that will interact with other numeric types. I w...
- Unknown W. Brackets (5/24) Dec 30 2005 The += thing is doable, but not =. This is called implicit casting, and...
- Manfred Nowak (45/47) Dec 31 2005 [...]
- Hasan Aljudy (3/22) Dec 31 2005 I'd with the proper OO thing and use:
I am working on a class that will interact with other numeric types. I want to not have to manually class things all the time, and also want to be able to use the class directly instead of a subfunction of the class... Like this.. class FOO { ..? } FOO foo = new FOO(); foo = 5; // See how I can just give a numeric value to it float bar = 5; foo += bar; // And interact with other types // foo now equals 10 Is this possible? I guess I want to make an int-like class. How do you do this? Thanks, Trevor Parscal
Dec 30 2005
The += thing is doable, but not =. This is called implicit casting, and can cause confusing errors when misused. Nonetheless, such things are sometimes very desirable, so it can be a conflicted issue many people argue over. -[Unknown]I am working on a class that will interact with other numeric types. I want to not have to manually class things all the time, and also want to be able to use the class directly instead of a subfunction of the class... Like this.. class FOO { ..? } FOO foo = new FOO(); foo = 5; // See how I can just give a numeric value to it float bar = 5; foo += bar; // And interact with other types // foo now equals 10 Is this possible? I guess I want to make an int-like class. How do you do this? Thanks, Trevor Parscal
Dec 30 2005
Trevor Parscal wrote: [...]FOO foo = new FOO(); foo = 5; // See how I can just give a numeric value to it[...] We all know that Walter is against overloading the `=´ operator. Therefore this is semantically wrong. When seeing this I would implement it as an opCall: foo(5); Therebye reinterpreting the OpCall as an assignment. Then Foo foo, bar; // snip foo(bar); // is an assignment of values This is different to foo= bar; // is an assignment of pointers. You cannot have both, when you in fact write `foo( bar)' as `foo= bar' So it seems to be possible 1) to have one xor the other depending on whether the magical opCall `Foo opCall( Foo <id>)' is declared. If it is declared, then the form `foo( ... )' is semantically disallowed and all calls of every `opCall' must have the form `foo= ...'. If it is not declared vice versa. 2) to have both, if we 2a) allow an unnamed attribute to a class. An unnamed attribute is represented by no name ;-), i.e. foo. = bar; The unnamed attribute represents the call of an `opCall', i.e. foo. = bar; is equivalent to foo( bar); is equivalent to foo.opCall( bar); Because expressions like foo. . . = bar; seems to have no sense, this leads to the 3rd alternative: 2b) create a new operator `.=' according to the above, where every foo .= bar; is equivalent to foo( bar); is equivalent to foo.opCall( bar); -manfred
Dec 31 2005
Trevor Parscal wrote:I am working on a class that will interact with other numeric types. I want to not have to manually class things all the time, and also want to be able to use the class directly instead of a subfunction of the class... Like this.. class FOO { ..? } FOO foo = new FOO(); foo = 5; // See how I can just give a numeric value to it float bar = 5; foo += bar; // And interact with other types // foo now equals 10 Is this possible? I guess I want to make an int-like class. How do you do this? Thanks, Trevor ParscalI'd with the proper OO thing and use:
Dec 31 2005