Scala Goodness: Structural Typing

Structural typing in Scala is the way to describe types by their structure, not by their name as with other typing. Structural typing reduces coupling and the need for inheritance. In Java you would mainly use interfaces instead of structural typing.

I go with the same examples as in “Scala Goodness: Compound types” so it’s easier to compare both solutions.

c:{ def call():Unit } describes a type which has a call method that returns nothing – like void in Java. The method does take all objects of types that satisfies this constraint. We can now use call on both classes:

You can also give your structural type a name with type aliasing:

With the same classes from above we get:

Both ways work with more than one method:

Calling with a new dog results in:

Structural typing is very useful if you are not able to modify classes to make them implement a trait or interface or if you want to reduce coupling and increase reuse. How does this relate to interfaces? The benefit of interfaces instead of structural typing is how they describe the roles of a class.

instead of

Scala glory!

See also:

176 thoughts on “Scala Goodness: Structural Typing”

  1. Pingback: best
  2. Pingback: fucoxanthin
  3. Pingback: geniux pills
  4. Pingback: intellux
  5. Pingback: probrain
  6. Pingback: radian-c
  7. Pingback: supreme antler
  8. Pingback: genius reviews
  9. Pingback: testomax
  10. Pingback: TRACKR BRAVO
  11. Pingback: alpha f1
  12. Pingback: alpha fuel
  13. Pingback: alpha man pro
  14. Pingback: alpha peak
  15. Pingback: alpha xtrm
  16. Pingback: caralluma extract
  17. Pingback: anabolic rx24
  18. Pingback: cognifocus
  19. Pingback: max synapse
  20. Pingback: nerventrax
  21. Pingback: nutra forskolin
  22. Pingback: premium cleanse
  23. Pingback: testorip
  24. Pingback: testoroar
  25. Pingback: zynev
  26. Pingback: forskolin slim
  27. Pingback: garcinia cleanse
  28. Pingback: garcinia xt
  29. Pingback: forskolin fuel
  30. Pingback: vitapulse
  31. Pingback: Vita pulse
  32. Pingback: vitapulse review
  33. Pingback: pure colon detox
  34. Pingback: somnapure
  35. Pingback: perfect biotics
  36. Pingback: reviews vitapulse
  37. Pingback: vitapulse vitamins
  38. Pingback: vitapulse scam
  39. Pingback: vitapulse
  40. Pingback: vitapulse
  41. Pingback: vita pulse
  42. Pingback: probiotic america
  43. Pingback: the bull
  44. Pingback: Destroy Depression
  45. Pingback: Brain Revitalizer
  46. Pingback: Bodyweight Bundle
  47. Pingback: Panic Away reviews
  48. Pingback: Ecomaxx Review
  49. Pingback: T90 Xplode
  50. Pingback: Maxtropin Reviews
  51. Pingback: Tea Tox Reviews
  52. Pingback: Tai Cheng
  53. Pingback: MegaDrox
  54. Pingback: Test X180
  55. Pingback: Test X180 Reviews
  56. Pingback: probiotic america
  57. Pingback: useful reference
  58. Pingback: check over here
  59. Pingback: useful reference
  60. Pingback: Piano For All
  61. Pingback: Check This Out
  62. Pingback: have a peek here
  63. Pingback: this content
  64. Pingback: check here
  65. Pingback: read this article
  66. Pingback: click site
  67. Pingback: click site
  68. Pingback: Get More Info
  69. Pingback: this contact form
  70. Pingback: Read More Here
  71. Pingback: additional hints
  72. Pingback: their explanation
  73. Pingback: official site
  74. Pingback: other
  75. Pingback: visit
  76. Pingback: additional hints
  77. Pingback: my response
  78. Pingback: read more
  79. Pingback: directory
  80. Pingback: here
  81. Pingback: Magic Of Making Up
  82. Pingback: official site
  83. Pingback: Post Free Ads
  84. Pingback: additional hints
  85. Pingback: anchor
  86. Pingback: internet
  87. Pingback: here
  88. Pingback: Visit Website
  89. Pingback: 60 day fix review
  90. Pingback: click here
  91. Pingback: click here
  92. Pingback: tires
  93. Pingback: rims
  94. Pingback: wheels

Comments are closed.