$30 off During Our Annual Pro Sale. View Details »

The advantage of using ’Eff’ in Scala Project

The advantage of using ’Eff’ in Scala Project

ma2k8
PRO

June 28, 2019
Tweet

More Decks by ma2k8

Other Decks in Programming

Transcript

  1. The advantage of using ’Eff’ in Scala Project Scala Matsuri

    2019 @ma2k8 4DBMBϓϩδΣΫτͰ&⒎Λ࢖༻͢Δར఺
  2. About Me ▶Tsubasa Matsukawa - SWE&SRE at Recruit Marketing Partners

    Co.,Ltd. & Quipper Co.,Ltd. @wing_007 ϦΫϧʔτϚʔέςΟϯάύʔτφʔζ݉2VJQQFSॴଐͰ 48&ͱ43&ྖҬΛ୲౰͍ͯ͠·͢ɻ
  3. Our Products ▶Study Sapuri English - English learning app [

    Web/iOS/Android ] ओʹελσΟαϓϦ&OHMJTIͷ։ൃΛ͍ͯ͠·͢ɻ ӳޠֶशʹڵຯ͕͋Δํ͸ੋඇ
  4. Scala has an elegant syntactic sugar ‘for expression’. To make

    the most of this force…. 4DBMBʹ͸ૉఢͳ౶ҥߏจGPS͕ࣜ͋Γ·͢ɻ ͦͷྗΛ࠷େݶར༻͢ΔͨΊʹɾɾɾ
  5. We needed to decided which ‘types’ to primarily use to

    when starting Scala projects. ࢲୡ͸4DBMBͷϓϩδΣΫτΛ։࢝͢ΔࡍɺओʹͲͷʮܕʯΛ ࢖༻͢Δͷ͔ΛܾΊΔඞཁ͕͋Γ·͢ɻ
  6. These ‘types’ are often chosen. ▶Future[A] ▶Future[Try[A]] ▶Future[E Either A]

    ▶EitherT[Future, E, A] ※There is also case that uses "Task" instead of "Future" ͜ͷΑ͏ͳܕ͕Α͘બ͹ΕΔͰ͠ΐ͏ɻ 'VUVSFͷ୅ΘΓʹ5BTLΛ࢖͏͜ͱ΋͋Γ·͢
  7. Our team is trying…. Eff[R, A] ࢲୡͷνʔϜͰ͸&⒎Λࢼ͍ͯ͠·͢

  8. Each pros and cons were…? ͦΕͧΕͷϝϦοτσϝϦοτ͸ʁ

  9. Future[A]

  10. ▶ Easy ௕ॴ͸؆୯͞ʹਚ͖·͢ɻ Pros of Future[A]

  11. Cons of Future[A] ▶ Rough error handling. ▶ Semaphore hard.

    ※Task solve Semaphore problem. ୹ॴ͸ηϚϑΥͷ੍ޚ͕೉͍͠఺ͱɺ ΤϥʔϋϯυϦϯά͕େࡶ೺ʹ͔͠ߦ͑ͳ͍఺Ͱ͢ɻ
  12. Future[Try[A]] ɹ

  13. Pros of Future[Try[A]] ▶ Error handling is possible. ௕ॴ͸ΤϥʔϋϯυϦϯά͕ՄೳʹͳΔ఺Ͱ͢ɻ

  14. Cons of Future[Try[A]] ▶ Only throwable can be used ▶

    Syntax is not simple(Coding 'try ~ catch' every time is very troublesome. ୹ॴ͸ΤϥʔϋϯυϦϯάʹ࢖༻Ͱ͖Δܕ͕ 5ISPXBCMFʹݶΔ఺ͱγϯλοΫε͕ෳࡶʹͳΔ఺Ͱ͢ɻ
  15. Personally, I often use ‘Try’ when wrapping Java libraries, but

    since I convert to ‘Either’, they do not appear in signatures. ݸਓతʹ͸5SZ͸+BWBϥΠϒϥϦͷ8SBQͰΑ͘࢖͍·͕͢ɺ &JUIFSܕʹม׵͠ɺγάχνϟʹ͸ग़͠·ͤΜɻ
  16. Future[E Either A]

  17. Pros of Future[E Either A] ▶ Flexible Error handling is

    possible. ▶ E is ADT can be flexible expression. ▶ Every time force error handling. ௕ॴ͸5SZΑΓ΋ॊೈͳΤϥʔϋϯυϦϯά͕ ՄೳʹͳΔ఺Ͱ͢ɻ
  18. Cons of Future[E Either A] ▶ Rough error handling. ▶

    Every time need to match the type even if you don't need error handling. ୹ॴ͸ΤϥʔϋϯυϦϯά͕ ෆཁͳՕॴͰ΋ܕ߹Θ͕ͤඞཁʹͳΔ఺Ͱ͢ɻ
  19. EitherT[Future, E, A]

  20. Pros of EitherT[Future, E, A] ▶ Can solve part of

    the complexity of nesting. ௕ॴ͸͋Δఔ౓ωετͯ͠΋ GPSࣜΛγϯϓϧʹอͯΔ఺Ͱ͢ɻ
  21. Cons of EitherT[Future, E, A] ▶ It gets slower as

    types are stacked. ▶ May change results in evaluation order. ▶ Too many lift. ୹ॴ͸཯଎໰୊ɺධՁॱͰ݁Ռ͕มΘΔՄೳੑ͕͋ΓɺϞφυ ελοΫʹ٧Ήܕ͕૿͑Δͱɺܕ߹Θ͕ͤ൥ࡶʹͳΔ఺Ͱ͢ɻ
  22. Eff[R, A]

  23. Pros of Eff[R, A] ▶ ‘Eff’ can solve other ‘types’

    disadvantage. ▶ Simple syntax. ▶ Can simply use various effects. ▶ Can solved performance problem when types are stacked. ▶ A result doesn't change for the order of evaluation. &⒎͸͜Ε·Ͱ঺հͨ͠ܕͷσϝϦοτͷ େ෦෼ΛղফͰ͖·͢ɻ
  24. Cons of Eff[R, A] ▶’Eff’ is very simple syntax. But

    not easy. ▶ difficult effects wire. ▶ Anything is lazy evaluation. ▶ Complexity of type specification. &⒎ͷσϝϦοτ͸ɺ؆୯Ͱ͸ͳ͍఺Ͱ͢ɻ ͋Δఔ౓ͷ׳Ε͸ඞཁͰ͠ΐ͏ɻ
  25. Our project used ‘Eff’ on clean architecture. զʑͷϓϩδΣΫτͰ͸ɺ$MFBO"SDIJUFDUVSFͰઃܭ͠ɺ Ҿ͖ճ͢ܕʹ&⒎Λ࢖༻͢Δߏ੒Λಋೖ͍ͯ͠·͢ɻ

  26. ͜ͷΑ͏ʹෳ਺ͷޮՌΛϑϥοτʹهड़Ͱ͖ΔͷͰɺ γϯϓϧʹهड़Ͱ͖͍ͯ·͢ɻ

  27. How to use ‘Eff’ &⒎ͷ࢖͍ํ Library: https://github.com/atnos-org/eff

  28. Define ADT "%5Λఆٛ͠·͢ɻ

  29. Define Smart-constructer "%5Λ&⒎ʹੵΉεϚʔτίϯετϥΫλΛఆٛ͠·͢ɻ

  30. ੵΜͩ"%5Λղऍ͢ΔΠϯλϓϦλΛఆٛ͠·͢ɻ Define Interpreter

  31. Ready to use ‘Eff’. ͜ΕͰ&⒎Λར༻͢Δ४උ͕Ͱ͖·ͨ͠ɻ

  32. &⒎Λར༻ͨ͠ϓϩάϥϜΛॻ͖·͢ɻ Write program!!

  33. ΠϯλϓϦλʹ͔͚ͯ&⒎Λ࣮ߦ͠·͢ɻ SVO99ͷؔ਺͸ͦΕͧΕJNQMJDJUDPOWFSTJPOͰੜ΍ͯ͠·͢ɻ Run program!!

  34. ‘Eff’ is Good! &⒎͸͍͍ͧʂʢ঺հهࣄͱίʔυαϯϓϧష͓͖ͬͯ·͢ Introduction article: https://tech.recruit-mp.co.jp/server-side/post-18728/ɹ Code sample: https://github.com/ma2k8/eff-arch-example

  35. Thanks! ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ