To see the difference add this snippet to your code:
    
    
    var calendar: BusinessCalendar
    calendar = new TargetCalendar
    echo calendar.isBusinessDay(getTime())  # prints `true` always, 
`BusinessCalendar.isBusinessDay` invoked
    calendar = new NullCalendar
    echo calendar.isBusinessDay(getTime())  # prints `true` with procedures and 
`false` with methods
    

Try it with `BusinessCalendar` declared as `proc` and as `method`.

Using `proc` you'll always call `BusinessCalendar.isBusinessDay`, because 
that's the type of the variable, and what to call is determined statically, 
that is at compilation time, exactly by the type of the variable.

Using methods what to call is determined at runtime, dynamically, by the actual 
type of object referenced by the variable. The first time it's 
`TargetCalendar`, which doesn't have its own method, so its base type 
(`BusinessCalendar`) method is called. The second time it's `BusinessCalendar`, 
which has its own method, which is called.

That is, if your objects are always assigned to variables of their own type and 
passed in arguments of their own type, you have no benefit of using methods 
(the same result and a little runtime overhead for methods). If actual type of 
the same variable may differ and at the same time should call its own, most 
specific, routine, then you need methods.

Reply via email to