Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
December 31, 2005 Automatic Casting | ||||
---|---|---|---|---|
| ||||
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 |
December 31, 2005 Re: Automatic Casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trevor Parscal | 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
|
December 31, 2005 Alternative Syntax for opCall (was: Automatic Casting) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trevor Parscal | 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
|
December 31, 2005 Re: Automatic Casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trevor Parscal | 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 Parscal
I'd with the proper OO thing and use:
# foo.assign( 5 );
|
Copyright © 1999-2021 by the D Language Foundation