help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] [PATCH] Add shape #inherit


From: Paolo Bonzini
Subject: [Help-smalltalk] [PATCH] Add shape #inherit
Date: Thu, 27 Sep 2007 16:44:03 +0200
User-agent: Thunderbird 2.0.0.6 (Macintosh/20070728)

This is the same as

    <shape: self superclass shape>

except that in a world where class syntax is 100% declarative this won't work correctly. Problem suggested by Robin Redeker on the issue tracker, and solved here; includes documentation patch to teach about this in the tutorial.

Paolo
2007-09-27  Paolo Bonzini  <address@hidden>

        * examples/Behavior.st: Add #inherit shape.


--- orig/doc/tutorial.texi
+++ mod/doc/tutorial.texi
@@ -2122,8 +2122,10 @@ is not practical.  Why not add a subclas
 
 @example
    Array subclass: NiledArray [
-       "We could even write <shape: Array shape> here!"
-       <shape: #pointer>
+       "We could write <shape: #pointer>, but we just use the same shape
+        as the superclass.  That's a good idea because the superclass
+        instance creation methods should work if we do not change the shape."
+       <shape: #inherit>
 
        boundsCheck: index [
            ^(index < 1) | (index > (self basicSize))
@@ -2652,19 +2654,17 @@ type of array which a String uses.
 Aha!  But when you go back to chapter 9 and look at the
 Smalltalk hierarchy, you notice that String does not inherit
 from ByteArray.  To see why, we must delve down yet another
-level, and arrive at the basic methods for creating a
-class.
+level, and arrive at the basic methods for setting up the
+structure of the instances of a class.
 
 When we implemented our NiledArray example, we used
address@hidden<shape: #pointer>}.  This special kind
-of class creation (and the other uses of @code{shape:} we'll show
-shortly) defines the fundamental structure of Smalltalk
-objects created within a given class.  Let's consider the
-differences in the next sub-sections.
address@hidden<shape: #inherit>}.  The shape is exactly that:
+the fundamental structure of Smalltalk objects created within a given
+class.  Let's consider the differences in the next sub-sections.
 
 @table @asis
 @item Nothing
-The default kind of class creation specifies the simplest
+The default shape specifies the simplest
 Smalltalk object.  The object consists only of the storage
 needed to hold the instance variables.  In C, this would be
 a simple structure with zero or more scalar address@hidden
@@ -2698,17 +2698,19 @@ places in Smalltalk.
 The storage allocated as specified by new: is an array of characters.
 The analog in C would be a dynamically allocated structure with
 scalar fields, followed by a array of @code{char}.
-
-There are many more shapes for more specialized usage.
 @end table
 
+There are many more shapes for more specialized usage.  All of them
+specify the same kind of ``array-like'' behavior, with different
+data types.
+
 How to access this new arrays?  You already know how to access instance
 variables---by name.  But there doesn't seem to be a name for this new
 storage.  The way an object accesses it is to send itself
 array-type messages like @code{at:}, @code{at:put:}, and so forth.
 
 The problem is when an object wants to add a new level
-of interpretation to the at: and at:put: messages.  Consider
+of interpretation to these messages.  Consider
 a Dictionary---it is a pointer-holding object
 but its @code{at:} message is in terms of a key, not an integer
 index of its storage.  Since it has redefined the @code{at:} message, how


--- orig/kernel/Behavior.st
+++ mod/kernel/Behavior.st
@@ -908,13 +908,19 @@ shapes
 
 shape: shape
     "Give the provided shape to the receiver's instances.
-     The shape can be nil, or one of #byte #int8 #character #short
-     #ushort #int #uint #int64 #uint64 #utf32 #float #double or #pointer."
+     The shape can be nil, or one of #byte #int8 #character #short #word
+     #ushort #int #uint #int64 #uint64 #utf32 #float #double or #pointer.
+     In addition, the special value #inherit means to use the shape of the
+     superclass; note however that this is a static setting, and subclasses
+     that used #inherit are not mutated when the superclass adopts a different
+     shape."
 
     | realShape |
-    realShape := shape == #word
-       ifTrue: [ CSymbols.CLongSize = 4 ifTrue: [ #uint ] ifFalse: [ #uint64 ] 
]
-       ifFalse: [ shape ].
+    realShape := shape.
+    shape = #word ifTrue: [
+       realShape := CSymbols.CLongSize = 4 ifTrue: [ #uint ] ifFalse: [ 
#uint64 ] ].
+    shape = #inherit ifTrue: [
+       realShape := self superclass shape ]
 
     self shape == realShape ifTrue: [ ^false ].
     realShape isNil




reply via email to

[Prev in Thread] Current Thread [Next in Thread]