Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
complexvalues [2019/01/04 16:38]
christian [Using Values]
complexvalues [2019/01/04 16:56]
christian [Using Values]
Line 38: Line 38:
 </code> </code>
  
-Withit you can:+With it you can:
   * create a value with <code smalltalk>person := Person example</code>   * create a value with <code smalltalk>person := Person example</code>
   * ask for its parts <code smalltalk>person name  "returns 'Christian Haider' "</code>   * ask for its parts <code smalltalk>person name  "returns 'Christian Haider' "</code>
Line 80: Line 80:
 The variables are set all at once by an initalizing method which has all initial values as parameters. The variables are set all at once by an initalizing method which has all initial values as parameters.
 The object becomes immutable after initialization and all instance variables are effectively constants. The object becomes immutable after initialization and all instance variables are effectively constants.
 +
 +The parameter names are the concatenated name and its class. This prevents name clashes, is systematic and still readable.
  
 <code smalltalk> <code smalltalk>
-Person>>initializeName: aString sex: aSymbol birthday: aDate +Person>>initializeName: nameString sex: sexSymbol birthday: birthdayDate 
-  name := aString+  name := nameString
-  sex := aSymbol+  sex := sexSymbol
-  birthday := aDate.+  birthday := birthdayDate.
   self beImmutable   self beImmutable
 </code> </code>
Line 92: Line 94:
  
 <code smalltalk> <code smalltalk>
-Person class>>name: aString sex: aSymbol birthday: aDate+Person class>>name: nameString sex: sexSymbol birthday: birthdayDate
   | inst |   | inst |
   inst := self new.   inst := self new.
-  inst initializeName: aString sex: aSymbol birthday: aDate.+  inst initializeName: nameString sex: sexSymbol birthday: birthdayDate.
   ^inst   ^inst
 </code> </code>
Line 115: Line 117:
   <constant: #name class: #{String}>   <constant: #name class: #{String}>
   <constant: #sex class: #{Symbol}>   <constant: #sex class: #{Symbol}>
-  <constant: #bithday class: #{Date}>+  <constant: #birthday class: #{Date}>
 </code> </code>
  
 === Defaults === === Defaults ===
  
-The simple values above are not very interesting. But when you define defaults for some of the variables, Values become more useful+The simple values above are not very interesting. But when you define defaults for some of the variables, Values become more useful. Lets add a nickname to the specification: 
 + 
 +<code smalltalk> 
 +Person class>>localSpecification 
 +  <constant: #name class: #{String}> 
 +  <constant: #sex class: #{Symbol}> 
 +  <constant: #bithday class: #{Date}> 
 +  <optional: #nickname class: #{String} default: 'self name'> 
 +</code> 
 + 
 +After generating code with:  
 +<code smalltalk>Person generateMethods</code> 
 +our example responds to #nickname : 
 + 
 +<code smalltalk> 
 +Person example nickname   "returns 'Christian Haider' "> 
 +</code> 
 + 
 +and there is a new constructor available: 
 +<code smalltalk> 
 +Person class>>name: nameString sex: sexSymbol birthday: birthdayDate nickname: nicknameString> 
 +</code> 
 + 
 +Now you can specify a nickname: 
 +<code smalltalk> 
 +Person name: 'Christian Haider' sex: #male birthday: (Date d: 25 m: 6 y: 1960) nickname: 'Chris'> 
 +</code> 
 + 
 +If you use the same parameter as the default, it is ignored 
 +<code smalltalk> 
 +Person name: 'Christian Haider' sex: #male birthday: (Date d: 25 m: 6 y: 1960) nickname: 'Christian Haider'> 
 +</code> 
 +because it is equal to 
 +<code smalltalk> 
 +Person name: 'Christian Haider' sex: #male birthday: (Date d: 25 m: 6 y: 1960)> 
 +</code> 
  
 ==== Get it ==== ==== Get it ====
  • complexvalues.txt
  • Last modified: 2022/03/06 08:57
  • by christian