Introduction to Scala

1,892 views
1,790 views

Published on

An introduction to Scala features. See associated code at http://tr.im/jA6A

Published in: Business, Technology
1 Comment
3 Likes
Statistics
Notes
  • Get example code at http://tr.im/jA6A
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,892
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
Downloads
51
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Introduction to Scala

  1. 1. <ul><li>A programming language for the JVM (and .net)
  2. 2. Functional / OO hybrid
  3. 3. Statically typed
  4. 4. Compile to class files
  5. 5. Iterpretive Console
  6. 6. Flexible syntax </li></ul>What is it?
  7. 7. <ul><li>'Scalable Language' </li><ul><li>On the JVM
  8. 8. Integrates with existing libraries
  9. 9. Integrates readily with other systems (in code XML & JSON support)
  10. 10. Flexible syntax – can be like Java or can move to DSLs and higher abstractions. </li></ul></ul>The name
  11. 11. <ul><li>Removes Java roadblocks without changing platform or throwing away existing properties </li><ul><li>Tasks that are difficult or tedious in Java can be simplified with minimal investment in time and tools </li></ul><li>Rapid development without losing type safety.
  12. 12. Java + new language features: </li><ul><li>Closures, actors, infix operators, case classes, pattern matching, list and map operations, native XML, implicits, defaults, nested elements, no primitives, currying, sequence comprehension, compound types, structural types, function literals … etc </li></ul></ul>Why should we care?
  13. 13. <ul><li>Code example - Hello World </li><ul><li>Points of interest: </li><ul><li>Object keyword
  14. 14. Def keyword
  15. 15. Assignment of code block to function
  16. 16. scala.Console
  17. 17. Application trait </li></ul></ul></ul>See associated source files at http://tr.im/jA6A HelloWorld.scala & HelloWorldWithTrait.scala
  18. 18. <ul><li>Using the basic building blocks, we can compose code that looks and feels similar to today's Java. </li><ul><li>Though not source compatible. Can't compile Java. </li></ul><li>Package, import, class, object, trait, var, val, def
  19. 19. Code example: Cat </li><ul><li>Relaxed naming constraints, constructor arg visibility, constructor overload, function definition, implied static typing, val vs var </li></ul></ul>The basic building blocks Cat.scala
  20. 20. <ul><li>Can be very terse
  21. 21. Return type implies type of function
  22. 22. Naming is greatly relaxed.
  23. 23. Code sample: Rational </li><ul><li>Symbols as function names -> infix operators, optional 'return' keyword, if statements are ternary, DSLs </li></ul></ul>Functions Rational.scala
  24. 24. <ul><li>XML is a native datatype
  25. 25. Rich XML operations make parsing and creating XML simple.
  26. 26. Code sample: NativeXML </li><ul><li>Eclipse plugin slightly broken – parentheses not required, XML direct in code, nested values, iterated lists, xpath-like functions </li></ul></ul>Native XML NativeXML.scala
  27. 27. <ul><li>Implicit functions map one type to another
  28. 28. Ascribes new functionality to old friends </li><ul><li>e.g. Make the JDBC library user-friendly </li></ul><li>Code sample: UpperCaseAwareString </li><ul><li>RichString already exists
  29. 29. Implicit def declaration
  30. 30. Multiple impicit defs, but method calls must be unambiguous at compilation </li></ul></ul>Extending other classes – Monkey Patching UpperCaseCharAwareString.scala
  31. 31. <ul><li>Compile time checked duck typing
  32. 32. Can define types based on functions signatures
  33. 33. Code sample: Duck </li><ul><li>Type keyword, multiple method definitions, includes param and return types, can be inlined </li></ul></ul>Structural types – Duck Typing Duck.scala
  34. 34. <ul><li>More than interfaces
  35. 35. Less than classes
  36. 36. Act as markers
  37. 37. Can have methods
  38. 38. Can have constructors
  39. 39. Code sample: Bravia </li><ul><li>Extend abstract or trait, mixins </li></ul></ul>Traits Bravia.scala
  40. 40. <ul><li>Define combinations of types in contracts
  41. 41. Code sample: Chess </li><ul><li>Compile time type checking for multiple types </li></ul></ul>Compound types chess.Chess.scala
  42. 42. <ul><li>Case objects are akin to enums
  43. 43. Case classes are parameterisable
  44. 44. Pattern Matching is akin to switch statement, but more powerful
  45. 45. Code sample: Months </li><ul><li>No new keyword, no params for object - params for class, match & case, _ wildcard, exposed params when matching on classes, also used for exception handling </li></ul></ul>Case objects/classes and pattern matching Months.scala
  46. 46. <ul><li>Functions are objects. Everything is!
  47. 47. Can access parent scope (closure)
  48. 48. Can be defined inline ('function literal')
  49. 49. Code sample: BookReader </li><ul><li>Function as a parameter, function knows and can modify instance values, functions defined inline. </li></ul></ul>Functions are objects too BookReader.scala
  50. 50. <ul><li>Any number of enumerations and filters mixed together to form a new list.
  51. 51. Enumerations add values, filters remove them.
  52. 52. Lightweight notation
  53. 53. Code sample: Condiments </li><ul><li>Note generics, note also list creation. </li></ul></ul>Sequence Comprehensions Condiments.scala
  54. 54. <ul><li>Functions with many params can be decomposed into a chain of functions.
  55. 55. This is supported and used to construct language extensions.
  56. 56. Code sample: AutoResourceManagement </li><ul><li>Partial function takes var args of Closable type, chained function takes a function that takes no params and returns nothing (Unit), makes AOP mainly redundant, note foreach and exception syntax. </li></ul></ul>Currying AutoResourceManagement.scala
  57. 57. <ul><li>www.scala-lang.org </li><ul><li>Binaries, source, tutorials and doco </li></ul><li>[email_address] - mailing list
  58. 58. http://groups.google.com/group/scala-melb - Scala Melbourne User Group
  59. 59. http://tr.im/jzQK – Scala for Java devs. </li></ul>Getting started
  60. 60. <ul><li>Testing </li><ul><li>http://www.artima.com/scalatest/
  61. 61. http://code.google.com/p/specs/
  62. 62. http://code.google.com/p/scalacheck/ </li></ul><li>Building </li><ul><li>http://code.google.com/p/simple-build-tool/ </li></ul><li>IDE Support </li><ul><li>Eclipse, NetBeans, IntelliJ </li></ul></ul>Tools

×