problem with casting?

View: New views
7 Messages — Rating Filter:   Alert me  

problem with casting?

by psi-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


class ob1():
        public a=0

class ob2(ob1):
        public b=0

print cast(ob1,ob2()) isa ob2

print ob2() as ob1 isa ob2

a as ob1=cast(ob1,ob2())
print a isa ob2

//a.b = 1 # see second output where this line was uncommented,
compiler thinks 'a' isn't an 'ob2'.

/*Running boo 0.9.2.3383 on Mono 2.4.2.3.
# Output @ Fri Sep 18 01:28:37 BST 2009
True
True
True
# Used (interp) :  0:00.63 real,0.58 user,0.02 sys
*/


/*Running boo 0.9.2.3383 on Mono 2.4.2.3.
# Output @ Fri Sep 18 01:28:57 BST 2009
test.boo(16,3): BCE0019: Boo.Lang.Compiler.CompilerError: 'b' is not a
member of 'ob1'. Did you mean 'a'?

Command exited with non-zero status 127
# Used (interp) :  0:00.57 real,0.52 user,0.03 sys
*/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: problem with casting?

by Cedric Vivier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

On Fri, Sep 18, 2009 at 8:37 AM, psi <simon.place@...> wrote:

a as ob1=cast(ob1,ob2())
print a isa ob2 #true

//a.b = 1 # see second output where this line was uncommented,
compiler thinks 'a' isn't an 'ob2'.


`a isa ob2' returns true because `isa' is a run-time reflection operation.
Since you declare `a' with ob1 type, statically you cannot access ob2 members without another cast (or using duck).

Cheers,


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: problem with casting?

by psi-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> `a isa ob2' returns true because `isa' is a run-time reflection operation.

but, 'a' was cast to an ob1.
(ob2 inheriting from ob1 not the other way round)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: problem with casting?

by Cedric Vivier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Sep 18, 2009 at 11:32 PM, psi <simon.place@...> wrote:

> `a isa ob2' returns true because `isa' is a run-time reflection operation.

but, 'a' was cast to an ob1.
(ob2 inheriting from ob1 not the other way round)


The local variable `a' is declared as being of ob1 type in "a as ob1 = cast(ob1, ob2())".
The cast here is actually not necessary, since ob2 inherits from ob1.
Casting a reference type does not change the underlying instance type: an ob2 instance will always be an ob2 instance (that's why the `isa', which is dynamic, knows that the instance is an ob2), casting just makes it directly usable (or not usable) depending on the context.




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: problem with casting?

by psi-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> Casting a reference type does not change the underlying instance type: an
> ob2 instance will always be an ob2 instance (that's why the `isa', which is
> dynamic, knows that the instance is an ob2), casting just makes it directly
> usable (or not usable) depending on the context.

cast needs to produce a reference that is typed as the parent, even
thought the sub-type is still there and could be accessible by other
references.

example:

a function takes an object type, that object has several sub-types,
and the function modifies what it does depending on the sub-type,
using 'isa'. If someone now adds to one of the sub-types, making a sub-
sub-type, and casts it to its parent to make a call to the function,
the internals of the function will be broken.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: problem with casting?

by Cedric Vivier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Sep 19, 2009 at 1:42 AM, psi <simon.place@...> wrote:
> Casting a reference type does not change the underlying instance type: an
> ob2 instance will always be an ob2 instance (that's why the `isa', which is
> dynamic, knows that the instance is an ob2), casting just makes it directly
> usable (or not usable) depending on the context.

cast needs to produce a reference that is typed as the parent, even
thought the sub-type is still there and could be accessible by other
references.

This is paraphrasing above ;)


example:
a function takes an object type, that object has several sub-types,
and the function modifies what it does depending on the sub-type,
using 'isa'. If someone now adds to one of the sub-types, making a sub-
sub-type, and casts it to its parent to make a call to the function,
the internals of the function will be broken.

Handy enough, you could declare ob2 `final' (sealed in C#), if you don't want 'someone' to inherit from it.
Anyways branching on run-time type detection is always fragile (beware of dragons), if that's not desired then `isa' is not the right tool for what you want.

Another safer approach would be to use overloading, so that the compiler would statically link to the most appropriate method.
Also, you could move the implementation of that said function to internal virtual methods of the ob* classes themselves (type-safe and future-proof).


Cheers,


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: problem with casting?

by Rodrigo B. de Oliveira :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Fri, Sep 18, 2009 at 3:06 PM, Cedric Vivier <cedricv@...> wrote:
> ...
> Anyways branching on run-time type detection is always
> fragile (beware of dragons) ...

For the fun of it, here are Devon  and  Cornwall:

    import Boo.Lang.PatternMatching

    class ob1():
        public a=0

    class ob2(ob1):
        public b=0

    def withPatternMatching(o):
        match o:
            case _ob2 = ob2():
                print _ob2.b

    def withDuckTyping(o as duck):
        print o.b

    o = ob2(b: 42)
    withPatternMatching(o)
    withDuckTyping(o)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang?hl=en
-~----------~----~----~----~------~----~------~--~---