Page 1 of 1

Nested (or inner) classes

PostPosted: Sat Sep 12, 2009 5:10 am
by hopscc
I've added a new ticket (ticket:178) for the enhancement of providing support for nested or inner classes.

Does anyone have any comments or discussion points to make around this area ?
e.g
utility/desire/need ?
Whether its worthwhile to support java like semantics ( automatic access to instance methods/variables in enclosing class) or C# like behaviour
(specific coding idiom for same access)
Other?
...
There will probably be a patch for a simple implementation posted shortly

Re: Nested (or inner) classes

PostPosted: Sat Sep 12, 2009 12:28 pm
by helium
I thought Java like == inner class, C# like == nested class.

Re: Nested (or inner) classes

PostPosted: Sat Sep 12, 2009 6:09 pm
by Charles
Maybe some code examples in all three languages would be helpful. Compare and contrast Java vs. C# and then propose syntax and semantics for Cobra.

Re: Nested (or inner) classes

PostPosted: Sat Sep 12, 2009 6:28 pm
by hopscc
C# vs java differences C# nested classes are ike C++ nested classes, not Java inner classes.

simple cobra looks pretty much as you'd expect ;
something like this
Code: Select all
# simple nested class - access from inside
class Enclosing

        class Nested
                cue init
                        base.init

                def geti as int
                        return 1

        def geti as int
                return 99

        def main
                a = Enclosing()
                assert a.geti == 99
                n=Enclosing.Nested()
                assert n.geti == 1

# slightly more involved
# multiple levels of Nested class, inferred and explicit typing
class Enclosing

        class Nested is public

                def geti as int
                        return 1

                class Nested2
                        def geti as int
                                return 2

        class NestedOther
                get geti
                        return 10

        def geti as int
                return 99

class Enc
        def main
                a = Enclosing()
                assert a.geti == 99

                en=Enclosing.Nested()
                assert en.geti == 1

                enn=Enclosing.Nested.Nested2()
                assert enn.geti == 2

                eno as Enclosing.NestedOther?
                eno = Enclosing.NestedOther()
                assert eno.geti == 10

Re: Nested (or inner) classes

PostPosted: Sat Sep 12, 2009 11:13 pm
by Charles
That's a handy link including the comments. Thanks.

Looks good to me.

The _boxStack you see in the Cobra compiler is there to support nested classes / structs. So you can see I tried to account for them even though I didn't have time to really do them.

-Chuck

Re: Nested (or inner) classes

PostPosted: Mon Sep 14, 2009 10:58 pm
by hopscc
Yah - I posted a patch for the simplest implementation against the ticket.
It uses .curBox which used _boxStack ( cleaned out the TODO comment)

Re: Nested (or inner) classes

PostPosted: Tue Sep 15, 2009 2:51 am
by Charles
Cool, I'll take a look.

Re: Nested (or inner) classes

PostPosted: Thu Sep 17, 2009 8:01 pm
by Charles
Applied. Thanks.