U E D R , A S I H C RSS

Small Talk/문법정리

목적

  • SBPP를 공부하기 위해 최소한의 문법은 떼야하겠다는 생각이 들었다.

기본 문법


  • ~cpp ClassName>>methodName.
    • 실제로 쓰이는 문법은 아니다. 하지만, Smalltlak 설명에서 해당 메소드가 어디에서 발현되는지를 알수 있게 만들어 준다.
    • Debug 모드에서 메세지의 흐름을 나타낼때도 이용된다.

  • ~cpp 
    배울때 가장 자주 보는 에러 메시지
    UndifinedObject(Object)>>doesNotUnderstand:
    
  • := 대입 연산자
    ~cpp 
    a := 1.
    b := OrderedCollection new.
    
  • self : 자신을 지칭하는 참조
    ~cpp 
    Money class>>amout: anAmount
        self new setAmount: anAmount.
    
  • super : 부모를 지칭하는 참조
    ~cpp 
    TempTestCase>>initialize
        super initialize.
    
  • yourself : 해당 객체를 지칭하는 참조
    ~cpp 
    a := OrderdCollection new
        add: 10;
        add: 1;
        add; 9;
        yourself.
    
  • ^ 리턴 : Smalltalk 모든 메소드는 리턴한다. C에서와 같은 void는 없다. 그래서 명시적으로 리턴 안해주면 self를 리턴한다.
    ~cpp 
    Color>>alpha
        ...
        ^1.0
    

메시지 보내기

from VisualWorks Lesson Browser 01 (Lesson: Language Basic)
  • 모든 스몰토크 표현식이 다음과 같은 양식을 따른다.All Smalltalk expressions follow the form:
  • anObject aMessage.
  • 예시)
    ~cpp 
    "Code in Bold"		"Comments in double-quotes"
    3 squared.		"Receiver object is small integer 3."
    			"The message sent is   #squared"
    
    'abc' asUppercase.	"Receiver object is string 'abc'."
    			"The message sent is   #asUppercase"
    
    200 factorial.		"Receiver object is small integer 200."
    			"The message sent is   #factorial"
    

메시지 종류

from VisualWorks Lesson Browser 01 (Lesson: Language Basic)
  • Unary messages
    • 선택자는 단일 식별자이며, 인수는 없다.(the selector is a single Identifier, there are no arguments)
    • 예시)
      ~cpp 
      3 negated			"selector is  #negated"
      'abcdefg' reverse		"selector is  #reverse"
      
  • Binary messages
    • 선택자는 특정한 기호(하나나 둘이상의 문자, 숫자가 아님)이고, 인수가 딱 하나만 있다.(the selector is one or two non-alphanumeric characters, followed by exactly one argument object)
    • 예시)
      ~cpp 
      3 + 5				"selector is  #+"
      				"argument is the Integer object 5"
      'abc' , 'xyz'			"selector is  #,  (comma character)"
      				"argument is the String 'xyz'"
      20 <= 20			"selector is  #<="
      				"argument is the Integer object 20"
      
  • Keyword messages
    • 키워드란 콜론(:)이 뒤에 붙는 식별자이다.(a keyword is an Identifier followed by colon ":" )
    • 각 키워드에 인수가 있을 때, 선택자는 하나 이상의 키워드이다.(the selector is one or more keywords, when called each keyword is followed by an argument object) -> 세번째 예시 참고
    • 예시)
      ~cpp 
      3 raisedTo: 17.			"selector is  #raisedTo:"
      				"argument is 17"
      3 between: 5 and: 10.		"selector is  #between:and:"
      				"arguments are 5 and 10"
      Point x: 4 y: 200. 		"receiver is the object Point"
      				"selector is  #x:y"
      				"arguments are 4 and 200"
      

신기한점

2 + 3 * 6 은 몇일까?
일반적으로는 20이겠지만 SmallTalk에서는 30이다.--; 열라 신기하다.

읽은지 얼마 안되었는데도 스몰토크의 메소드 길이는 7줄을 넘지 않는다는 말이 나온다. 그러면서 extract method(이 말을 직접적으로 쓰진 않았지만)도 나온다.

리팩토링 관련 문구 계속 나오고 있다.
컨테이너 리턴하는 메소드는 앞에 as를 붙인다. 이것도 써먹어야겠다.

Message 의 전달 순서

from Smalltalk by Example
  1. 메세지는 왼쪽에서 오른쪽으로 진한다. Evaluation is done left to right.
    • 그래서 2 + 3 * 6 -> 30
  2. Unary 메세지는 가장 높은 우선 순위를 가진다. messages have the highest precedence.
  3. Binary 메세지는 그 다음의 우선 순위를 가진다. Binary message have the next precedence.
  4. Keyword 메세지는 가장 낮은 우선 순위를 가진다.Keyword message have the lowest precedence.
  5. 괄호 '()' 를 써서 우선 순위를 변경할수 있다. You can alter precedence by using parenthses.

Smalltalk
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:03
Processing time 0.0142 sec