14
Copyright © 2014 TIS Inc. All rights reserved. Sep. 6, 2014. ScalaMatsuri 2014 LT Atsushi Oku TIS Inc. Short introduction of Implicit Conversion

Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

Embed Size (px)

DESCRIPTION

ScalaMatsuri 2014 LT

Citation preview

Page 1: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

Copyright © 2014 TIS Inc. All rights reserved.

Sep. 6, 2014.

ScalaMatsuri 2014 LT

Atsushi Oku

TIS Inc.

Short introduction of

Implicit Conversion

Page 2: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

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

Page 3: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

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.

Page 4: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

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

Page 5: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

Copyright © 2014 TIS Inc. All rights reserved. 4

Implicit Conversion

• Why no error?

`1’ (Int) doesn’t have `->’ method

Page 6: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

Copyright © 2014 TIS Inc. All rights reserved. 5

Implicit Conversion

• If compiler find a type error,

the compiler looks for implicit

conversion.

Page 7: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

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

Page 8: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

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

Page 9: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

Copyright © 2014 TIS Inc. All rights reserved.

Add new method to existing class

• New method `sub' is added to Int !

8

Page 10: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

Copyright © 2014 TIS Inc. All rights reserved.

Implicit class

• Since Scala 2.10, `implicit class' is available

9

Page 11: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

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

Page 12: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

Copyright © 2014 TIS Inc. All rights reserved.

My First DSL

11

Page 13: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

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

Page 14: Short Introduction of Implicit Converion (ScalaMatsuri2014 LT)

THANK YOU