www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Type inference error with DI

reply Salih Dincer <salihdb hotmail.com> writes:
```d
interface ICon
{
   string Str();
}

class Foo : ICon
{
   string Str() {
     return "Foo";
   }
}

class Bar : ICon
{
   string Str() {
     return "Bar";
   }
}

import std.stdio;

void main()
{
   auto foo = new Foo();
   ICon bar = new Bar();
   /* replace ICon with auto,
    * please...*/
   auto test =
   [
     foo, bar
   ];
   foreach(t; test)
   {
     t.Str.writeln;
   }
}
```

It is thrown an error* because of type object.Object[].

(*) DIsampleFoo.d(34): Error: no property `Str` for type 
`object.Object`
Sep 27 2021
parent Dukc <ajieskola gmail.com> writes:
On Monday, 27 September 2021 at 19:36:04 UTC, Salih Dincer wrote:
 It is thrown an error* because of type object.Object[].

 (*) DIsampleFoo.d(34): Error: no property `Str` for type 
 `object.Object`
I don't think D will automatically use a common interface as a base type. Either change `auto test` to `ICon[] test` or make `ICon` an abstract class, and I think it'll work.
Sep 27 2021