Short Introduction of Implicit Conversion by TIS, Inc.
-
Upload
scalaconfjp -
Category
Software
-
view
98 -
download
1
description
Transcript of Short Introduction of Implicit Conversion by TIS, Inc.
Copyright © 2014 TIS Inc. All rights reserved.
Sep. 6, 2014.
ScalaMatsuri 2014 LT
Atsushi Oku
TIS Inc.
Short introduction of
Implicit Conversion
Copyright © 2014 TIS Inc. All rights reserved. 1
About Me
• Programming in Java over 15 years
• A member of R&D Division
(Strategic Technology Center)
• Joined in our Scala research project in Mar. ‘14
Copyright © 2014 TIS Inc. All rights reserved. 2
About this LT
• I participated in Scala training courses offered by
Typesafe Inc.
– “Fast track to Scala” / “Advanced Scala”
• Through these trainings, I become to like
"Implicit Conversion" (and DSL) feature.
• So I ’ll try to introduce this feature and small
sample codes.
Copyright © 2014 TIS Inc. All rights reserved. 3
About training courses
• http://typesafe.com/how/trainings
• Some courses can be participated from
anywhere in the world "virtually"
– even if you are at home in Japan
– by using webcam, headset and VNC software
Copyright © 2014 TIS Inc. All rights reserved. 4
Implicit Conversion
• Why no error?
`1’ (Int) doesn’t have `->’ method
Copyright © 2014 TIS Inc. All rights reserved. 5
Implicit Conversion
• If compiler find a type error,
the compiler looks for implicit
conversion.
Copyright © 2014 TIS Inc. All rights reserved.
When does compiler look for ...
Type mismatch looking for implicit
conversion to the excepted type
(from Double to Int)
Receiver 'Int' doesn’t have `->' method
looking for implicit conversion
of the receiver (from Int to X which has
`->' method)
Ex1
Ex2
6
Copyright © 2014 TIS Inc. All rights reserved.
Define Implicit Conversion
• Any method name is OK
• Implicit conversion has to be in scope
in order to be applied.
7
Copyright © 2014 TIS Inc. All rights reserved.
Add new method to existing class
• New method `sub' is added to Int !
8
Copyright © 2014 TIS Inc. All rights reserved.
Implicit class
• Since Scala 2.10, `implicit class' is available
9
Copyright © 2014 TIS Inc. All rights reserved.
My First DSL
10
• I'd like to write codes in my DSL like this
• My simple Date class is like this
Copyright © 2014 TIS Inc. All rights reserved.
My First DSL
11
Copyright © 2014 TIS Inc. All rights reserved. 12
Please join our colleague's presentation
A-4 14:50 – 15:30
Scalable Generator: Using Scala in SIer Business
SIerに立ちはだかるScalaの壁に進化型ジェネレータで挑む
- TIS Yugo Maede
THANK YOU