Slide 1

Slide 1 text

The advantage of using ’Eff’ in Scala Project Scala Matsuri 2019 @ma2k8 4DBMBϓϩδΣΫτͰ&⒎Λ࢖༻͢Δར఺

Slide 2

Slide 2 text

About Me ▶Tsubasa Matsukawa - SWE&SRE at Recruit Marketing Partners Co.,Ltd. & Quipper Co.,Ltd. @wing_007 ϦΫϧʔτϚʔέςΟϯάύʔτφʔζ݉2VJQQFSॴଐͰ 48&ͱ43&ྖҬΛ୲౰͍ͯ͠·͢ɻ

Slide 3

Slide 3 text

Our Products ▶Study Sapuri English - English learning app [ Web/iOS/Android ] ओʹελσΟαϓϦ&OHMJTIͷ։ൃΛ͍ͯ͠·͢ɻ ӳޠֶशʹڵຯ͕͋Δํ͸ੋඇ

Slide 4

Slide 4 text

Scala has an elegant syntactic sugar ‘for expression’. To make the most of this force…. 4DBMBʹ͸ૉఢͳ౶ҥߏจGPS͕ࣜ͋Γ·͢ɻ ͦͷྗΛ࠷େݶར༻͢ΔͨΊʹɾɾɾ

Slide 5

Slide 5 text

We needed to decided which ‘types’ to primarily use to when starting Scala projects. ࢲୡ͸4DBMBͷϓϩδΣΫτΛ։࢝͢ΔࡍɺओʹͲͷʮܕʯΛ ࢖༻͢Δͷ͔ΛܾΊΔඞཁ͕͋Γ·͢ɻ

Slide 6

Slide 6 text

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Λ࢖͏͜ͱ΋͋Γ·͢

Slide 7

Slide 7 text

Our team is trying…. Eff[R, A] ࢲୡͷνʔϜͰ͸&⒎Λࢼ͍ͯ͠·͢

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Future[A]

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Future[Try[A]] ɹ

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Cons of Future[Try[A]] ▶ Only throwable can be used ▶ Syntax is not simple(Coding 'try ~ catch' every time is very troublesome. ୹ॴ͸ΤϥʔϋϯυϦϯάʹ࢖༻Ͱ͖Δܕ͕ 5ISPXBCMFʹݶΔ఺ͱγϯλοΫε͕ෳࡶʹͳΔ఺Ͱ͢ɻ

Slide 15

Slide 15 text

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ܕʹม׵͠ɺγάχνϟʹ͸ग़͠·ͤΜɻ

Slide 16

Slide 16 text

Future[E Either A]

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

EitherT[Future, E, A]

Slide 20

Slide 20 text

Pros of EitherT[Future, E, A] ▶ Can solve part of the complexity of nesting. ௕ॴ͸͋Δఔ౓ωετͯ͠΋ GPSࣜΛγϯϓϧʹอͯΔ఺Ͱ͢ɻ

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Eff[R, A]

Slide 23

Slide 23 text

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. &⒎͸͜Ε·Ͱ঺հͨ͠ܕͷσϝϦοτͷ େ෦෼ΛղফͰ͖·͢ɻ

Slide 24

Slide 24 text

Cons of Eff[R, A] ▶’Eff’ is very simple syntax. But not easy. ▶ difficult effects wire. ▶ Anything is lazy evaluation. ▶ Complexity of type specification. &⒎ͷσϝϦοτ͸ɺ؆୯Ͱ͸ͳ͍఺Ͱ͢ɻ ͋Δఔ౓ͷ׳Ε͸ඞཁͰ͠ΐ͏ɻ

Slide 25

Slide 25 text

Our project used ‘Eff’ on clean architecture. զʑͷϓϩδΣΫτͰ͸ɺ$MFBO"SDIJUFDUVSFͰઃܭ͠ɺ Ҿ͖ճ͢ܕʹ&⒎Λ࢖༻͢Δߏ੒Λಋೖ͍ͯ͠·͢ɻ

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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