understanding typing. understanding ruby
DESCRIPTION
- What is typing? - Pros and cons of type declarations. - What does quacking like a dock mean? - Static Typing vs Unit Testing. - Making us better at Ruby. 你可以在以下鏈結找到中文說明: http://www.codedata.com.tw/social-coding/understanding-typing-understanding-ruby/TRANSCRIPT
![Page 1: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/1.jpg)
Understanding Typing, Understanding Ruby
Justin Lin
http://openhome.cc
![Page 2: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/2.jpg)
What I'll cover
• What is typing?
• Pros and cons of type declarations.
• What does quacking like a dock mean?
• Static Typing vs Unit Testing.
• Making us better at Ruby.
2
![Page 3: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/3.jpg)
Speaker?
http://www.linkedin.com/in/caterpillar 3
![Page 4: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/4.jpg)
What is typing?
• Assigning a data type, what is called typing, gives meaning to a sequences of bits such as a value in memory or some object such as a variable.
4
![Page 5: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/5.jpg)
• Before typing … you have to figure out
– the set of related values…
– and the set of their related operation…
5
![Page 6: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/6.jpg)
Type checking
• Checking the constraints of types.
3.14 doesn't belong to Int.
The instance doesn't have the operation defined in Int.
6
![Page 7: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/7.jpg)
• Checking occurs at compile-time or runtime …
– Statically-typed languages(Haskell, Scala, Java)
– Dynamically-typed languages(JavaScript, Python, Ruby)
Checked before running doubleMe
Checked when running the method '*'
7
![Page 8: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/8.jpg)
Today is Ruby Conf,
if my memory serves me … :p
• You all know disadvantages of statically-
typed languages, such as …
– Type declaration is verbose, inexpressive, and
annoying.
– Even I see a bird that walks like a duck and
swims like a duck and quacks like a duck, I can't
call that bird a duck.
– The static typing doesn't ensure the function is
correct. Unit test is also required.
8
![Page 9: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/9.jpg)
Today is Ruby Conf,
if my memory serves me … :p
• You all know disadvantages of statically-
typed languages, such as …
– Type declaration is verbose, inexpressive, and
annoying.
– Even I see a bird that walks like a duck and
swims like a duck and quacks like a duck, I can't
call that bird a duck.
– The static typing doesn't ensure the function is
correct. Unit test is also required.
9
You know too much ……
![Page 10: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/10.jpg)
Type declaration
• Verbose, inexpressive, and annoying?
• Haskell is a statically-typed language. Type inference works here.
• For clearness, it's recommended to declare the type.
10
![Page 11: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/11.jpg)
• Type inference draws conclusions about the
types of variables based on how
programmers use those variables.
• It presents in more and more statically-typed
languages.
11
![Page 12: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/12.jpg)
• The verbose Java also do type inference
somewhere, even not perfect … XD
12
List<Person>
Person
![Page 13: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/13.jpg)
Martin Fowler
"One day I found myself trying to follow some well-written Ruby code. I found the lack of type information on parameters made life difficult - I kept saying to myself 'what exactly do I have here?'"
http://martinfowler.com/bliki/DynamicTyping.html
13
![Page 14: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/14.jpg)
• Type declaration also provides type information.
• For clearness, dynamically-typed languages may need type information, no matter it‘s through libraries …
14
![Page 15: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/15.jpg)
• comments …
• or even naming conventions.
15
![Page 16: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/16.jpg)
Function/method overloading
• Allows creating several methods with the
same name which differ from each other in
the type of the input (and the output) of the
function.
• Allows creating several methods with the
same name which have variable number of
arguments.
16
![Page 17: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/17.jpg)
What you want is Ad-hoc polymorphism?
• A polymorphic function can denote a number
of distinct and potentially heterogeneous
implementations depending on the type of
argument(s).
• Also known as function overloading.
17
![Page 18: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/18.jpg)
What you want is Ad-hoc polymorphism?
• A polymorphic function can denote a number
of distinct and potentially heterogeneous
implementations depending on the type of
argument(s).
• Also known as function overloading.
Subtype polymorphism
18
![Page 19: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/19.jpg)
What you want are default values?
• Java has no syntax for setting default values
for arguments. Overloading is a way.
It's, actually, a default value.
19
![Page 20: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/20.jpg)
Overloading in Dynamically-typed languages?
• Runtime type/property checking.
• Default values/option object.
20
![Page 21: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/21.jpg)
What I really need is a range-like object, not only a Range instance.
Am I having distinct implementations depending on the type/properties of argument(s)
21
![Page 22: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/22.jpg)
Duck typing
• Quack like a duck…
• Statically-typed languages can't do that?
Structural typing
22
![Page 23: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/23.jpg)
• Well…well…don't cheat on me … structural
typing is static duck typing …
• How about this?
Verbose, of course!! This is Java … XD
23
![Page 24: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/24.jpg)
Why do you need duck typing?
• Flexible … especially for start-up.
• When you take duck typing, you are using,
actually, a set of operations.
• Who does this set of operations belong to?
– Instances of some class?
– Instances of different classes?
24
![Page 25: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/25.jpg)
Instances of some class?
• Defining a singleton method is, actually,
defining the instance method in the
anonymous singleton class.
Open the anonymous singleton class of duck
25
![Page 26: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/26.jpg)
• After you enjoy its convenience, refactor
the code …
26
![Page 27: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/27.jpg)
Instances of different classes?
• In Java, we'll define an interface for the
set of operations.
27
![Page 28: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/28.jpg)
• When you take duck typing, you are using,
actually, a set of operations.
• When instances of different classes share
the same set of method implementations,
what will you do?
28
![Page 29: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/29.jpg)
• From JDK8, we can define default methods in interface…
29
![Page 30: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/30.jpg)
• In Ruby, what will you do?
Duck typing is flexible, but it doesn't mean that you don't need further thinking…
30
![Page 31: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/31.jpg)
Generics
• Compilers are really noisy…
Shut up, Please! I know what I am doing, OK?
31
![Page 32: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/32.jpg)
• Because you tell it to shut up ….
• Generics saves the world?
ClassCastException?
It’s all your fault.
Hey, you're doing something
stupid, and you can't tell me
to shut up…YA!!
32
![Page 33: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/33.jpg)
• Generics destroys the code!!
By Mario Fusco, http://www.slideshare.net/mariofusco/monadic-java 33
![Page 34: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/34.jpg)
• The loading of type declaration is taken by
API developers. The API clients get
convenience.
• And the work of type checking is pushed to
compiler-time. You avoid the runtime exception - ClassNotFoundException.
Parametric polymorphism
34
![Page 35: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/35.jpg)
Who is responsible for type checking?
• Supporters of statically-typed languages
– Compilers perform comprehensive type
checking at compiler-time. Ensure that some
errors due to wrong types can't happen at
runtime.
• Supporters of dynamically-typed languages
– Passing appropriate types to the function
provides no guarantee that the result of the
function will be the correct value. Unit Test can
ensure types and behavior is correct.
35
![Page 36: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/36.jpg)
Static Typing vs Unit Testing
• Compilers do type assertion cheaply or even
for free – if you have good type inference.
• The more type-error messages, the more
you have to think about types to pass
compilers.
• It's especially true when you're writing Haskell.
36
![Page 37: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/37.jpg)
• When using dynamically-typed languages,
you ease the burden of declaring types, but
were bent with the burden of type checking.
• Do you know …
– Real requirements of type checking in your code?
– What types are in the language?
– How to do unit test for type checking?
• Would you like and have the ability to write
more unit test to ensure types and behavior
is correct?
• Are you writing unit test?
37
![Page 38: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/38.jpg)
Type checking
Why does fill use repond_to?, not is_a?
38
![Page 39: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/39.jpg)
• How to unit test doubleMe?
• Double whom? Fixnum? String? Array?
Range? Ouch …it doesn't define :+ method.
• If an object doesn't define :+ method, how
to deal with? method_missing?
• Ask yourself more questions about types
when unit test.
39
![Page 40: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/40.jpg)
Joshua Bloch
"Given how hard it is to write correct programs, we need all the help we can get. So anything that removes potential bugs from our plate is good. That’s why I’m very much a believer in static typing and in static analysis"
《Coders at work》
40
![Page 41: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/41.jpg)
• Figure out your requirements of type checking,
look for tools in Ruby eco-system like …
– python-rightarrow
– PEP-3107 -- Function Annotations
41
![Page 42: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/42.jpg)
– IDE Support
42
![Page 43: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/43.jpg)
• Or even something like TypeScript
– tsc compile the code to JavaScript …
– A dynamically-typed language but …
43
![Page 44: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/44.jpg)
It's almost time to draw a conclusion
• One more question, left for you…
• Think about difference between checked
exceptions and unchecked exceptions,
an exclusive feature in Java … XD
• It'll help you think about how to deal with
exceptions in Ruby …
44
![Page 45: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/45.jpg)
Making us better at Ruby
• Using dynamically-typed languages needs
understanding statically-typed languages.
• Knowing the advantages of statically-typed
languages furthers learning pros and cons of
dynamically-typed languages.
• Recognizing the restrictions of statically-typed
languages helps digging the responsibilities
you should take in dynamically-typed
languages.
45
![Page 46: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/46.jpg)
• With all these great power in Ruby …
– Dynamically-typed
– Object individuation
– Open class
– Runtime introspection
– …
• What responsibility should you take?
46
![Page 47: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/47.jpg)
"We're all consenting adults here."
NOT just a slogan.
![Page 48: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/48.jpg)
With great power comes
great responsibility. Spider-Man (2002)
48
![Page 49: Understanding Typing. Understanding Ruby](https://reader034.vdocument.in/reader034/viewer/2022042513/5554b00cb4c90502618b5578/html5/thumbnails/49.jpg)
Understanding Typing, Understanding Ruby.
Thanks!!
49