On Monday, July 29, 2013 09:40:45 AM ольга крыжановская wrote:
> Dan, what does super() do?
To oversimplifiy, it returns a reference to the superclass (a bit more
complicated since python has multiple inheritence). In ksh we have a reference
to the current object using _, but can't for example extend an overridden
method by calling the base version from a derived class. It shares some
overlapping uses as virtuals in C++ (though everything is virtual in python).
I can't really write an equivalent in ksh because ksh's constructors are broken
/ non-existent as pointed out in another thread.
I have a partial workaround that you can prefix all your type definitions with,
but I can't think of any way to call the callConstructor function
automatically.
typeset -T T=(
typeset -h 'Type variable' type
typeset -h 'Name of the constructor function that will be called'
constructor=init
typeset -h 'Arguments to pass to the constructor.' -a args
typeset -h 'Arguments to pass to the type declaration' -a typeargs
function create {
# Broken.
trap "${!_}.callConstructor" Exit
}
function callConstructor {
[[ -v .sh.type.${_.type} ]] || return 1
nameref self=_
eval '"${_.type}" self' \; "_.${_.constructor} $(printf '%q '
"${_.args[@]}")"
}
)
> On Mon, Jul 29, 2013 at 9:37 AM, Dan Douglas <[email protected]> wrote:
> > On Wednesday, July 24, 2013 12:40:01 PM David Korn wrote:
> >> cc: [email protected] [email protected]
> >> Subject: Re: Re: [ast-users] _ doesn't refer to the correct object.
> >> --------
> >>
> >> > What happens if "x" itself is a typed variable? What does python do?
> >>
> >>
> >> I don't know what python does. What I did for ksh was to have _ reference
> >> x when x is a type variable.
> >
> > Ksh has "type variables"? As in generics?
> >
> > If you really mean "objects", in Python, everything is an object and has a
> > type. There are no "untyped variables". Why wouldn't Ksh's _ work the same
> > for
> > user-defined types as for built-in types?
> >
> > #!/usr/bin/env python3
> >
> > class A(object):
> > def __init__(self):
> > self.y = "I'm from A"
> >
> > @property
> > def x(self):
> > return self.y
> >
> > class B(A):
> > def __init__(self, **kwargs):
> > self.y="I'm from B"
> > print(self.x) # "I'm from B"
> > super().__init__(**kwargs) # call base class
> > constructor.
> >
> > obj = B()
> > print(obj.x) # "I'm from A"
> >
> >
> > I take it that __ is going to work like super(), hopefully. This basic
> > program
> > fails in several ways:
> >
> > #!/usr/bin/env ksh
> >
> > typeset -T Person_t=(
> > integer -S population
> > typeset name
> >
> > function create {
> > ((_.population++))
> > }
> >
> > # This destructor fails altogether for some reason.
> > function unset {
> > ((_.population--))
> > }
> >
> > # Static method
> > typeset -fS totalpop
> > function totalpop {
> > printf 'Total population is currently %d.\n'
> > "${_.population}"
> > }
> > )
> >
> > typeset -T Maintainer_t=(
> > Person_t _
> >
> > function create {
> > # We need some equivalent to Python super().
> > # Theres no base class access or virtual / override
> > methods.
> > .sh.type.Person_t.create
> > .sh.type.Person_t.totalpop
> > }
> >
> > # I don't know whether this is needed. It fails either way.
> > function unset {
> > .sh.type.Person_t.unset
> > .sh.type.Person_t.totalpop
> > }
> > )
> >
> > typeset -T Shell_t=(
> > Maintainer_t -a maintainers
> > typeset list
> >
> > function create {
> > print -r 'Another UNIX shell?!'
> > }
> > )
> >
> > function main {
> > compound shells=(
> > Shell_t ksh=(maintainers=((name=DGK) (name=Glenn));
> > list=ast-users)
> > Shell_t bash=(maintainers=((name=Chet));
> > list=bug-bash)
> > Shell_t mksh=(maintainers=((name=TG)))
> > )
> >
> > typeset -p shells
> > unset -v shells
> > }
> >
> > main "$@"
> >
> > --
> > Dan Douglas
> > _______________________________________________
> > ast-users mailing list
> > [email protected]
> > http://lists.research.att.com/mailman/listinfo/ast-users
>
>
>
>
--
Dan Douglas
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users