Hello all,

I can't seem to find an answer to this one, and I'm a little stumped. So I'm 
reaching out to get a better understanding as to what is happening here. So I'm 
building a small project which relies on OOP in Nim, and specifically the 
inheritance aspect.

So take for example, I have the following classes/interfaces/what-have-you:
    
    
    type
      Application = ref object of RootObj
        resources*: seq[Resource]
    
    
    Run
    
    
    type
      Resource = ref object of RootObj
        things*: seq[string]
    
    method consume*(self: Resource) {.base.} = echo("Consumed!")
    
    
    Run

Let's say the `Resource` parent has multiple base methods with default 
implementations that can be overridden. And say I have the following 
class/interface/what-have-you that inherits from `Resource`.
    
    
    type
      FoodResource = ref object of Resource
    
    method consume*(self: FoodResource) = echo("Eaten!")
    
    
    Run

So here is the start of the weird part that I don't understand. When I 
instantiate an `Application`, and assign an instance of `FoodResource` to its 
property `resources`, it will throw a type error... which I guess is fair, 
given that `FoodResource` really isn't type `Resource`.
    
    
    let food = FoodResource(
      things: @["Apple", "Orange", "Pear"]
    )
    
    let app = Application(
      resources: @[food]
    )
    
    
    Run

But when I create another class/interface/what-have-you that inherits from 
`Resource` and assign an instance of that together with the instance of 
`FoodResource` to `resources` property of the instance of `Application`, it 
works.
    
    
    type
      FuelResource = ref object of Resource
    
    method consume*(self: FuelResource) = echo("Guzzled!")
    
    
    Run
    
    
    let food = FoodResource(
      things: @["Apple", "Orange", "Pear"]
    )
    
    let fuel = FuelResource(
      things: @["Shell", "BP"]
    )
    
    let app = Application(
      resources: @[food, fuel]
    )
    
    
    Run

So my question is, how does the shorthand initialisation of a sequence infer 
it's internal types when mixed with classes/interfaces/what-have-you that 
inherit from a parent? Why does it enforce the parent type `Resource` when 
there is only one item in the sequence but will allow for inherited types when 
there is multiple items in the sequence? I do hope this all makes sense.

Reply via email to