[Newbies] OOP Question - adding methods to core classes

Sean P. DeNigris sean at clipperadams.com
Sun Oct 31 01:33:40 UTC 2010

I'm talking to Ruby via XMLRPC using the XMLRPC package from SqS.

The library has this heinous case statement:
	val caseOf: {
		[True] -> [^ self wrapVal: '1' type: 'boolean'].
		[true] -> [^ self wrapVal: '1' type: 'boolean'].
		[False] -> [^ self wrapVal: '0' type: 'boolean'].
		[false] -> [^ self wrapVal: '0' type: 'boolean'].
	} otherwise: [
		(val class) caseOf: {
			[SmallInteger] -> [^ self wrapVal: val asString type: 'i4'].
			[Float] -> [^ self wrapVal: val asString type: 'double'].
			[String] -> [^ self
				wrapVal: (self normalize: val)].
			[ByteString] -> [^ self
				wrapVal: (self normalize: val)].
			[XMLRPCDateTime] -> [^ self wrapVal: val encodeISO8601 type:
			[Array] -> [^ self
				wrapVal: (self encodeArray: val)
				type: 'array'].
			[Dictionary] -> [^ self
				wrapVal: (self encodeStruct: val)
				type: 'struct'].
		} otherwise: [
			val isStream
				ifTrue: [^ self wrapVal: (Base64MimeConverter mimeEncode: val) contents
type: 'base64']
					"Unhandled type."
					[XMLRPCException signal: 'Unhandled type:  ' , val className].

I was thinking how beautiful this code could be if each class had it's own
encode method, like:
    In method protocol *xmlrpc

BONUS: if this was the case, extending for new types would be ridiculously
trivial - just add the method to the type instead of trying to hook into
large control structures, e.g.
* so that a Symbol on the ST end is recognized as a Symbol on the Ruby side
(ends up as a string right now)
*so that I can talk to remote Ruby objects

OTOH, I was concerned about cluttering the core classes.  I've been going
back and forth with this kind of thing since I came to Smalltalk, never got
it resolved.

What do you think?

View this message in context: http://forum.world.st/OOP-Question-adding-methods-to-core-classes-tp3020788p3020788.html
Sent from the Squeak - Beginners mailing list archive at Nabble.com.

More information about the Beginners mailing list