static typing - Do you know of any examples of elegant solutions in dynamically typed languages? -
imagine 2 languages (apart type information) have same syntax, 1 statically typed while other 1 uses dynamic typing. then, every program written in statically typed language, 1 can derive equivalent dynamically typed program removing type information. not neccessarily possible other way around, class of dynamically typed programs strictly larger class of statically typed programs. let's call dynamically typed programs, there no mapping of variables types making them statically typed "real dynamically typed programs".
as both language families definitly turing-complete, can sure every such real dynamically typed program there exists statically typed program doing same thing, read "experienced programmers able write elegant code in dynamically typed languages". ask myself: there examples of real dynamically typed programs, equivalent statically typed programm more complex / less "elegant" (whatever may mean)?
do know of such examples?
i sure many "elegance" problems of static languages, static type checking isn't blame, lack of expressiveness of static type system implemented in language , limited capabilities of compiler. if done "righter" (like in haskell example), programs turn out terse, elegant .. , safer dynamic counterpart.
here's illustration (c++ specific, sorry): c++ powerful, implements metalanguage it's template class system. still, simple function hard declare:
template<class x,class y> ? max(x x, y y)
there astounding amount of possible solutions, ?=boost::variant<x,y>
or computing ?=is_convertible(x,y)?(x:is_convertible(y,x):y:error)
, none of them satisfiying.
but imagine preprocessor, transform input program it's equivalent continuation passing style form, each continuation callable object accepts possible argument types. cps version of max this:
template<class x, class y, class c> void cps_max(x x, y y, c cont) // cont object can called x or y { if (x>y) cont(x); else cont(y); }
the problem gone, max calls continuation accepts x or y. so, there solution max static type checking, can't express max in it's non-cps form, untransform(cps_max)
undefined, speak. so,we have argument max
can done right, don't have means so. lack of expressiveness.
update 2501: assume there unrelated types x , y , there bool operator<(x,y)
. shouldmax(x,y)
return? let further assume, x , y both have member function foo();
. how make possible write:
void f(x x, y y) { max(x,y).foo(); }
returning either x or y , invoking foo() on result no problem dynamic language, close impossible static languages. however, can have intended functionality rewriting f() use cps_max:
struct call_foo { template<class t> void operator(const t &t) const { t.foo(); } }; void f(x x, y y) { cps_max(x,y,call_foo()); }
so can't problem static type checking, looks ugly , not scale beyond simple examples. missing static language can not provide static and readable solution.
Comments
Post a Comment