SmallTalk

Discussion in 'Mac Programming' started by scan, Nov 30, 2005.

  1. macrumors 6502

    Joined:
    Oct 24, 2005
    #1
    Anyone know SmallTalk in here?
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    I've dabbled in it but I'm not proficient. It's a good thing to learn prior to learning Objective-C.
     
  3. thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #3

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

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    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.
     
  5. macrumors member

    Joined:
    Nov 23, 2005
    #5
    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:

    Code:
    [NSObject alloc];
    Here is an Objective-C message, where an instance is the receiving object:

    Code:
    [myInstance alterData];
    When you declare (as well as implement) a class method, you must put an addition sign in front of it.

    Code:
    +alloc;
    When you declare a instance method, you must put a subtraction sign in front of it.

    Code:
    -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.
     
  6. macrumors member

    Joined:
    Nov 23, 2005
    #6
    Out of curiosity, where did you get your Smalltalk compiler, is it for Mac?
     
  7. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #7
    Try Squeak. It's a typical environment that runs on Mac OS X.
     
  8. thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #8
    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
     
  9. thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #9
    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.
     
  10. macrumors 6502a

    Joined:
    Jun 17, 2003
    #10
    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:
    Code:
    // Java loop
    for(int x=1; x<=20; x++) {
        System.out.println("" + x);
    }
    is written in Smalltalk by:
    Code:
    1 to: 20 do: [:x | x printNl ] !
    
     
  11. thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #11
    is there some general rule of thumb when to write class methods. I don't quite understnad why adn when they are used.

     
  12. macrumors 6502a

    Joined:
    Jun 17, 2003
    #12
    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).
     
  13. thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #13
    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

     
  14. macrumors member

    Joined:
    Nov 23, 2005
    #14
    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.)
     
  15. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #15
    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)
     
  16. macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #16
    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.
     
  17. macrumors member

    Joined:
    Nov 23, 2005
    #17
    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:

     
  18. macrumors member

    Joined:
    Nov 23, 2005
    #18
  19. macrumors 6502a

    Joined:
    Jun 17, 2003
    #19
    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.
     
  20. macrumors member

    Joined:
    Nov 23, 2005
    #20
    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.
     
  21. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #21
    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
     

Share This Page