. .

st4u

ST 4U 323: More on Polymorphism

December 19, 2012 7:32:57.253

Today's Smalltalk 4 You goesback to Polymorphism in Smalltalk for some more discussion. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Polymorphism

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


We've gone over a basic polymorphism example, but one point needs to be made: when you decide to add polymorphic behavior to your own objects, the API you need to cover is typically small. What if you need to cover a system class - like, say, Collection?

Collection API

In the simple example we covered here, there was one method to create in each object: #currentValue. If you intend to cover something larger, you'll have more work to do.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: ,

Enclosures:
[st4u323-iPhone.m4v ( Size: 2411402 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 322: Keyboard Binding Changes in VW

December 17, 2012 7:27:20.990

Today's Smalltalk 4 You looks at one of the changes - keyboard shortcuts - you'll run into when upgrading from an older rev of VisualWorks to the latest. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

VisualWorks

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Tags: ,

Enclosures:
[st4u322-iPhone.m4v ( Size: 1903066 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 321: Using Polymorphism

December 14, 2012 10:32:32.511

Today's Smalltalk 4 You looks at the standard development process using VA Smalltalk and ENVY. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Polymorphism.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


We've gone over a sketch of a Case Statement object in Smalltalk, but most of the time, polymorphism is the better answer. Today we'll give a simple example. Consider three classes that hold financial data:



Object subclass: #Instrument
    classInstanceVariableNames: ''
    instanceVariableNames: 'value '
    classVariableNames: ''
    poolDictionaries: ''

Object subclass: #SavingsAccount
    classInstanceVariableNames: ''
    instanceVariableNames: 'balance '
    classVariableNames: ''
    poolDictionaries: ''

Object subclass: #StockAccount
    classInstanceVariableNames: ''
    instanceVariableNames: 'stocks '
    classVariableNames: ''
    poolDictionaries: ''

Each object calculates their value differently, but the idea is, we can send #currentValue to each, without needing to know the way they function internally (for an example in the base libraries, consider all of the objects that respond to #next). Here's the three implementations of #currentValue:


Instrument

currentValue
	^self value

SavingsAccount

currentValue
	^self balance

StockAccount

currentValue
	^self stocks inject: 0 into: [:subTotal :next | subTotal + next currentValue].

The nice part comes in when we need to get the values. We need not worry what kind of instrument we have; we simply send #currentValue to it:


| savings investment1 investment2 investment3 investment4 stockAcct |
savings := SavingsAccount new: 1000.
investment1 := Instrument value: 200.
investment2 := Instrument value: 400.
investment3 := Instrument value: 600.
investment4 := Instrument value: 800.
stockAcct := StockAccount stocks: 
	(Array
			with: investment1
			with: investment2
			with: investment3
			with: investment4).

Transcript show: 'Savings: ', savings currentValue printString; cr.
Transcript show: 'Stocks: ', stockAcct currentValue printString; cr.
Transcript show: 'Investment 1: ', investment1 currentValue printString; cr.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: ,

Enclosures:
[st4u321-iPhone.m4v ( Size: 3971079 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 320: Using a CaseStatement in Smalltalk

December 12, 2012 11:15:01.801

Today's Smalltalk 4 You goes back to the CaseStatement sketch we did recently, and goes over when such a construct might be useful in Smalltalk. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Case Statement.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Having looked at a Case Statement sketch and at polymorphism, we'll turn back to the Case Statement idea - when might such a thing be useful in Smalltalk? There are times when you end up with a long section of #ifTrue:ifFalse: blocks - when dealing with keyboard shortcuts, or possibly return values from an external application. Consider this simple example:



"imagine that val is the return value from some external application"

| val cases |
val := ((EsRandom new next) * 10) rounded.
cases := CaseStatement case: 1 do: [Transcript show: '1'; cr].
cases
	case: 2  do: [Transcript show: '2'; cr];
	case: 3  do: [Transcript show: '3'; cr];
	case: 4  do: [Transcript show: '4'; cr];
	case: 5  do: [Transcript show: '5'; cr];
	case: 6  do: [Transcript show: '6'; cr];
	case: 7  do: [Transcript show: '7'; cr];
	case: 8  do: [Transcript show: '8'; cr];
	case: 9  do: [Transcript show: '9'; cr].
cases switch: val default: [Transcript show: 'Not Found: ', val printString; cr].


Instead of a random number generator, imagine that the code coming back is from an external system call, and we need to do different things based on what came back. We could set up a polymorphic caller, using a dictionary matching numbers to symbols, and then performing the symbols - but that might actually be less clear than the code above. It's not often that you'll need a case statement in Smalltalk, but it does come up from time to time.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: ,

Enclosures:
[st4u320-iPhone.m4v ( Size: 3329436 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 319: Oracle and VW 7.9

December 10, 2012 10:59:31.148

Today's Smalltalk 4 You looks at a small change in the way VW works against Oracle (as of VW 7.9). If you're upgrading from an older rev of the product, you might run into this. There's a code example in this post, and you should probably be aware that Cincom has classified this as a bug, for which they'll be issuing a resolution. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Oracle and #bindInput:

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

s

Tags: , ,

Enclosures:
[st4u319-iPhone.m4v ( Size: 2758104 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 318: Sketching Out a CaseStatement Object

December 7, 2012 12:33:47.791

Today's Smalltalk 4 You sketches out a case statement object in Smalltalk. Not because this is a great idea; generally, you want to use Polymorphism. However, having been asked about such a thing, we have a sketch of how to do it. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Case Statement.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Recently, I was asked about Case Statements and Smalltalk - Smalltalk does not have one (nor does it need one, for the most part) - but it makes for a small, interesting example. I had created one of these in VisualWorks years ago, but it ported pretty cleanly into VA Smalltalk (the only real difference being that #new in VA does not send #initialize to the newly created instance). I have verification that the code works as-is in GNU Smalltalk as well - not a surprise, as it's pretty basic stuff. Here's the class definition:



Object subclass: #CaseStatement
    classInstanceVariableNames: ''
    instanceVariableNames: 'cases '
    classVariableNames: ''
    poolDictionaries: ''

There's a convenience method to create the first case, and the collection of cases is a collection of associations (where the key is the case, the value is the execution block for that case):


case: aCondition do: aBlock
	"answer a new instance with initial condition"

	^self new case: aCondition do: aBlock

There are a few instance methods to hook up the machinery:


switch: aCondition
	"execute block previously stored"

	self switch: aCondition default: [self error: 'No Default Found']

switch: aCondition default: aBlock
	"execute block previously stored"

	| association |
	association := self findOnKey: aCondition.
	association notNil 
		ifTrue: [(self findValue: association) value]
		ifFalse: [aBlock value]

findOnKey: aCondition
	"answer association or nil"

	^cases detect: [:each | each key = aCondition] ifNone: [nil]

findValue: anAssociation
	"answer the value"

	^anAssociation value

You can then see how this kind of thing works:


| caseStatement |
caseStatement := CaseStatement case: 1 do: [Transcript show: 'one'; cr].
caseStatement 
	case: 2 do: [Transcript show: 'two'; cr];
	case: 3 do: [Transcript show: 'three'; cr];
	case: 4 do: [Transcript show: 'four'; cr].

To actually use the statement, you do this kind of thing:


caseStatement switch: 6 default: [Transcript show: 'Could not find case'; cr].
caseStatement switch: 5.


Try that out and see what happens. You can find the code in VRGoodies (Contributed) in VisualWorks, and that code will port cleanly to at least VA and GNU (and I expect Pharo or Squeak as well).

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: , ,

Enclosures:
[st4u318-iPhone.m4v ( Size: 4364936 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 317: Dictionary Variance

December 5, 2012 7:32:02.809

Today's Smalltalk 4 You looks at a small behavior difference in the way things work in VA Smalltalk, Pharo, and VisualWorks, involving strings and IdentityDictionary. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Identoty.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Today we'll look at IdentityDictionary to point out a small difference in how things work in VA, Pharo, and VisualWorks. Consider the following code:


dict := Dictionary new.
dict 
	at: 'one' put: 1;
	at: 'two' put: 2;
	at: 'three' put: 3.
^dict at: 'one'.

You won't be surprised by the answer in any Smaltalk. Now consider this:


dict := IdentityDictionary new.
dict 
	at: 'one' put: 1;
	at: 'two' put: 2;
	at: 'three' put: 3.
^dict at: 'one'.

In Pharo and VA, you get back 1 - the literals are apparently reused, making them safe as keys in an IdentityDictionary. In VisualWorks, on the other hand, you get a KeyNotFound exception. In VisualWorks, the literals are not reused (and, since the introduction of immutability a few releases ago, that behavior is guaranteed).

If you are working on a project that is maintaining code across multiple Smalltalk implementations, this is a useful thing to know.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: ,

Enclosures:
[st4u317-iPhone.m4v ( Size: 2339597 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 316: System Reporter in Pharo

December 3, 2012 8:15:59.149

Today's Smalltalk 4 You looks at the system reporter in Pharo. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Reporter

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Tags: ,

Enclosures:
[st4u316-iPhone.m4v ( Size: 2952287 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 315: Writing Collections with Streams

November 30, 2012 10:09:51.520

Today's Smalltalk 4 You looks at using Streams to write collections (not Strings). If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Write Streams.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Today we'll have a look at writing arbitrary collections via streams. In Smalltalk, streams can be used to write collections of objects (not just strings). For example: here we set up a ByteArray, and then write to it:


collection := ByteArray new: 5.
stream := collection writeStream.
stream nextPut: 10.
stream nextPut: 55.
stream nextPut: 10000.
^collection

If you inspect the results, you'll see the ByteArray #[10 55 0 0 0]. You can also create collections of disparate objects:


collection2 := Array new: 5.
stream2 := collection2 writeStream.
stream2 nextPut: 'one'.
stream2 nextPut: (1/3).
^collection2

Inspecting the results will give you the collection #('one' (1/3)) - the last object, if you look in the inspector, is a fraction.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: ,

Enclosures:
[st4u315-iPhone.m4v ( Size: 2693564 )]

posted by James Robertson

 Share Tweet This

st4u

ST 4U 314: Reading Collections with Streams

November 28, 2012 9:59:10.964

Today's Smalltalk 4 You looks at readng objects from collections in Smalltalk - streams are hardly limited to strings. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

read streams.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Today we'll have a look at reading arbitrary collections via streams. In Smalltalk, streams can be used to read any collection (not just strings). For example: here we set up a ByteArray, and then read from it:


stream := #[12 23 34 78 99] readStream.
collection := OrderedCollection new.
collection add: stream next.
collection skip: 2.
collection add: stream next.
^collection

If you inspect the results, you'll see the collection #(12 78). You can do the same with collections of disparate objects:


stream2 := #('one' 2 'four' 16.2 $F 81) readStream.
collection2 := OrderedCollection new.
collection2 add: stream2 next.
collection2 skip: 2.
collection2 add: stream2 next.
^collection2

Inspecting the results will give you the collection #('one' 16.2). Next time, we'll look at writing collections using streams.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: ,

Enclosures:
[st4u314-iPhone.m4v ( Size: 2881153 )]

posted by James Robertson

 Share Tweet This

Previous Next (554 total)