|
|
|
|
|
by lispm
2629 days ago
|
|
Lisp is strongly, implicit and dynamically typed, with type coercions. For example it provides generic arithmetic: * (let ((a 1) ; integer
(b 2.0) ; default float
(c 1/3) ; ratio
(d #c(1.2 2.3))) ; complex number
(+ a b c d))
#C(4.5333333 2.3)
Variables A, B, C, D have no type declarations. The values usually carry type information. The + operation will take any numeric value and create a result value of a type it chooses. for example (+ 1/2 1/2) will result to 1. Adding ratios might create a ratio or an integer.There is a whole bunch of literature on compiling Scheme: https://github.com/scheme-live/library.readscheme.org/blob/m... |
|
Generally with unknown (at compile-time) variable types, you need to box the variables (carry type information in addition to the value). The operators may then either work on the boxed variables and choose behaviour based on the type information or the operators may be specialized in many versions to work on the unboxed variables (this requires the run-time to dispatch to the correct specialized version, if the types cannot be determined statically).
This is generally a trade-off between space and execution time - if the number of possible types are low (either because of a limited type system or because the possible different types can be determined statically), then it may make sense to specialize.
In JS, in addition to mutable types and values for each variable, you also have a challenge with variable scope. It is possible to introduce new variables in the global scope from a local scope, so depending on run-time values, a variable for a given statement may or may not have been declared.
An example from the thesis:
Assuming that 'a' was not declared elsewhere also, the call to 'g()' will either print out the value of 'a' (ie. "5") or will result in a run-time error.