one or two things you may not know about typesystems
TRANSCRIPT
![Page 1: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/1.jpg)
one or two things
phillip calçado http://fragmental.tw
you may not know
about type systems
![Page 2: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/2.jpg)
myths
![Page 3: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/3.jpg)
myth:type systems are just syntax checkers
![Page 4: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/4.jpg)
“The fundamental purpose of a type system is to prevent the occurrence of execution errors during the running of a program.”
- Luca Cardelli, Type Systems
![Page 5: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/5.jpg)
what kind oferrors?
![Page 6: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/6.jpg)
package org.apache.commons.lang.time;
public class DateUtils { public static boolean isSameDay(Date date1, Date date2) { if (date1 == null || date2 == null) { throw new IllegalArgumentException("The date must not be null"); } return verifySameDay(date1, date2); }}
![Page 7: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/7.jpg)
package org.apache.commons.lang.time;
public class DateUtils { public static boolean isSameDay(Date date1, Date date2) { if (date1 == null || date2 == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); return isSameDay(cal1, cal2); }}
but why would it be allowed to be null in
the first place?
![Page 8: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/8.jpg)
use System;
public class DatePrinter { public static void Main(string[] args) { Print(new DateTime()); }
public static void Print(DateTime d) { Console.WriteLine(d); }}
![Page 9: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/9.jpg)
use System;
public class DatePrinter { public static void Main(string[] args) { Print(new DateTime()); }
public static void Print(DateTime d) { Console.WriteLine(d); }}
☑
![Page 10: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/10.jpg)
use System;
public class DatePrinter { public static void Main(string[] args) { Print(null); }
public static void Print(DateTime d) { Console.WriteLine(d); }}
![Page 11: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/11.jpg)
use System;
public class DatePrinter { public static void Main(string[] args) { Print(null); }
public static void Print(DateTime d) { Console.WriteLine(d); }}
☒pcalcado@pcalcado:awayday2009$gmcsDatePrinter.csDatePrinter.cs(7,5):errorCS1502:Thebestoverloadedmethodmatchfor`DatePrinter.Print(System.DateTime)'hassomeinvalidargumentsDatePrinter.cs(10,22):(Locationofthesymbolrelatedtopreviouserror)DatePrinter.cs(7,5):errorCS1503:Argument`#1'cannotconvert`null'expressiontotype`System.DateTime'Compilationfailed:2error(s),0warnings
![Page 12: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/12.jpg)
use System;
public class DatePrinter { public static void Main(string[] args) { Print(null); }
public static void Print(DateTime? d) { Console.WriteLine(d); }}
![Page 13: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/13.jpg)
use System;
public class DatePrinter { public static void Main(string[] args) { Print(null); }
public static void Print(DateTime? d) { Console.WriteLine(d); }}
☑
![Page 14: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/14.jpg)
“I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system [...] My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference [...] This has led to innumerable errors [...] which have probably caused a billion dollars of pain and damage in the last forty years. In recent years, a number of program analysers [...] in Microsoft have been used to check references, and give warnings if there is a risk they may be non-null. More recent programming languages like Spec# have introduced declarations for non-null references. This is the solution, which I rejected in 1965.”
- C.A.R. Hoare
![Page 15: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/15.jpg)
dynamic means weak
myth:
![Page 16: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/16.jpg)
what isweak?
![Page 17: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/17.jpg)
“typeful programming advocates static typing, as much as possible, and dynamic typing when necessary; the strict observance of either or both of these techniques leads to strong typing, intended as the absence of unchecked run-time type errors.”
- Luca Cardelli, Typeful Programming
![Page 18: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/18.jpg)
unchecked run-time type errors
![Page 19: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/19.jpg)
pcalcado@pcalcado:~$php‐aInteractivemodeenabled<?php$i_am_a_string="see?";$weird_result=100+$i_am_a_string+20;echo$weird_result."\n";echo$i_am_a_string."\n";?>120see?
![Page 20: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/20.jpg)
$weird_result=100+“see”+20;
unchecked run-time type errors
=>120
![Page 21: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/21.jpg)
pcalcado@pcalcado:~$irb>>weird_result=100+"see?"+20TypeError:Stringcan'tbecoercedintoFixnum from(irb):1:in`+' from(irb):1>>
![Page 22: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/22.jpg)
static means safe
myth:
![Page 23: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/23.jpg)
“typeful programming advocates static typing, as much as possible, and dynamic typing when necessary; the strict observance of either or both of these techniques leads to strong typing, intended as the absence of unchecked run-time type errors.”
- Luca Cardelli, Typeful Programming
![Page 24: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/24.jpg)
“typeful programming advocates static typing, as much as possible, and dynamic typing when necessary; the strict observance of either or both of these techniques leads to strong typing, intended as the absence of unchecked run-time type errors.”
- Luca Cardelli, Typeful Programming
![Page 25: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/25.jpg)
$weird_result=100+“see”+20;=>120
type error:
![Page 26: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/26.jpg)
public class NoError{ public static void main(String[] args){ Triangle t = new Triangle(); t.addVertex(0,0); t.addVertex(10,10); t.addVertex(20,20); t.addVertex(30,30); System.out.println("Your triangle has "+ t.getVertices().size() + " vertices"); }}
class Triangle{private List<int[]> vertices = new ArrayList<int[]>();
public void addVertex(int x, int y){ vertices.add(new int[]{x, y}); } public List<int[]> getVertices(){ return vertices; }}
![Page 27: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/27.jpg)
no type error:=>Yourtrianglehas4vertices
![Page 28: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/28.jpg)
myth: static means
bureaucratic
![Page 29: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/29.jpg)
public class Sum {
public static int add(int a, int b) { return a + b; }
}
![Page 30: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/30.jpg)
Is this hypothetical language dynamic or static?
add(a, b) { return a + b}
![Page 31: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/31.jpg)
Dynamic
def add(a, b) a + bend
Ruby
![Page 32: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/32.jpg)
Static
((a, b) => a + b)
C#
![Page 33: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/33.jpg)
Dynamic
(defn add [a b] (+ a b))
Clojure
![Page 34: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/34.jpg)
Static
add a b = a + b
Haskell
![Page 35: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/35.jpg)
static can be smart
![Page 36: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/36.jpg)
add a b = a + b
Prelude>:loadadd.hs[1of1]CompilingMain(add.hs,interpreted)Ok,modulesloaded:Main.*Main>:typeaddadd::(Numa)=>a‐>a‐>a*Main>:type(add12)(add12)::(Numt)=>t*Main>
![Page 37: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/37.jpg)
myth:
only dynamic is flexible
![Page 38: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/38.jpg)
>>my_func()NoMethodError:undefinedmethod`my_func'formain:Object from(irb):1>>instance_eval("defmy_func()\nputs666\nend")=>nil>>my_func()666=>nil>>
![Page 39: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/39.jpg)
![Page 40: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/40.jpg)
“we think that people use eval as a poor man’s substitute for higher-order functions. Instead of passing around a function and call it, they pass around a string and eval it. [...] A final use of eval that we want to mention is for partial evaluation,multi-stage programming, or meta programming. We argue that in that case strings are not really the most optimal structure to represent programs and it is much better to use programs to represent programs, i.e. C++-style templates, quasiquote/unquote as in Lisp, or code literals as in the various multi-stage programming languages.”
- The End of the Cold War Between Programming Languages, Erik Meijer and Peter Drayton
![Page 41: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/41.jpg)
main = runBASIC $ do 10 GOSUB 1000 20 PRINT "* Welcome to HiLo *" 30 GOSUB 1000
100 LET I := INT(100 * RND(0)) 200 PRINT "Guess my number:" 210 INPUT X 220 LET S := SGN(I-X) 230 IF S <> 0 THEN 300
240 FOR X := 1 TO 5 250 PRINT X*X;" You won!" 260 NEXT X 270 STOP
300 IF S <> 1 THEN 400 310 PRINT "Your guess ";X;" is too low." 320 GOTO 200
400 PRINT "Your guess ";X;" is too high." 410 GOTO 200
1000 PRINT "*******************" 1010 RETURN
9999 END
![Page 42: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/42.jpg)
652 lines of Haskell
![Page 43: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/43.jpg)
what does the future hold?
![Page 44: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/44.jpg)
does typing matter?
![Page 45: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/45.jpg)
=>typing influences language features and tools
YES=>static typing is being wrongly bashed because of C#/Java just as dynamic was bashed because of PHP/Perl=>schools are merging (e.g. C# 4) and it’s important to know each one’s sweet spot
![Page 46: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/46.jpg)
=>saying that something is static or dynamic doesn’t tell much about what it can do
NO=>most nice features in Python/Ruby/JavaScript are related to meta-model, not typing=>Java/C# are bureaucratic for historical reasons, not limitations on typing
![Page 47: one or two things you may not know about typesystems](https://reader036.vdocument.in/reader036/viewer/2022081514/554f8df2b4c9052a518b522f/html5/thumbnails/47.jpg)
refs:=>http://pico.vub.ac.be/~wdmeuter/RDL04/papers/Meijer.pdf=>http://lucacardelli.name/Papers/TypefulProg.A4.pdf=>http://sadekdrobi.com/2008/12/22/null-references-the-billion-dollar-mistake/
=>http://www.flickr.com/photos/twindx/=>http://www.flickr.com/photos/darwinbell/=>http://www.flickr.com/photos/wainwright/=>http://www.flickr.com/photos/fikirbaz/