Thread overview
Type converter from build in to user type
Nov 30, 2012
js.mdnq
Nov 30, 2012
Jonathan M Davis
Nov 30, 2012
jerro
Nov 30, 2012
Ali Çehreli
Nov 30, 2012
js.mdnq
November 30, 2012
I have a struct I am trying convert from int's to the type. Since I can't add a opCast overload to an int I don't know how to do it. My opCast convertors in my class do not work for the assignment operator:


class myType
{
   opCast, opAssign
}

mytype = 3;

Error that we can't convert an int to myType. While it's very easy to go from a myType to whatever by writing a cast for it(in the class), how do I go from an int to myType(in the class)?

For example, I can do

mytype = mytype.opCast(3);
or mytype.opAssign(3);

but these are too verbose to be useful.


November 30, 2012
On Friday, November 30, 2012 03:59:05 js.mdnq wrote:
> I have a struct I am trying convert from int's to the type. Since I can't add a opCast overload to an int I don't know how to do it. My opCast convertors in my class do not work for the assignment operator:
> 
> 
> class myType
> {
> opCast, opAssign
> }
> 
> mytype = 3;
> 
> Error that we can't convert an int to myType. While it's very
> easy to go from a myType to whatever by writing a cast for it(in
> the class), how do I go from an int to myType(in the class)?
> 
> For example, I can do
> 
> mytype = mytype.opCast(3);
> or mytype.opAssign(3);
> 
> but these are too verbose to be useful.

You can put a constructor on your type which takes an int and then use std.conv.to instead of casting. It's generally preferred to use std.conv.to anyway, since it's generally safer (since it allows fewer (no?) unsafe casts and checks conversions in some cases - e.g. whether the value in the int being cast to ubyte will fit in a ubyte). It will also use your opCast if you define one, so you can then use std.conv.to in both directions.

- Jonathan M Davis
November 30, 2012
On Friday, 30 November 2012 at 02:59:06 UTC, js.mdnq wrote:
> I have a struct I am trying convert from int's to the type. Since I can't add a opCast overload to an int I don't know how to do it. My opCast convertors in my class do not work for the assignment operator:
>
>
> class myType
> {
>    opCast, opAssign
> }
>
> mytype = 3;
>
> Error that we can't convert an int to myType. While it's very easy to go from a myType to whatever by writing a cast for it(in the class), how do I go from an int to myType(in the class)?
>
> For example, I can do
>
> mytype = mytype.opCast(3);
> or mytype.opAssign(3);
>
> but these are too verbose to be useful.

Why can't you use opAssign? This works fine:

class A
{
    void opAssign(int i)
    {
        writeln(i);
    }
}

auto a = new A();
a = 1;                 // prints 1

November 30, 2012
On 11/29/2012 07:24 PM, jerro wrote:
> On Friday, 30 November 2012 at 02:59:06 UTC, js.mdnq wrote:
>> I have a struct I am trying convert from int's to the type. Since I
>> can't add a opCast overload to an int I don't know how to do it. My
>> opCast convertors in my class do not work for the assignment operator:
>>
>>
>> class myType
>> {
>> opCast, opAssign
>> }
>>
>> mytype = 3;
>>
>> Error that we can't convert an int to myType. While it's very easy to
>> go from a myType to whatever by writing a cast for it(in the class),
>> how do I go from an int to myType(in the class)?
>>
>> For example, I can do
>>
>> mytype = mytype.opCast(3);
>> or mytype.opAssign(3);
>>
>> but these are too verbose to be useful.
>
> Why can't you use opAssign? This works fine:
>
> class A
> {
> void opAssign(int i)
> {
> writeln(i);
> }
> }
>
> auto a = new A();
> a = 1; // prints 1
>

A complete example:

import std.conv;

class C
{
    int i;

    this(int i)
    {
        this.i = i;
    }

    C opAssign(int i)
    {
        this.i = i;
        return this;
    }

    int opCast(T : int)() const
    {
        return i;
    }
}

void main()
{
    auto c = new C(42);
    c = 43;
    auto i = to!int(c);
    assert(i == 43);
}

Ali
November 30, 2012
On Friday, 30 November 2012 at 03:40:31 UTC, Ali Çehreli wrote:
> On 11/29/2012 07:24 PM, jerro wrote:
>> On Friday, 30 November 2012 at 02:59:06 UTC, js.mdnq wrote:
>>> I have a struct I am trying convert from int's to the type. Since I
>>> can't add a opCast overload to an int I don't know how to do it. My
>>> opCast convertors in my class do not work for the assignment operator:
>>>
>>>
>>> class myType
>>> {
>>> opCast, opAssign
>>> }
>>>
>>> mytype = 3;
>>>
>>> Error that we can't convert an int to myType. While it's very easy to
>>> go from a myType to whatever by writing a cast for it(in the class),
>>> how do I go from an int to myType(in the class)?
>>>
>>> For example, I can do
>>>
>>> mytype = mytype.opCast(3);
>>> or mytype.opAssign(3);
>>>
>>> but these are too verbose to be useful.
>>
>> Why can't you use opAssign? This works fine:
>>
>> class A
>> {
>> void opAssign(int i)
>> {
>> writeln(i);
>> }
>> }
>>
>> auto a = new A();
>> a = 1; // prints 1
>>
>
> A complete example:
>
> import std.conv;
>
> class C
> {
>     int i;
>
>     this(int i)
>     {
>         this.i = i;
>     }
>
>     C opAssign(int i)
>     {
>         this.i = i;
>         return this;
>     }
>
>     int opCast(T : int)() const
>     {
>         return i;
>     }
> }
>
> void main()
> {
>     auto c = new C(42);
>     c = 43;
>     auto i = to!int(c);
>     assert(i == 43);
> }
>
> Ali

Thanks, I was using a generic opCast which was causing the problem. It must have gotten that way from other mistakes I was probably making.

C!T opAssign(T i) rather than C!T opAssign(T)(T i)

It thought the D compiler would automatically deduce the second case but I guess it's redundant.