Hi, I want to cast a object to a certain class. How can I do cast operation in Squeak as in other programming language? For example, I can do something like in java:
String i = (String)str;
I couldn't figure out the syntac for it.
Thanks Kwiyi
_________________________________________________________________ Say good-bye to spam, viruses and pop-ups with MSN Premium -- free trial offer! http://click.atdmt.com/AVE/go/onm00200359ave/direct/01/
"Kwiyi Woo" 03/01/04 02:35 >>>
Hi, I want to cast a object to a certain class. How can I do cast operation inS queak as in other programming language? For example, I can do something like in java:
String i = (String)str;
I couldn't figure out the syntac for it.
Variables in Squeak have no type, so there's no typecasting necessary. In other words, if the variable str contains a string, you just write
| i | "This declares i as a variable." i := str.
to assign str's value to i. The variable declaration section occurs at the top of the method, like in Pascal.
frank
Casting is a workaround for the problems of static typing. Smalltalk (and thus Squeak) is dynamically typed and therefore doesn't need casting. What exactly are you trying to accomplish?
Variables aren't typed, so you don't need casts for that. And if the object behaves like a string, then you just call string methods on it. If it doesn't, you'd have to implement those methods just the same as you would in Java.
Perhaps you could post a code snippet?
Julian
Kwiyi Woo wrote:
Hi, I want to cast a object to a certain class. How can I do cast operation in Squeak as in other programming language? For example, I can do something like in java:
String i = (String)str;
I couldn't figure out the syntac for it.
Thanks Kwiyi
Say “good-bye” to spam, viruses and pop-ups with MSN Premium -- free trial offer! http://click.atdmt.com/AVE/go/onm00200359ave/direct/01/
From: "Kwiyi Woo" kwiyi@hotmail.com wrote:
Hi, I want to cast a object to a certain class. How can I do cast operation in Squeak as in other programming language? For example, I can do something like in java:
String i = (String)str;
I couldn't figure out the syntac for it.
Thanks Kwiyi
In some low-level languages, casting is a way to give different interpretations to the contents of a given group of contiguous memory cells. There is nothing like casting in smalltalk, but there are conversion methods. For objects of any class, you may wish to try the method 'printString':
(13 - 2) printString will answer the string '11'
Conversions between collections are especially powerful. Just try, for example:
'The quick brown fox jumps over the lazy sleeping dog' asSet.
You get a set of all the characters in the string. Try also the conversion methods 'asBag' 'asArray' 'asOrderedCollection' 'asSortedCollection'
Note that conversion always creates a new instance; you need memory for both the given object and the converted object.
Hope this helps, Boris
squeak-dev@lists.squeakfoundation.org