Re: Namespaces (2)

new topic     » topic index » view thread      » older message » newer message

Irv wrote:

> We gain the ability to do things like:
>  import "addrbook.pi" as Customer
>  import "addrbook.pi" as Vendor

What you are doing here is using "addrbook.pi" as a class definition, and
'import' as the class initializer. But since the bindings are only static,
you can't write something like this:

   procedure show_info( me )
      ? me.name
      ? me.phone
   end procedure

because the interpreter can't resolve what 'me.name' is supposed to be.
Using static namespaces to simulate classes looks attractive, but is a poor
solution at best.


>  Customer[NAME] = "Homer Simpson"
>  Vendor[NAME] = "Ace Services".....

With associated lists, you could write:

   Customer["Name"] = "Homer Simpson"

It looks like you don't gain much, but in reality, you've managed to avoid
that nasty problem of namespace collision. Because the index is a sequence
instead of a constant, you don't have to worry about another module defining
NAME.

And the code that you write is polymorphic, as well. For example, if you
stored routine ids in the associated list, you could write something like
this:

   procedure draw_it( sequence self )
      call_proc( self["draw"], {self} )
   end procedure

Here's a class constructor:

   function new_customer( sequence name, sequence phone )
      sequence self
      self = {}
      self["Name"] = name
      self["Phone"] = phone
        return self
   end function

Python and JavaScript both use associative lists to implement objects. They
allow using dot notation for syntactic sugar; i.e.:

   self.Name = name

but underneath, the are doing:

   self["Name"] = name

which is more or less equivalent in Euphoria (if you are using Jiri's
toolkit) to:

   self = sset( self, "Name", name )

-- David Cuny

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu