4. “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
6. 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);
}
}
7. package org.apache.commons.lang.time;
public class DateUtils {
but why would it be
public static boolean isSameDay(Date date1, Date
date2) {
if (date1 == null || date2 == null) {
allowed to be null in
throw new IllegalArgumentException("The date
must not be null");
}
the first place?
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
return isSameDay(cal1, cal2);
}
}
8. use System;
public class DatePrinter {
public static void Main(string[] args)
{
Print(new DateTime());
}
public static void Print(DateTime d)
{
Console.WriteLine(d);
}
}
9. ☑
use System;
public class DatePrinter {
public static void Main(string[] args)
{
Print(new DateTime());
}
public static void Print(DateTime d)
{
Console.WriteLine(d);
}
}
10. use System;
public class DatePrinter {
public static void Main(string[] args)
{
Print(null);
}
public static void Print(DateTime d)
{
Console.WriteLine(d);
}
}
11. ☒
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$ gmcs DatePrinter.cs
} DatePrinter.cs(7,5): error CS1502: The best overloaded method match for
`DatePrinter.Print(System.DateTime)' has some invalid arguments
DatePrinter.cs(10,22): (Location of the symbol related to previous error)
DatePrinter.cs(7,5): error CS1503: Argument `#1' cannot convert `null'
expression to type `System.DateTime'
Compilation failed: 2 error(s), 0 warnings
12. use System;
public class DatePrinter {
public static void Main(string[] args)
{
Print(null);
}
public static void Print(DateTime? d)
{
Console.WriteLine(d);
}
}
13. ☑
use System;
public class DatePrinter {
public static void Main(string[] args)
{
Print(null);
}
public static void Print(DateTime? d)
{
Console.WriteLine(d);
}
}
14. “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
17. “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
23. “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
24. “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
26. 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;
}
}
36. add a b = a + b
Prelude> :load add.hs
[1 of 1] Compiling Main
( add.hs, interpreted )
Ok, modules loaded: Main.
*Main> :type add
add :: (Num a) => a ‐> a ‐> a
*Main> :type (add 1 2)
(add 1 2) :: (Num t) => t
*Main>
40. “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
41. 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
45. =>typing influences
language features and
tools
=>static typing is
YES
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
46. =>saying that something
is static or dynamic
doesn’t tell much about
what it can do
=>most nice features in
NO Python/Ruby/JavaScript
are related to meta-
model, not typing
=>Java/C# are
bureaucratic for
historical reasons, not
limitations on typing