understanding typing. understanding ruby

Post on 14-May-2015

2.760 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

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

Understanding Typing, Understanding Ruby

Justin Lin

caterpillar@openhome.cc

http://openhome.cc

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

Speaker?

http://www.linkedin.com/in/caterpillar 3

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

• Before typing … you have to figure out

– the set of related values…

– and the set of their related operation…

5

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

• 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

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

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 ……

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

• 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

• The verbose Java also do type inference

somewhere, even not perfect … XD

12

List<Person>

Person

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

• Type declaration also provides type information.

• For clearness, dynamically-typed languages may need type information, no matter it‘s through libraries …

14

• comments …

• or even naming conventions.

15

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

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

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

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

Overloading in Dynamically-typed languages?

• Runtime type/property checking.

• Default values/option object.

20

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

Duck typing

• Quack like a duck…

• Statically-typed languages can't do that?

Structural typing

22

• Well…well…don't cheat on me … structural

typing is static duck typing …

• How about this?

Verbose, of course!! This is Java … XD

23

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

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

• After you enjoy its convenience, refactor

the code …

26

Instances of different classes?

• In Java, we'll define an interface for the

set of operations.

27

• 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

• From JDK8, we can define default methods in interface…

29

• In Ruby, what will you do?

Duck typing is flexible, but it doesn't mean that you don't need further thinking…

30

Generics

• Compilers are really noisy…

Shut up, Please! I know what I am doing, OK?

31

• 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

• 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

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

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

• 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

Type checking

Why does fill use repond_to?, not is_a?

38

• 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

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

– IDE Support

42

• Or even something like TypeScript

– tsc compile the code to JavaScript …

– A dynamically-typed language but …

43

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

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

• With all these great power in Ruby …

– Dynamically-typed

– Object individuation

– Open class

– Runtime introspection

– …

• What responsibility should you take?

46

"We're all consenting adults here."

NOT just a slogan.

With great power comes

great responsibility. Spider-Man (2002)

48

Understanding Typing, Understanding Ruby.

Thanks!!

49

top related