Symbol.st
changeset 77 6c38ca59927f
parent 68 59faa75185ba
child 88 81dacba7a63a
equal deleted inserted replaced
76:38782bff886a 77:6c38ca59927f
    20 Symbol comment:'
    20 Symbol comment:'
    21 
    21 
    22 COPYRIGHT (c) 1988 by Claus Gittinger
    22 COPYRIGHT (c) 1988 by Claus Gittinger
    23               All Rights Reserved
    23               All Rights Reserved
    24 
    24 
    25 Symbols represent unique strings - every symbol with same printString
    25 $Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.9 1994-05-17 10:09:25 claus Exp $
    26 exists exactly once in the system; Symbols are used for selectors, global
       
    27 variable-keys etc.
       
    28 
       
    29 $Header: /cvs/stx/stx/libbasic/Symbol.st,v 1.8 1994-03-30 09:37:04 claus Exp $
       
    30 '!
    26 '!
    31 
    27 
       
    28 !Symbol class methodsFor:'documentation'!
       
    29 
       
    30 documentation
       
    31 "
       
    32     Symbols represent unique strings - every symbol with same printString
       
    33     exists exactly once in the system; Symbols are used for selectors, global
       
    34     variable-keys etc.
       
    35 "
       
    36 ! !
       
    37 
    32 !Symbol class methodsFor:'instance creation'!
    38 !Symbol class methodsFor:'instance creation'!
    33 
       
    34 XXXbasicNew:size
       
    35     "catch instance creation - symbols are not created with new"
       
    36 
       
    37     self error:'symbols may not be created with new:'
       
    38 ! 
       
    39 
    39 
    40 basicNew:size
    40 basicNew:size
    41     "redefined to return a string instead of a symbol -
    41     "redefined to return a string instead of a symbol -
    42      this allows all copy methods inherited from String to
    42      this allows all copy methods inherited from String to
    43      return strings containing the symbols characters.
    43      return strings containing the symbols characters.
    68 %}
    68 %}
    69 .
    69 .
    70     ^ self mustBeString
    70     ^ self mustBeString
    71 !
    71 !
    72 
    72 
       
    73 fromString:aString
       
    74     "same as intern: for Symbol, but may be used to create interned instances
       
    75      of subclasses.
       
    76      Notice: this fails, if you try to intern an instance of a subclass, AND
       
    77      a symbol with the same name already exists. In this case, the original
       
    78      symbol is returned. To use it for enum-type symbols, make certain, that the
       
    79      names are unique (for example by including the classes name as a prefix-string)."
       
    80 
       
    81     |newSym len|
       
    82 
       
    83     aString knownAsSymbol ifTrue:[
       
    84         ^ aString asSymbol
       
    85     ].
       
    86 
       
    87     "
       
    88      create a new uninterned instance first
       
    89     "
       
    90     len := aString size.
       
    91     newSym := super basicNew:len.
       
    92     newSym replaceFrom:1 to:len with:aString.
       
    93 
       
    94     "now, intern it"
       
    95 %{
       
    96     extern OBJ _INTERNSYMBOL();
       
    97 
       
    98     newSym = _INTERNSYMBOL(newSym, (OBJ *)0, __context);
       
    99 %}.
       
   100     ^ newSym
       
   101 !
       
   102 
    73 internCharacter:aCharacter
   103 internCharacter:aCharacter
    74     "return a unique symbol with printname taken from the Character-argument"
   104     "return a unique symbol with printname taken from the Character-argument"
    75 
   105 
    76     ^ self intern:(aCharacter asString)
   106     ^ self intern:(aCharacter asString)
    77 ! !
   107 ! !
    86     aString knownAsSymbol ifTrue:[
   116     aString knownAsSymbol ifTrue:[
    87         trueBlock value:(aString asSymbol).
   117         trueBlock value:(aString asSymbol).
    88         ^ true
   118         ^ true
    89     ].
   119     ].
    90     ^ false
   120     ^ false
       
   121 !
       
   122 
       
   123 hasInterned:aString
       
   124     "return true, if the argument, aString is known as Symbol;
       
   125      false otherwise"
       
   126 
       
   127     ^ aString knownAsSymbol
    91 ! !
   128 ! !
    92 
   129 
    93 !Symbol methodsFor:'accessing'!
   130 !Symbol methodsFor:'accessing'!
    94 
   131 
    95 basicAt:index put:something
   132 basicAt:index put:something
    96     "report an error - symbols may not be changed"
   133     "report an error if interned - interned symbols may not be changed."
    97 
   134 
    98     self error:'symbols may not be changed'
   135     self knownAsSymbol ifTrue:[
    99 !
   136         self error:'symbols may not be changed'.
   100 
   137         ^ something
   101 at:index put:something
   138     ].
   102     "report an error - symbols may not be changed"
   139     "
   103 
   140      uninterned - allow change
   104     self error:'symbols may not be changed'
   141     "
       
   142     ^ super basicAt:index put:something
   105 ! !
   143 ! !
   106 
   144 
   107 !Symbol methodsFor:'copying'!
   145 !Symbol methodsFor:'copying'!
   108 
   146 
   109 deepCopyUsing:aDictionary
   147 deepCopyUsing:aDictionary
   143 
   181 
   144 !Symbol methodsFor:'misc'!
   182 !Symbol methodsFor:'misc'!
   145 
   183 
   146 species
   184 species
   147     ^ String
   185     ^ String
       
   186 ! !
       
   187 
       
   188 !Symbol methodsFor:'system primitives'!
       
   189 
       
   190 become:anotherObject
       
   191     "make all references to the receiver become references to anotherObject
       
   192      and vice-versa. For symbols, some spacial action is required, to
       
   193      correctly handling a become of the global dictionaries.
       
   194      Anyway: this is very dangerous - mysterous side-effects are to be
       
   195      expected."
       
   196 
       
   197     (Smalltalk includesKey:self) ifTrue:[
       
   198         super become:anotherObject.
       
   199 %{
       
   200         __rehashSystemDictionaries();
       
   201 %}.
       
   202     ] ifFalse:[
       
   203         super become:anotherObject
       
   204     ]
       
   205     "
       
   206      |aSym|
       
   207      aSym := #fooBar.
       
   208      Smalltalk at:#fooBar put:1.
       
   209      #fooBar become:#barBaz.
       
   210      'sym is now: ' print. aSym printNL.
       
   211      Smalltalk includesKey:#fooBar
       
   212      Smalltalk at:#barBaz
       
   213     "
       
   214 !
       
   215 
       
   216 becomeNil
       
   217     "make all references to the receiver become nil - effectively getting
       
   218      rid of the receiver. For symbols, this is not allowed, if thr receiver
       
   219      is used as a key in some SytemDictionary.
       
   220      This can be a very dangerous operation - be warned."
       
   221 
       
   222     (Smalltalk includesKey:self) ifTrue:[
       
   223         self primitiveFailed
       
   224     ] ifFalse:[
       
   225         super becomeNil
       
   226     ]
   148 ! !
   227 ! !
   149 
   228 
   150 !Symbol class methodsFor:'binary storage'!
   229 !Symbol class methodsFor:'binary storage'!
   151 
   230 
   152 binaryDefinitionFrom: stream manager: manager
   231 binaryDefinitionFrom: stream manager: manager