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