PDA

View Full Version : SmallTalk




scan
Nov 30, 2005, 05:09 PM
Anyone know SmallTalk in here?



bousozoku
Nov 30, 2005, 05:56 PM
I've dabbled in it but I'm not proficient. It's a good thing to learn prior to learning Objective-C.

scan
Nov 30, 2005, 07:18 PM
I've dabbled in it but I'm not proficient. It's a good thing to learn prior to learning Objective-C.


unfortunately I need to learn smalltalk. one of my main questions is what is the difference between an instance method and a class method

bousozoku
Nov 30, 2005, 07:54 PM
unfortunately I need to learn smalltalk. one of my main questions is what is the difference between an instance method and a class method

Instance methods are specific to the created (instance of the class) object but class methods are universal across created objects/instances.

This same concept works in SmallTalk, Objective-C, and Java, as well as some less pure object-oriented languages.

Jordan72
Nov 30, 2005, 08:48 PM
unfortunately I need to learn smalltalk. one of my main questions is what is the difference between an instance method and a class method

There are two fundamental object types in Objective-C: Class objects and Instances.

When your code is exectued, the Objective-C runtime creates class object's. Class objects create objects, which are referred to as instances.

A class method is only used by the class object. An instance method is used by the instance created by the class object. (A class method's primary use is to create instances of a class.)

Here is an Objective-C message, where a class object is the receiving object:

[NSObject alloc];

Here is an Objective-C message, where an instance is the receiving object:

[myInstance alterData];

When you declare (as well as implement) a class method, you must put an addition sign in front of it.

+alloc;

When you declare a instance method, you must put a subtraction sign in front of it.

-alterData;


Objects needs to exist, before a message will work. When a program executes, if no object existed, such as the class objects, there would be no way to send messages to create objects. Class objects initiate the ability to send messages and class methods intiate the ability to create instance objects, which then allows messages to be sent between objects created in the program.

Jordan72
Nov 30, 2005, 09:18 PM
Out of curiosity, where did you get your Smalltalk compiler, is it for Mac?

bousozoku
Nov 30, 2005, 10:30 PM
Out of curiosity, where did you get your Smalltalk compiler, is it for Mac?

Try Squeak. It's a typical environment that runs on Mac OS X.

scan
Nov 30, 2005, 11:46 PM
yeah i'm using squeak. i've never had this much trouble with a programming language.

I'm still not quite sure about the class and instance metods. So if you create an object of some class, then it can only use the instance methods? so how would the class methods be used? i'm unclear about that

scan
Nov 30, 2005, 11:56 PM
pardon my ignorance, but could someone give me an example of how instance and class methods are used and when to use them? or perhaps comparing it with Java?

I have another question about smalltalk. How do loops work? I know there is no loops in smalltalk but there is a do method I believe? i'm not sure how the syntax goes.

mrichmon
Dec 1, 2005, 12:22 AM
pardon my ignorance, but could someone give me an example of how instance and class methods are used and when to use them? or perhaps comparing it with Java?

I have another question about smalltalk. How do loops work? I know there is no loops in smalltalk but there is a do method I believe? i'm not sure how the syntax goes.

Smalltalk class methods are equivalent to static methods in Java.
Smalltalk instance (or object) methods are equivalent to object methods in Java.

Loops in Smalltalk are handled by passing a block which contains the instructions for the body of the loop as an argument to the do: method.

For example:

// Java loop
for(int x=1; x<=20; x++) {
System.out.println("" + x);
}
is written in Smalltalk by:

1 to: 20 do: [:x | x printNl ] !

scan
Dec 1, 2005, 12:27 AM
is there some general rule of thumb when to write class methods. I don't quite understnad why adn when they are used.

Smalltalk class methods are equivalent to static methods in Java.
Smalltalk instance (or object) methods are equivalent to object methods in Java.

Loops in Smalltalk are handled by passing a block which contains the instructions for the body of the loop as an argument to the do: method.

For example:

// Java loop
for(int x=1; x<=20; x++) {
System.out.println("" + x);
}
is written in Smalltalk by:

1 to: 20 do: [:x | x printNl ] !

mrichmon
Dec 1, 2005, 12:36 AM
yeah i'm using squeak. i've never had this much trouble with a programming language.


The thing about Smalltalk is that you absolutely must understand object-oriented programming. Most other less pure OO languages (such as C++, Java, C#, to some extent Objective-C) you can get away with only vague ideas of OO design.

For example, sub-typing is not the same as inheritence. Polymorphism does not necessarily require class inheritence. In Java and C++ it is easy to consider these three concepts to be the same thing. In fact, there are important differences.

The best way to get the concepts straight is to read the first chapter or two of a decent Smalltalk book and really take time to understand the information. Many of the Smalltalk books are structured in the same way using the same examples to illustrate concepts in the early chapters. This is because many Smalltalk books follow the examples given in the Smalltalk blue book. So most any book will do. If pressed I would recommend "Smalltalk: An Introduction to Application Development Using VisualWorks" by Hopkins and Horan, or "Smalltalk-80: The Language" by Goldberg and Robson (otherwise known as the Purple book... the Purple book is a reprint of the first 3 parts from the Blue book).

scan
Dec 1, 2005, 01:26 AM
the book for my couse is SmallTalk, Objects, and Design by Chamond Liu. and i have read teh book very throough. with the problem I'm having, it literally talks about for 2 lines

The thing about Smalltalk is that you absolutely must understand object-oriented programming. Most other less pure OO languages (such as C++, Java, C#, to some extent Objective-C) you can get away with only vague ideas of OO design.

For example, sub-typing is not the same as inheritence. Polymorphism does not necessarily require class inheritence. In Java and C++ it is easy to consider these three concepts to be the same thing. In fact, there are important differences.

The best way to get the concepts straight is to read the first chapter or two of a decent Smalltalk book and really take time to understand the information. Many of the Smalltalk books are structured in the same way using the same examples to illustrate concepts in the early chapters. This is because many Smalltalk books follow the examples given in the Smalltalk blue book. So most any book will do. If pressed I would recommend "Smalltalk: An Introduction to Application Development Using VisualWorks" by Hopkins and Horan, or "Smalltalk-80: The Language" by Goldberg and Robson (otherwise known as the Purple book... the Purple book is a reprint of the first 3 parts from the Blue book).

Jordan72
Dec 1, 2005, 09:05 AM
yeah i'm using squeak. i've never had this much trouble with a programming language.

I'm still not quite sure about the class and instance metods. So if you create an object of some class, then it can only use the instance methods? so how would the class methods be used? i'm unclear about that

I'm not famililiar with smalltalk syntax, but I can answer theoretically.

Think of the pupose of instance methods like c functions. They primarily are meant to alter data you send the function or alter data that is local to the function.

Think of the purpose class methods as to primarily malloc objects. You could think of them as mallocing structs. But there is something special about these structs, they have methods identified with them.

So to call a method, you must use the object that declares that method.

(If you give me a link to an editor and compiler, I'll give you some examples with correct syntax. I'm interested in Smalltalk, the inspiritor of Objective-C.)

caveman_uk
Dec 1, 2005, 09:22 AM
Dunno about smalltalk specifically but in general terms an instance method is a method that is performed by an object that is an instance of a class. A class method is one that is performed by the class itself (you don't need to have an instance defined)

GeeYouEye
Dec 1, 2005, 04:17 PM
Instance methods are the methods you're probably already familiar with if you've done any C++ or Java programming; whereas in Java you might have

myString.length(); or myString.componentsSeparatedByString(",")

this would be in Smalltalk

myString length or myString componentsSeparatedByString:','

the main thing here is that you instantiate a class, (create a variable to hold and instance of that type; i think the syntax is |myString| in Smalltalk) and the instance of the class is an object that responds to messages that you send it, such as length, or componentsSeparatedByString:

But the classes themselves are also objects. When you send a message to a class object, it does not have any information about what instances of the class are like; it's like putting client code in with the class, located there usually for convenience with one big exception: memory allocation. Memory allocation is done by the class object, usually in the "new" classs method. So in order for myString to actually store anything, you have to do

myString := String new

before you try do send length or componentsSeparatedByString: to myString. An example of a convenience class method would be something like separateString: intoComponentsBy: (I don't think this actually exists), which would return an array. You might use it like this:

|myArray|
myArray := String separateString:'apple,bat,cat,dog,elephant' intoComponentsBy:','

and it would be implemented like this, assuming the arguments are named stringToBeSeparated and separatorString, repectively:

|tmpString anArray|
tmpString :=String new
tmpString setString:stringToBeSeparated
anArray := tmpString componentsSeparatedBy:separatorString
^anArray

Incidentally, AFAIK the closest thing that Java and C++ have to this is constructors, which isn't saying much (DISCLAIMER: I don't know Java very well yet, so it's possible it has something and I haven't found it yet). Objective-C has full support for class methods, but currently little to no support for class variables (say you wanted to track the number of instances of a class), though it did in the past.

Jordan72
Dec 4, 2005, 12:42 AM
Objective-C has full support for class methods, but currently little to no support for class variables (say you wanted to track the number of instances of a class), though it did in the past.

Hey, I haven't used the potential for a class object to contain variables, but there seems to be no limitations, Apple says it better than I can:

Some classes declare static variables and provide class methods to manage them. (Declaring a variable static in the same file as the class definition limits its scope to just the class—and to just the part of the class that’s implemented in the file. Unlike instance variables, static variables can’t be inherited by subclasses.)

Static variables help give the class object more functionality than just that of a “factory” producing instances; it can approach being a complete and versatile object in its own right. A class object can be used to coordinate the instances it creates, dispense instances from lists of objects already created, or manage other processes essential to the application. In the case when you need only one object of a particular class, you can put all the object’s state into static variables and use only class methods. This saves the step of allocating and initializing an instance.

Note:*It is also possible to use external variables that weren’t declared static, but the limited scope of static variables better serves the purpose of encapsulating data into separate objects.

Jordan72
Dec 4, 2005, 12:48 AM
Saw this book in the store today. Scan, this might be of some help to you. It's fairly inexpensive.http://froogle.google.com/froogle_cluster?q=squeak+book+robot&pid=1919944458645109882&oid=17097873704025001992&btnG=Search+Froogle&lmode=&addr=&scoring=p&hl=en

mrichmon
Dec 4, 2005, 12:52 AM
Hey, I haven't used the potential for a class object to contain variables, but there seems to be no limitations, Apple says it better than I can:

Note that the text from Apple only applies to Objective-C. Many languages (including Smalltalk if I recall correctly) and certainly Java and C++ have sub-classes inheriting both class/static methods and class/static fields.

Jordan72
Dec 4, 2005, 07:41 PM
Note that the text from Apple only applies to Objective-C. Many languages (including Smalltalk if I recall correctly) and certainly Java and C++ have sub-classes inheriting both class/static methods and class/static fields.

Apparently Objective-C class objects have copies of the field variables they declare. Although, I haven't tried to see if they have superclass field. I also haven't called an instance method. I'll give it a try.

jeremy.king
Dec 4, 2005, 10:20 PM
is there some general rule of thumb when to write class methods. I don't quite understnad why adn when they are used.

Yes, scope and purpose will play a huge role. Class (static in Java) methods can only see class variables, while instance methods will be able to "see" instance AND class variables.

Here - this will give you more than you need. You really need to learn OO fundamentals and then worry about implementation.

http://www.inf.ufsc.br/poo/smalltalk/ibm/tutorial/content.html