November 29, 2016
On 11/29/2016 02:21 AM, Basile B. wrote:
> The cast from a class type to a sub class in itself does absolutely
> nothing.

That can't be right. A bad downcast gives you null, so it has to check the dynamic type information. Compare with upcasts which are statically known to be correct, so they don't need to check anything at runtime.
November 29, 2016
On Monday, 28 November 2016 at 11:26:41 UTC, dm wrote:
> ```
> abstract class MyClass(T)
> {
>   public:
>    @property const(T) value(){return _value;}
>    @property void value(T val){_value = val;}
> ...
>   private:
>    T _value;
> ...
> }

To avoid having to use the Object class directly you can make an base class of the class template.
Like:

```
abstract class MyClass {}
abstract class MyClassImpl(T)
{
public:
    @property const(T) value(){return _value;}
    @property void value(T val){_value = val;}
 ...
   private:
    T _value;
 ...
}

MyClassInt and float inherits from MyClassImpl
```

And use it like:

```
void main() {
   MyClass[] objs;
   objs ~= new MyClassFloat();
   objs ~= new MyClassInt();
}
```

November 29, 2016
On Tuesday, 29 November 2016 at 15:56:23 UTC, Jerry wrote:
> abstract class MyClass {}
> abstract class MyClassImpl(T)


Oops, forgot MyClassImpl should extend from MyClass.

abstract class MyClassImpl(T) : MyClass {
...
}

November 30, 2016
On Tuesday, 29 November 2016 at 09:58:16 UTC, ag0aep6g wrote:
> On 11/29/2016 02:21 AM, Basile B. wrote:
>> The cast from a class type to a sub class in itself does absolutely
>> nothing.
>
> That can't be right. A bad downcast gives you null, so it has to check the dynamic type information. Compare with upcasts which are statically known to be correct, so they don't need to check anything at runtime.

Usually casts to base classes can be determined if they're valid at compile-time.

Take this for an example:

class Foo {
}

class Bar : Foo {
}

void main() {
    auto bar = new Bar;

    auto foo = cast(Foo)bar; // The compiler should know that bar is of type Bar, which is a subclass of Foo and thus the cast theoretically is redundant.
}

Even in a situation like this, the compiler should be able to see if the cast could ever be invalid during compile-time determined by calls to fun.

void fun(Cast)(Bar bar) {
    return cast(Cast)bar; // If Cast is Foo then the compiler should know the cast is redundant.
]

I don't know if the D compiler actually takes such situation into account, but I'd assume it does some kind of optimization in regards of that.
November 30, 2016
On Tuesday, 29 November 2016 at 15:56:23 UTC, Jerry wrote:
> On Monday, 28 November 2016 at 11:26:41 UTC, dm wrote:
>> ```
>> abstract class MyClass(T)
>> {
>>   public:
>>    @property const(T) value(){return _value;}
>>    @property void value(T val){_value = val;}
>> ...
>>   private:
>>    T _value;
>> ...
>> }
>
> To avoid having to use the Object class directly you can make an base class of the class template.
> Like:
>
> ```
> abstract class MyClass {}
> abstract class MyClassImpl(T)
> {
> public:
>     @property const(T) value(){return _value;}
>     @property void value(T val){_value = val;}
>  ...
>    private:
>     T _value;
>  ...
> }
>
> MyClassInt and float inherits from MyClassImpl
> ```
>
> And use it like:
>
> ```
> void main() {
>    MyClass[] objs;
>    objs ~= new MyClassFloat();
>    objs ~= new MyClassInt();
> }
> ```

I would rather go with an interface than a base class.


November 30, 2016
On 11/30/2016 10:42 AM, Bauss wrote:
> Usually casts to base classes can be determined if they're valid at
> compile-time.

Yeah, that's what I said. A cast to a base class is an "upcast". Upcasts don't need run-time checks. The other direction (cast to more derived class) is a downcast. Downcasts need run-time checks.
November 30, 2016
On Wednesday, 30 November 2016 at 14:53:21 UTC, ag0aep6g wrote:
> On 11/30/2016 10:42 AM, Bauss wrote:
>> Usually casts to base classes can be determined if they're valid at
>> compile-time.
>
> Yeah, that's what I said. A cast to a base class is an "upcast". Upcasts don't need run-time checks. The other direction (cast to more derived class) is a downcast. Downcasts need run-time checks.

Actually I've always called an "upcast" a "downcast" ! This incredible misconception explains why you had to correct me after my yesterday's answer.
December 01, 2016
On Tuesday, 29 November 2016 at 15:56:23 UTC, Jerry wrote:
> To avoid having to use the Object class directly you can make an base class of the class template.
> Like:
>
> ```
> abstract class MyClass {}
> abstract class MyClassImpl(T)
> {
> public:
>     @property const(T) value(){return _value;}
>     @property void value(T val){_value = val;}
>  ...
>    private:
>     T _value;
>  ...
> }
>
> MyClassInt and float inherits from MyClassImpl
> ```
>
> And use it like:
>
> ```
> void main() {
>    MyClass[] objs;
>    objs ~= new MyClassFloat();
>    objs ~= new MyClassInt();
> }
> ```

Yes, but anyway you need to downcast if(MyClassBlahBlah subclass = cast(MyClassBlahBlah)obj)...
So it's not much sense to have base class or interface MyClass.
1 2
Next ›   Last »