Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Quark Framework 260 2. Data Declarations In common with other functional languages, CAL provides a way to declare new types using data declarations. Here is an example of declaring a new data type for 'Employee': /** * The Employee data type is an example of a CAL algebraic data type. It * has one data constructor, RegularEmployee. The RegularEmployee data * constructor has 3 fields: name, manager and directReports. * * The firstName and lastName fields have type String. The manager field * has type Maybe Employee. This reflects the fact that an employee may * have one manager or no managers (in the case fo the CEO), and that * manager is also an Employee. directReports has type [Employee] i.e. a * list of 0 or more employees. Note that the manager and directReports * fields have types that recursively refer to the Employee type. In other * words, Employee is a recursive algebraic data type. */ data Employee = RegularEmployee name :: String //the employeeID field is a potential future addition to the //Employee data type. Notice below that case expressions using //field-name based extraction would not need to be updated due //to this change, but case expressions using positional based //extraction would need updating. //employeeID :: Int manager :: (Maybe Employee) directReports :: [Employee] //This deriving clause provides a default instance implementation for //the Show type class for the Employee type. Essentially what this //means is that the Debug.show function will work with Employee //values to display a String representation of the value suitable for //debugging purposes. deriving Show; Note that fields (data constructor arguments) must have names in CAL, as well as their type. The deriving clause makes the data type work with functions defined in certain type classes. Here we ensure that CAL automatically adds the requisite logic to make values of Employee be renderable as strings for the purposes of tracing. Fields can be extracted in a variety of ways: a) Positionally in a case expression: /** * @arg employee * @return the employee's name */ employeeName :: Employee -> String; employeeName employee = case employee of
Quark Framework 261 //illustrates positionally based case expression extraction of the //"name" field of the RegularEmployee data constructor RegularEmployee empname _ _ -> empname; ; b) By field name in a case expression: /** * @arg employee * @return the employee's name */ employeeName :: Employee -> String; employeeName employee = case employee of //illustrates field-name based case expression extraction of the //"name" field of the RegularEmployee data constructor RegularEmployee {name} -> name; ; This method is obviously more stable with respect to changes in data constructor arguments than the positional version c) By a selector expression /** * @arg employee * @return the employee's name */ employeeName :: Employee -> String; employeeName employee = //illustrates data constructor field selection of the directReports //field employee.RegularEmployee.name; Note that CAL allows multiple constructor arguments to be cited in a case extractor, along with multiple constructors to match on (so long as they all have the named arguments). So, the following scenario is possible (assuming the data declaration includes the employeeID field and new constructors for Contractor and Associate): // ...snip... case employee of //illustrates multiple field case expression extraction over multiple //data constructors. Note the local renaming of the employeeID field //to 'id' (RegularEmployee | Contractor | Associate) {name, employeeID = id } -> (name, id); ; // ...snip...
- Page 213 and 214: JExamples 209 JExamples Industry Co
- Page 215 and 216: Jikes 211 Jikes Developer(s) IBM St
- Page 217 and 218: Michael Kölling 213 Microsoft Pate
- Page 219 and 220: List of Java keywords 215 case catc
- Page 221 and 222: List of Java keywords 217 long nati
- Page 223 and 224: List of Java keywords 219 Reserved
- Page 225 and 226: Name mangling 221 Name mangling In
- Page 227 and 228: Name mangling 223 } }; }; /* = _ZN9
- Page 229 and 230: Name mangling 225 if (__1cGstrcmp6F
- Page 231 and 232: Name mangling 227 ['_Test__private_
- Page 233 and 234: Name mangling 229 External links
- Page 235 and 236: Patrick Naughton 231 See also • H
- Page 237 and 238: New I/O 233 FileChannel can be used
- Page 239 and 240: New I/O 235 // Pattern used to pars
- Page 241 and 242: New I/O 237 References [1] "JSR 51:
- Page 243 and 244: Omniscient Debugger 239 Omniscient
- Page 245 and 246: OpenJDK 241 Inclusion in software d
- Page 247 and 248: OpenJDK 243 [12] "Open JDK is here!
- Page 249 and 250: PHP/Java Bridge 245 References [1]
- Page 251 and 252: Java package 247 External links jav
- Page 253 and 254: Parboiled (Java) 249 } } return seq
- Page 255 and 256: Plain Old Java Object 251 POJO gene
- Page 257 and 258: Pluggable look and feel 253 Pluggab
- Page 259 and 260: Primitive wrapper class 255 Primiti
- Page 261 and 262: Quark Framework 257 Quark Framework
- Page 263: Quark Framework 259 has proven to p
- Page 267 and 268: Quark Framework 263 As well as LECC
- Page 269 and 270: Quark Framework 265 `catch` (\ex ->
- Page 271 and 272: REPLAY (software) 267 Qualities REP
- Page 273 and 274: Real time Java 269 References [1] J
- Page 275 and 276: Restlet 271 Connectors The list of
- Page 277 and 278: SLAMD 273 SLAMD The SLAMD Distribut
- Page 279 and 280: SLAMD Server 275 SLAMD Server A SLA
- Page 281 and 282: Bruno Souza 277 Bruno Souza Born Na
- Page 283 and 284: Static import 279 References [1] Ja
- Page 285 and 286: strictfp 281 Behaviors and restrict
- Page 287 and 288: Sun Web Developer Pack 283 Sun Web
- Page 289 and 290: Swing (Java) 285 The Swing Architec
- Page 291 and 292: Swing (Java) 287 Relationship to AW
- Page 293 and 294: Swing (Java) 289 } window } See als
- Page 295 and 296: SwingLabs 291 • JDNC [7] : Contai
- Page 297 and 298: Synth Look and Feel 293 Supposing t
- Page 299 and 300: TeachScheme! 295 • a function def
- Page 301 and 302: TeachScheme! 297 TeachScheme! and B
- Page 303 and 304: Unified Expression Language 299 Imp
- Page 305 and 306: Java Virtual Machine 301 Bytecode v
- Page 307 and 308: Java Virtual Machine 303 Heap The J
- Page 309 and 310: Xerlin 305 • Steinbrenner, Eugen
- Page 311 and 312: XStream 307 } and add some of these
- Page 313 and 314: Article Sources and Contributors 30
Quark Framework 260<br />
2. Data Declarations<br />
In common with other functional languages, CAL provides a way to declare new types using data declarations. Here<br />
is an example of declaring a new data type for 'Employee':<br />
/**<br />
* The Employee data type is an example of a CAL algebraic data type. It<br />
* has one data constructor, RegularEmployee. The RegularEmployee data<br />
* constructor has 3 fields: name, manager and directReports.<br />
*<br />
* The firstName and lastName fields have type String. The manager field<br />
* has type Maybe Employee. This reflects the fact that an employee may<br />
* have one manager or no managers (in the case fo the CEO), and that<br />
* manager is also an Employee. directReports has type [Employee] i.e. a<br />
* list of 0 or more employees. Note that the manager and directReports<br />
* fields have types that recursively refer to the Employee type. In other<br />
* words, Employee is a recursive algebraic data type.<br />
*/<br />
data Employee =<br />
RegularEmployee<br />
name :: String<br />
//the employeeID field is a potential future addition to the<br />
//Employee data type. Notice below that case expressions using<br />
//field-name based extraction would not need to be updated due<br />
//to this change, but case expressions using positional based<br />
//extraction would need updating.<br />
//employeeID :: Int<br />
manager :: (Maybe Employee)<br />
directReports :: [Employee]<br />
//This deriving clause provides a default instance implementation for<br />
//the Show type class for the Employee type. Essentially what this<br />
//means is that the Debug.show function will work with Employee<br />
//values to display a String representation of the value suitable for<br />
//debugging purposes.<br />
deriving Show;<br />
Note that fields (data constructor arguments) must have names in CAL, as well as their type. The deriving clause<br />
makes the data type work with functions defined in certain type classes. Here we ensure that CAL automatically adds<br />
the requisite logic to make values of Employee be renderable as strings for the purposes of tracing.<br />
Fields can be extracted in a variety of ways:<br />
a) Positionally in a case expression:<br />
/**<br />
* @arg employee<br />
* @return the employee's name<br />
*/<br />
employeeName :: Employee -> String;<br />
employeeName employee =<br />
case employee of