About KCP and automatic initialize

Avi Bryant avi at beta4.com
Wed Sep 17 08:10:59 UTC 2003


On Wed, 17 Sep 2003, Ian Piumarta wrote:

> On Wed, 17 Sep 2003, Richard A. O'Keefe wrote:
>
> > "Andreas Raab" <andreas.raab at gmx.de> wrote:
> > 	+initializeWithFoo: aFoo bar: aBar
> > 	could simply compile a class-side method which is
> > Yes.  YES.  **YES**!   Let's DO it!
>
> A quick and dirty hack that hasn't been tested much.  Don't file it into a
> mission-critical image.

Here's one that uses the MethodAnnotations changeset from Andreas (anyone
have a URL for that?).

So,

 initializeWithFoo: x
   <initializer: #foo:>
   foo := x

will automatically create the class side method

 foo: arg1
   ^ self basicNew initializeWithFoo: arg1

Avi
-------------- next part --------------
'From Squeak3.6beta of ''4 July 2003'' [latest update: #5411] on 17 September 2003 at 1:07:15 am'!
"Change Set:		InitializerAnnotations-avi
Date:			17 September 2003
Author:			Avi Bryant

Automatically generate class side instance creation methods from instance side initialization methods with an #initializer annotation.

For example, 

initializeWithFoo: x
  <initializer: #foo:>
  foo _ x

will automatically create the class side method

foo: arg1
  ^ self basicNew initializeWithFoo: arg1

This requires the MethodAnnotations-ar changeset.
"!


!Object class methodsFor: 'annotations' stamp: 'avi 9/17/2003 00:53'!
addSelector: selector withMethod: compiledMethod 
	super addSelector: selector withMethod: compiledMethod.
	(compiledMethod valueOfProperty: #initializer) ifNotNilDo: 
		[:init |
		self compileCreationMethod: init forInitializationSelector: selector]! !

!Object class methodsFor: 'annotations' stamp: 'avi 9/17/2003 01:03'!
compileCreationMethod: createSelector forInitializationSelector: initSelector
	createSelector numArgs = initSelector numArgs ifFalse:
		[^ self error: 'Creation method must take the same number of arguments as initialization method'].
	self class compile: (self messagePatternForSelector: createSelector), '
	^ self basicNew ', (self messagePatternForSelector: initSelector)! !

!Object class methodsFor: 'annotations' stamp: 'avi 9/17/2003 01:01'!
messagePatternForSelector: aSymbol
	^ String streamContents:
		[:stream |
		aSymbol keywords withIndexDo:
			[:ea :i |
			stream nextPutAll: ea.
			ea last = $: ifTrue:
				[stream space; nextPutAll: 'arg', i asString.
				i _ i + 1]]]! !



More information about the Squeak-dev mailing list