Types Flashcards

(27 cards)

1
Q

static typing

A

expression and types are checked at compile time for correctness

the type of a value is determined by the type of the variable it was read from

can be explicit, implicit or mixed

e.g. C, Haskell, Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

static monomorphic typing

A

variable is associated with a single, fixed type

a.k.a lexical typing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

static polymorphic typing

A

variable associated with a type scheme which contains type variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

dynamic typing

A

expressions and types are checked at runtime

untyped variables and functions

often scripting and prototyping languages are dynamically typed

values have types not variables

e.g. Lisp, Python, JavaScript

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

type encoding: boxing

A

type information is stored in the header of the value structure

numeric code for each object

type check operation: *ptr == #x

has high overheads, type check = memory access, each object needs an extra word of storage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

type encoding: tagging

A

type information is stored in the pointer to the value

optimisation of boxing for most common types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

type encoding: big bag of pages

A

type information depends on the region of memory the value is in

multiple homogenous pages

only practical with a few fixed types

type check operation: lower_bound <= ptr <= upper_cound

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

high bit tagging

A
  • type check most significant 8 bits
  • ptr & #xFF000000==#x??000000
  • limited number of immediate tags
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

low bit tagging

A
  • type check least significant 2 or 3 bits
  • limited number of immediate tags, 3 bits = 7 types, 2 bits = 3 types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

untyped

A

up to the programmer how to interpret a value

all values are presented as a machine byte or word

assembly langs still treat floats differently to ints as CPUs have seperate floating point and non-floating point registers

e..g ASM, BCPL, Forth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

strong vs weak typing

A

reflects the level of automatic conversion permitted

C is weakly typed, allows code like double x = 1; and the int value get coerced to a double before assignment.

languages like Rust are more precise and will not automatically coerce types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

type coercion

A

an implicit change of type, done automatically by the compiler

e.g. when given 1.0 + 2, python will convert 2 to a float automatically

this can sometimes lead to unexpected behaviour

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

type casting

A

explicit change of type.

e.g. double = (double)2/1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

mixing types: dynamic typing

A

the compiler must perform type checks during runtime before calling a function and if the types are wrong then perform coercions or error.

adds a lot of runtime overhead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

mixing types: static typing

A

the compiler will determine the types of a and b and do any coercions and choose the correct function at compile time, no runtime checks are needed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

mixing types: untyped

A

the operation is performed, there are no types to check or coerce

17
Q

mixing classes: dynamic lookup

A

the compiler generates code to
* choose the correct method to call on the current value
* call the chosen method

adds runtime overhead

18
Q

mixing classes: static lookup

A

the compiler can’t just determine the types and find the appropriate method at compile time because even in a static language a variable can be reassigned to a subtype of the original assignment, possibly changing the function that should be called.

19
Q

duck typing

A

highly dynamic kind of typing

to eval a.foo() the runtime examines the current value of a to see if there is a foo method defined on it and calls it if it finds one

20
Q

manifest typing

A

the program code includes the types of variables

e.g. C

21
Q

implicit typing

A

the compiler infers any types it needs as much as it can

e.g. in Haskell
inc x = x + 1

where Haskell determines the tpye of inc to be Num a => a -> a

22
Q

mixed typing

A

a variables type can be infered by its usage

e.g. in Rust
fn fix(x: f64) -> i32 {}
let y = fix(z);

Rust will determine the type of z as f64 and y as i32

Rust allow most type annotations to be implicit but requires some to be manifest, function declarations must be explicit

23
Q

overloading

A

multiple function definitions with the same name but different argument types

also known as ad-hoc polymorphism

compile time syntactic method selection

24
Q

name mangling

A

when the compiler renames functions internally to differentiate between overloaded functions

25
monomorphisation
replacing something apparently polymorphic with multiple monomorphic bits of code
26
type checking
verifies value usage is consistent with declarations
27
type inference
computes types consistent with value usage