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:

66 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

Comments are closed.