[comp.lang.smalltalk] Smalltalk syntax question/quibble

peter@ficc.uu.net (Peter da Silva) (03/07/90)

I have thought that the Smalltalk syntax for defining a new class was
a bit inconsistent with the rest of the language. I'm thinking of how
one would write a small object-oriented language, and while I like the
Smalltalk syntax, I'm thinking a more elegant syntax for creating new
classes or methods would be:

	name <- Class Subclass: instance_variables
	name Method: methodname Is: block

So the syntax for point would be like:

	Point <- Magnitude Subclass: #( 'x' 'y' ).
	Point Method: 'new' Is: [ x <- 0. y <- 0. ^self. ].
	Point Method: 'x:y:' Is: [ x0 y0 | x <- x0. y <- y0. ].
	Integer Method: '@' Is: [ y |
		p <- Point new.
		p x: self y: y.
		^p.
	].

Etcetera. What's wrong with this picture? (obviously something is)
-- 
 _--_|\  Peter da Silva. +1 713 274 5180. <peter@ficc.uu.net>.
/      \
\_.--._/ Xenix Support -- it's not just a job, it's an adventure!
      v  "Have you hugged your wolf today?" `-_-'

thomson@hub.toronto.edu (Brian Thomson) (03/07/90)

In article <ZR12K7Axds13@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>I have thought that the Smalltalk syntax for defining a new class was
>a bit inconsistent with the rest of the language. 
>
...
>	Point <- Magnitude Subclass: #( 'x' 'y' ).
>	Point Method: 'new' Is: [ x <- 0. y <- 0. ^self. ].
>	Point Method: 'x:y:' Is: [ x0 y0 | x <- x0. y <- y0. ].
>	Integer Method: '@' Is: [ y |
>		p <- Point new.
>		p x: self y: y.
>		^p.
>	].
>
For starters, those method bodies are compiled in the immediate execution
environment.  You have to pass a string, not a block, so the compiler
can compile them in the context of the newly created class.

Other than that, this doesn't seem to be a very great departure.
You are doing more or less what Smalltalk already does, except that
you are ignoring (and, I presume, defaulting) a lot of other information
that Smalltalk lets you provide, eg.:

Subclass: variables
	^self subclass: (Symbol new: 0)	"Subclass name not known"
	     instanceVariableNames: variables
	     classVariableNames: nil
	     poolDictionaries: nil
	     category: 'NoCategory'.
-- 
		    Brian Thomson,	    CSRI Univ. of Toronto
		    utcsri!uthub!thomson, thomson@hub.toronto.edu

cowan@marob.masa.com (John Cowan) (03/07/90)

In article <ZR12K7Axds13@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>I have thought that the Smalltalk syntax for defining a new class was
>a bit inconsistent with the rest of the language. I'm thinking of how
>one would write a small object-oriented language, and while I like the
>Smalltalk syntax, I'm thinking a more elegant syntax for creating new
>classes or methods would be:
>
>	name <- Class Subclass: instance_variables
>	name Method: methodname Is: block

[examples deleted]

The trouble with this scheme is that a class created by this means doesn't
know its own name.  If you say "Point := Object subclass: #(foo bar baz)"
then Point holds the new class, but the new class has no name.  So the
standard syntax "Object subclass: #Point" etc. etc. means that the class
can know its own name, for printing purposes and such.

Similarly, the reason the syntax for methods won't work is that blocks
can't be directly coerced to methods.  Blocks can refer to variables in the
current lexical scope, whereas method definitions always have a null
lexical environment.  Changing this would seriously alter the character of
the language, and it is not clear what such "nested methods" would mean.
The compiler, not just the run-time, must know in advance when it's dealing
with a method and when with a block - compare in Common Lisp the different
treatment given by most compilers to DEFUN vs. LAMBDA.

new@udel.edu (Darren New) (03/08/90)

You would also have to note which methods were instance methods and
which were class methods.  I suspect that something very similar to
what you propose is already out there somewhere.

lgm@cbnewsc.ATT.COM (lawrence.g.mayka) (03/08/90)

In article <25F52B91.185F@marob.masa.com> cowan@marob.masa.com (John Cowan) writes:
>The compiler, not just the run-time, must know in advance when it's dealing
>with a method and when with a block - compare in Common Lisp the different
>treatment given by most compilers to DEFUN vs. LAMBDA.

Your Common Lisp example is a bit confusing.  Perhaps you're
referring to the difference between

	'(LAMBDA ...)

which creates a list that *can* be compiled or interpreted as a
function (with a null lexical environment), versus

	#'(LAMBDA ...)

which returns a closure (which may include variables from the
lexical environment in which it appears).


	Lawrence G. Mayka
	AT&T Bell Laboratories
	lgm@ihlpf.att.com

Standard disclaimer.