Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 18, 2010 Replacement for C++ Style Implicit casts? | ||||
---|---|---|---|---|
| ||||
Attachments:
| In C++ it is possible to declare a class as follows class Foo { Foo(int x) { } } You can then use that constructor to implicitly convert int to Foo. E.g Foo x = 0; //equivalent to Foo(0) Is there a way in D to do an implicit or explicit conversion from an integral type to a class? -Mike |
October 18, 2010 Re: Replacement for C++ Style Implicit casts? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Chaten | On Mon, 18 Oct 2010 15:47:40 -0400, Mike Chaten <mchaten@gmail.com> wrote:
> In C++ it is possible to declare a class as follows
> class Foo {
> Foo(int x) { }
> }
> You can then use that constructor to implicitly convert int to Foo. E.g
> Foo x = 0; //equivalent to Foo(0)
>
> Is there a way in D to do an implicit or explicit conversion from an
> integral type to a class?
explicit cast == opCast
implicit cast == alias this
Look up those two features in the docs.
-Steve
|
October 18, 2010 Re: Replacement for C++ Style Implicit casts? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer Attachments:
| I was under the impression that alias this just was shorthand for
Class Foo {
int x;
alias x this;
}
Foo foo = new Foo
foo = 9; // foo.x = 9
Foo Foo = 9 // null.x =9;
Also, for opCast, doesnt that only work for going from Foo to int and not the other way around?
-Mike
On Oct 18, 2010 3:55 PM, "Steven Schveighoffer" <schveiguy@yahoo.com> wrote:
> On Mon, 18 Oct 2010 15:47:40 -0400, Mike Chaten <mchaten@gmail.com> wrote:
>
>> In C++ it is possible to declare a class as follows
>> class Foo {
>> Foo(int x) { }
>> }
>> You can then use that constructor to implicitly convert int to Foo. E.g
>> Foo x = 0; //equivalent to Foo(0)
>>
>> Is there a way in D to do an implicit or explicit conversion from an integral type to a class?
>
> explicit cast == opCast
> implicit cast == alias this
>
> Look up those two features in the docs.
>
> -Steve
|
October 18, 2010 Re: Replacement for C++ Style Implicit casts? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Chaten | On Mon, 18 Oct 2010 23:47:40 +0400, Mike Chaten <mchaten@gmail.com> wrote:
> In C++ it is possible to declare a class as follows
> class Foo {
> Foo(int x) { }
> }
> You can then use that constructor to implicitly convert int to Foo. E.g
> Foo x = 0; //equivalent to Foo(0)
>
> Is there a way in D to do an implicit or explicit conversion from an
> integral type to a class?
>
> -Mike
For structs the following works:
struct Foo
{
this(int x) { ... }
}
Foo x = 0;
Classes are allocated on heap and as such there is no way to achieve the same.
|
October 18, 2010 Re: Replacement for C++ Style Implicit casts? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Chaten | Mike Chaten:
> In C++ it is possible to declare a class as follows
> class Foo {
> Foo(int x) { }
> }
> You can then use that constructor to implicitly convert int to Foo. E.g
> Foo x = 0; //equivalent to Foo(0)
>
> Is there a way in D to do an implicit or explicit conversion from an integral type to a class?
Do you mean something like this?
class Foo {
int x;
static Foo opCall(int x_) {
auto f = new Foo;
f.x = x_;
return f;
}
}
void main() {
Foo f = Foo(5);
assert(f.x == 5);
}
(With structs it's simpler)
Bye,
bearophile
|
October 18, 2010 Re: Replacement for C++ Style Implicit casts? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Chaten | Mike Chaten <mchaten@gmail.com> wrote: > I was under the impression that alias this just was shorthand for > Class Foo { > int x; > alias x this; > } > Foo foo = new Foo > foo = 9; // foo.x = 9 > Foo Foo = 9 // null.x =9; Not just. this would also work: int n = foo; // void bar( int n ) {} bar( foo ); > Also, for opCast, doesnt that only work for going from Foo to int and not > the other way around? Indeed. For implicit casts to Foo, I don't know what, if anything, works. void baz( Foo f ) {} baz( 3 ); // How? Likely, there is no such functionality in D, at least for the moment. -- Simen |
October 19, 2010 Re: Replacement for C++ Style Implicit casts? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas Attachments:
| Thanks for all your help!
-Mike
On Mon, Oct 18, 2010 at 6:21 PM, Simen kjaeraas <simen.kjaras@gmail.com>wrote:
> Mike Chaten <mchaten@gmail.com> wrote:
>
> I was under the impression that alias this just was shorthand for
>> Class Foo {
>> int x;
>> alias x this;
>> }
>> Foo foo = new Foo
>> foo = 9; // foo.x = 9
>> Foo Foo = 9 // null.x =9;
>>
>
> Not just. this would also work:
>
> int n = foo;
> // void bar( int n ) {}
> bar( foo );
>
>
>
> Also, for opCast, doesnt that only work for going from Foo to int and not
>> the other way around?
>>
>
> Indeed. For implicit casts to Foo, I don't know what, if anything, works.
>
> void baz( Foo f ) {}
> baz( 3 ); // How?
>
> Likely, there is no such functionality in D, at least for the moment.
>
> --
> Simen
>
|
Copyright © 1999-2021 by the D Language Foundation