Class-based NewtonScript Programming, an article for PIE
Developers magazine describing a technique for structuring a
NewtonScript program using class-like objects.
| HTML
An introduction for a reprint of Ungar and Smith's paper Self:
the power of simplicity in PIE Developers magazine.
HTML
A Model for Address-Based Software and Hardware, a paper
presented at the 1992 Hawaii International Conference on System
Sciences. Gives the underlying principles of Newton OS kernel memory
interfaces, and the rationale for the ARM processor's unusual MMU
design.
PS.gz
The Newton Application Architecture, a paper presented at the
1994 IEEE Computer Conference.
PS.gz
text
The Newton Operating System, a paper presented at the 1994
IEEE Computer Conference. |
PS |
text
Low Power Hardware for a High Performance PDA, a paper
presented at the 1994 IEEE Computer Conference. |
PS |
text
Using a prototype-based language for user interface programming:
the Newton project's experience, a paper presented at OOPSLA '95.
Technoblog Classes are Just a Prototype Pattern
My friend Dave Fayram (who helped bring advanced
LSI classification to Ruby’s
classifier) has heeded
Matz’s advice to learn
Io and is
bringing me with him. I have been thinking a lot about
prototyped versus class-based languages lately and once I really
understood it, I fell in love. I have a feeling I will be
writing a lot about this topic, but here is a brief
introduction.
class Animal
attr_accessor :name
end
amoeba = Animal.new
amoeba.name = "Greenie"
class Dog < Animal
def bark
puts @name + " says woof!"
end
end
lassie = Dog.new
lassie.name = "Lassie"
lassie.bark
Notice in the Io version that you never ever define a class.
You don’t need to.
# Prototype-based Io
Animal := Object clone
# An object can be instantiated
amoeba := Animal clone
amoeba name := "Greenie"
# An object can be used to sub-class
Dog := Animal clone
Dog bark := method(
write(name .. " says woof!")
)
# An object can be instantiated
lassie := Dog clone
lassie name := "Lassie"
lassie bark # => Lassie says woof!
You will notice some syntactical differences immediately.
First, instead of the dot (.) operator, Io uses spaces (note:
technically, with a couple lines of Io you can actually make Io
use the dot operator or the arrow operator (->) or anything else
you would like).
Next, you will notice that instead of making a new instance
of a class, when you use prototype-based languages you clone
objects. This is the foundation of prototyping… defining classes
is unnecessary, everything is just an object! Furthermore,
every object is essentially a hash where you can set the
values of the hash as methods for that object.
You get a sense for this kind of idea in Ruby when you do
this:
class Hmm
end
Hmm.class # => Class
So the class of the Hmm class is a class…
classes are instance objects of Class. In short,
almost everything in Ruby is an object already, except whas is
the class of Class?
If you have never seen this before, you might want to take a
minute to recuperate from the aneurism this should have caused.
If fact, this is a conundrum found in all class-based
programming languages and is solved in various different ways
(in Ruby, Matz chose to solve this paradox by making Class
a singleton class).
The important part to take away from this discussion is that
almost everything in Ruby is an object, even classes.
Prototyping embraces this fact and takes it the last step
necessary for the abstraction and completely removes the
distinction between a class and an object. Once you see it this
way, you will notice that having classes at all is a crutch… an
unnecessary layer that doesn’t add to object-oriented
programming… in fact it makes things more confusing to have the
separation at times.
How could class-based programming make things more confusing
you ask? The answer lies in metaprogramming. Metaprogramming in
Ruby can be beautifully easy. Here is a canonical example:
class Foo
def bar
@bar
end
def bar=(bar)
@bar = bar
end
end
class Foo
attr_accessor :bar
end
However, more involved metaprogramming, like that found in
Rails’ ActiveRecord implements metaprogramming for thing like
has_many in the following way.
module AnimalSounds
def bark
puts "woof"
end
def self.append_features(base)
super
base.extend(ClassMethods)
end
module ClassMethods
def create_sound(attr_name)
define_method(attr_name) { puts "Method #{attr_name} called" }
end
end
end
class Pet
include AnimalSounds
create_sound :meow
end
fido = Pet.new
fido.bark
fido.meow
The implementation of create_sound is very
convoluted, though in the end we are left with a very nice
interface. The convolution is birthed from the separation
between classes and instances in Ruby. When you give up your
grasp of classes and enter the world of prototyped languages,
metaprogramming at this level falls into place much easier and
cleaner.
AnimalSounds := Object clone
AnimalSounds bark := "woof"
AnimalSounds createSound := method(attrName,
self setSlot(attrName, block("Method " .. attrName .. " called"))
)
Pet := Object clone appendProto(AnimalSounds)
Pet createSound("meow")
fido := Pet clone
fido bark
fido meow
Interesting, isn’t it. I wonder what Rails’s ActiveRecord and
it’s engines would look like in Io, don’t you?
As a final mention to the power of prototyping, I would like
to mention ActionScript, which is Macromedia’s programming
language available within Flash. On the face of it, it looks
like any other class-based programming language, but the class
of Class paradox is mute because ActionScript a
prototype-based language. The “Class” is basically a clone of an
Object. Think on that. Classes are just a prototype
pattern.
Posted in
Io,
Programming,
Ruby | 14
comments
Comments
Leave a
response
[Mar 27, 2004]
Slashdot The Slate Programming Language
-
Re:Deja vu by RovingSlug
(Score:2) Saturday March 27,
@01:11PM
prototype based
languages (Score:5, Informative)
by
Jecel Assumpcao Jr (5602) on
Saturday March 27, @01:40PM (#8689938)
(http://www.merlintec.com/)
|
When running a program, it is very
likely that you will want to create new
objects as you go along. You have some
alternatives:
- call a magic constructor fucntion (C++ and friends)
- send a message like "new" to a
factory object (like a class in
Smalltalk)
- send a message like "copy" or
"clone" to an object that is like
the one you want to create
In the third case you might find out
that you can get by with a set of
"prototype" objects to copy from and you
don't need classes at all. But to
actually eliminate classes you will have
to find solutions to the other things
they do for you like hold the behaviors
for the objects (you can put them in the
objects themselves, for example) and
reflection (Self uses special "mirror
objects" for that).
There are several different styles of
prototype based
languages [dekorte.com]. |
| [
Reply to This |
Parent ] |
| |
Re:prototype
based languages (Score:4,
Interesting)
by
Piquan (49943) on Saturday March
27, @04:40PM (#8691002)
|
| Being a Lisp programmer, I'm
always looking for new ideas to
bring into my Lisp programs. It
looks like Lisp-- possibly even CLOS--
could support prototype-based
programming without extensive pain.
One thing that I'm wondering
about in prototype-based OOP is
redefining stuff. In Smalltalk and
CLOS (I don't know Self), you can
redefine methods over classes on the
fly, or change member variable
definitions, or whatever. I take
advantage of this to have a
production server running for months
while I make improvements.
But in a prototype language, this
looks, well, difficult. If your
methods are associated with
prototype objects, then if you have
existing non-prototype objects and
change a method, then would the
non-prototype objects get the method
def passed down, or what?
It seems like a prototype
language would also have problems
with multiple inheritance and
multiple dispatch, but it looks like
they've licked those too.
Interesting. |
-
Re:Deja vu by ZenFu
(Score:1) Saturday March
27, @02:18PM
Re:Deja vu
(Score:4, Informative)
by
angel'o'sphere (80593) <{ed.rotnemoo}
{ta} {redienhcs.olegna}> on Saturday
March 27, @03:23PM (#8690506)
(http://www.oomentor.de/ | Last
Journal:
Saturday February 15, @08:36AM)
|
What's a "prototype-based
object-oriented" language and how does
it differ from C++ and Java?
In Java and C++ you have classes. In a
prototyped language usually not, there
you only have objects.
To make it simple, lets look at the
java.util.Hashtable.
Suppose if you write this:
class MyClass {} the compiler would do
this: Object MyClass = new HashTable();
So, instead of defining a class, you
just created an empty object
... I used a Hashtable, because
that makes the later explanations more
easy, it could have been simply an
object of course.
So far your class has no properties
... no data and no methods.
So you likely would write something like
this:
class MyClass { Object start; Object end; }
As prototyped languages are usually
typeless you would only need to write
this:
class MyClass { start; end; }
Our hypotetical compiler makes this from
your typed code:
Object MyClass = new Hashtable();
// define a "new class" MyClass.add("start", null); MyClass.add("end", null);
So, instead of "defining" a class, like
you did in Java or C++, you only created
an object in the most global namespace.
That object is called "MyClass" just as
your class would have been called.
That object has two "slots" with the
names "start" and "stop", just like a
class would have two attributes of type
Object.
So: the concept is called prototyping,
because you would usually initialize the
MyClass object more properly, so that
"start" and "end" would have a value and
would not be empty.
Now comes the interesting point: we know
how to make classes now. How to make
objects from them?
Well, all Objects have a method called
"new".
In java you would write new MyClass().
But you could of course assume a class
had a static method called NEW().
So MyClass.NEW() would create a new
object of type MyClass. That method is
usualy build into the language and every
"class" has it. So, what does NEW() do?
It "clones" the object MyClass.
In Java we have a class MyClass, and
with new we create objects. The objects
have some kind of poitner to their
class.
In a prototyped language you only have
objects. If you say new to them, they
simply create a clone from themselves.
The original object is called
"prototype" or "traits". Instead of
calling a constructor to initialize such
a new created object it is fully
initialized with default values during
teh cloning.
Why did I use a Hashtable as example?
Well, you simply can tell a Hashtable to
clone itself ... if the
Hashtable describes a prototype. The
slots in a Hashtable are similar to teh
slots in an object of an prototyped
language. Suich a slot usualy has a name
and can hold any object ...
methods are objects as well :D
You can merge objects ...
and so you do not only get a new object
but also a new "class". Thats one way
how inheritance could be implemented.
But usualy you use nested Hashtables for
inheritance, as yoou most often want
single inheritance only ..
and merging is used for mixins
.... another term wixh would need
an explanaition as long as this one.
Regards, angel'o'sphere |
In recent years, an alternative to the traditional
class-based object-oriented language model has emerged. In this
prototype-based paradigm, there are no classes. Rather, new kinds of objects
are formed more directly by composing concrete, full-fledged objects, which
are often referred to as prototypes. When compared to class-based languages,
prototype-based languages are conceptually simpler, and have many other
characteristics that make them appealing, especially for the development of
evolving, exploratory and/or distributed software systems. The distinction
between class-based and prototype-based systems reflects a long-lasting
philosophical dispute concerning the representation of abstractions.
Class-based languages, such as Smalltalk, C++ and Java, explicitly use classes
to represent similarity among collections of objects. Prototype-based systems,
such as Self, NewtonScript and Omega, do not rely so much on advance
categorization, but rather try to make the concepts in the problem domain as
tangible and intuitive as possible. A typical argument in favor of prototypes
is that people seem to be much better at dealing with specific examples first,
then generalizing from them, than they are at absorbing general abstract
principles first and later applying them in particular cases. This book
presents the history and development of prototype-based programming and
describes a number of prototype-based programming languages and applications.
Such range from programs for portable digital appliances, graphical
user-interface management systems for desktop and workstations, and cutting
edge research on software visualisation and program restructuring. The book
will be suitable for advanced software development practitioners, graduate
students, and researchers active in the field.
[PDF]Object
Based Languages Prototype Based Languages or, applying ...
File Format: PDF/Adobe Acrobat -
View as HTML
Page 1. Advanced issues in object oriented programming,
prototype based languages
page: 1 out of 1 Advanced Issues in Object Oriented Programming
Object Based ...
www.doc.ic.ac.uk/~scd/Teaching/ObjectBased.pdf -
Similar pages
Self_on_Python
Cetus Links 18,595 Links on
Objects and Components - Self
Papers about
Self and OO Programming
In case of broken links
please try to use Google search. If you find the page please notify
us about new location
Prototype-based programming - Wikipedia, the free encyclopedia
Object-based PLs
page of Rainer Blome
Open
Directory - Computers Programming Languages Object-oriented Languages
Papers about
Self and OO Programming
The Object-Oriented Page
James Noble's Homepage
Background
on Object Oriented Programming
Mark Zeren's Newton Programming
Page
I first started programming the Newton in January 1994. Since then, I've
learned that what really attracts me to Newton development is the NewtonScript
language. Created by Walter Smith,
NewtonScript is a small functional, garbage collected language which uses a
prototype based (as opposed to class based) inheritence mechanism. It is very
well adapted to low memory environments and to user interface programming.
If you are curious about NewtonScript or Newton programming I encourage you
to check out Apple's developer web
pages. There you will find the Newton Toolkit (for Windows and MacOS),
documentation, and many other freely available resources for Newton development.
For a complete set of Newton links check out Rob Bruce's
Ultimate Newton Site.
Copyright © 1996-2009 by Dr. Nikolai Bezroukov.
www.softpanorama.org was
created as a service to the UN Sustainable Development Networking Programme (SDNP)
in the author free time.
Submit
comments This document is an industrial compilation designed and created
exclusively for educational use and is placed under the copyright of the
Open Content License(OPL).
Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made
for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
- The statements, views and opinions presented on
this web page are those of the author and are not endorsed by, nor do they necessarily
reflect, the opinions of the author present and former employers, SDNP or any other
organization the author may be associated with.
- We do not warrant the correctness of the information provided or its
fitness for any purpose
- In no way this site is associated with or endorse cybersquatters
using
the term "softpanorama" with other main or country domains (e.g. softpanorama.com) with
bad faith intent to profit from the goodwill belonging to
someone else.
Last updated:
August 15, 2009
|
ActionScript := ECMAScript clone appendProto(flashInterface)
JavaScript := ECMAScript clone appendProto(browserInterface)
So that’s why I like JavaScript so much…
:-)
So does this mean JavaScript is a prototype based language too?
Yes, JavaScript is prototype-based and very fun to work with when you discover that. :)
Totally off topic, but this is a beautiful theme and it’s great to see it in use!
This is my main reason for using Io. I simply cannot use Python or Ruby at this point (not that I ever could—I used Scheme prior). The syntax is general is also beautiful.
That said, Io still has some rough edges, as it is a very young language. On the plus side though, that means you can join in and help resolve the remaining issues in a way you find most agreeable. Go on… give it a shot…
Recently I was playing with Squeak Smalltalk. It’s a live system, you make changes to the image as it runs. I added a couple properties to the Character class (author, createdate). Immediately, the properties were available in every character in the system…all the dev tools I was running, the text editors, everything using characters now had the new properties available.
Would something like this work in a prototype-based language? If not, Smalltalk classes aren’t just a prototype pattern.
(In any case, Io sure is intriguing.)
Yes, inheritance works just as well in prototype-based languages. Add a couple methods to the Object class and you get them everywhere.
Forgive me being dense, but if you clone bar from foo, and later add a property to foo, does it show up in the already-created bar? (Ie., the cloned object maintains a reference to the object it was cloned from, rather than simply copying it?)
No need to apologize. The answer is yes, but don’t take my word for it, download and try for yourself: http://iolanguage.com/downloads/
Thanks. Good idea :) Although I’m a little worried that after I try it I won’t be happy with anything else!
So clone is not so much a deep copy of an object as it is a quasi-inheritence mechanism.
To make sure I understand correctly…
define_methodand friends withininstance_evalin Ruby.With prototypes, the relationship between class/superclass is the same as instance/class. An instance has a reference to a class in it, and a class has a reference to its superclass.
So if you have a List object/class, and you create an instance:
myList := List clone
Then myList contains a reference to List. And if a propery/method in myList isn’t found, it looks in List:
List bar := method(“This is bar” print) myList foo := method(“This is foo” print)
myList bar # looks in List mylist foo # looks in myList
So you could argue that myList is a subclass of List. You can subclass it or make an instance:
anotherList := myList clone
So clone returns a fresh object with only one propery: the reference to the superclass.
If you clone an array containing strings in Ruby and then modify the string objects of the original array with string.gsub!(regex) the string objects of the clone will be modified as well!
Quite a nifty Actionscript tool btw is the free open-source MotionTwin Actionscript Compiler ( http://mtasc.org , written in OCaml, www.ocaml-tutorial.org). The author is also the driving force behind http://nekovm.org, an interesting virtual machine project for scripting languages & language design.