derived class as argument for base class and other rules for inheritance

About Monkey 2 Forums Monkey 2 Programming Help derived class as argument for base class and other rules for inheritance

This topic contains 14 replies, has 7 voices, and was last updated by  Mark Sibly 1 year, 2 months ago.

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #13020

    abakobo
    Participant

    in the following code I can give a derived class as parameter for an argument defined as the base class. The same is true for assignation. Is it expect behaviour?
    If I cast a derived class from the base one I can access the derived method but not the Base one!
    What may I and what shouldn’t I do. What are the rules with these inheritance things?

    #13035

    nerobot
    Participant

    Probably, it’s a lang bug. The code looks correct.

    #13041

    Simon Armstrong
    Participant

    Shouldn’t the downcasting on line 26 be an error?

    #13043

    Mark Sibly
    Keymaster

    Look all right to me. Pulling out the noise, code is:

    Shouldn’t the downcasting on line 26 be an error?

    Don’t think so. B is a subclass of A, so Cast<B>( a ) is semantically correct (ie: compilable) if ‘a’ is of class A (or any other superclass of B). In general, the compiler can’t know at compile time what the true class of ‘a’ is though.

    #13045

    Jesse
    Participant

    I too expected that to be an error since class a doesn’t know about class b but class b does know about class a…But I like that it returns a null.

    #13055

    abakobo
    Participant

    ok thanks

    I thought invoking a method on a Null instance was giving an error..

    #13056

    Mark Sibly
    Keymaster

    I thought invoking a method on a Null instance was giving an error..

    It was and it should. In debug mode I get the following error…

    Attempt to invoke method on null instance

    …on line 29, ob3.doMore()

    This is 100% correct behaviour – you can’t invoke a method on a Null instance. You could a while back but haven’t been able to for some time now.

    #13059

    abakobo
    Participant

    Strange… Here with latest dev branch using “i686-6.2.0-posix-dwarf-rt_v5-rev1” and windows 10 I get the following. (see picture, whole code and console prints are visible, code attached as file too.)

    #13116

    Mark Sibly
    Keymaster

    Can you post output of console? You can copy and paste from console by clicking in console, then using edit menu to select-all/copy (Yes, this should be smoother, my fault really).

    [edit]Ok, can reproduce, seems to be a mingw thing. Under msvc x64 I get:

    ob is Null
    -now trying to invoke the doMore() method on ‘ob’ Null intance-

    Attempt to invoke method on null instance

    ie: it should die before the ‘yo!’ Will check it out…

    #13122

    Diffrenzy
    Keymaster

    Testing that upload of .monkey2 files is now allowed .

    #13141

    cocon
    Participant

    This one seems to have a link to an object instance somewhere in memory, but the displayed value is Null.

     

    However trying overloading the To operator makes the program behave correctly.

     

    However I know that the point of the post is to stress test the compiler (I am not trying to patch the code example). But other than that you would consider that as a programming practice, super-casting is always ambiguous, because most of the times you can’t be sure how the derived type translates to their parent.

    As a point of reference in C# language this operation is not permitted. So perhaps Monkey would introduce the same restriction, to promote more formal and strict habits.

     

    #13157

    Mark Sibly
    Keymaster

    This one seems to have a link to an object instance somewhere in memory, but the displayed value is Null.

    Please post runnable examples including Main() – how am I meant to know what goes before that etc?

    #13160

    Mark Sibly
    Keymaster

    var b = (B)a; // <– This won’t happen…
    > // System.InvalidCastException has been thrown

    I’m not too keen on making Cast throw an exception or runtime error, as cast currently doubles as an ‘intanceOf’ style operation, eg: you can go…

    If Cast<B>( a ) Print "A as a B!"

    Changing this behaviour would break a ton of my own code and would mean I would have to add ‘InstanceOf’ or similar to the language. I’m quite happy with current behaviour.

    #13166

    cocon
    Participant

    I updated the previous post to include the code sample.

    #13170

    Mark Sibly
    Keymaster

    Cheers, I thought that was probably it but for the sake of clarity and to minimize confusion it’s always nice to be as clear as possible.

    Same answer though, Cast wont be throwing an exception any time soon. Even if it did, I’d still like something ‘castish’ which is faster than “If i instanceOf T then t:=Cast<T>( i )” which effectively needs to do 2 downcasts.

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.