Why doesn't ActionScript have “generics”?

前端 未结 4 1046
死守一世寂寞
死守一世寂寞 2020-12-18 18:14

Can anyone tell me why ActionScript 3, a statically typed language, doesn\'t have generics? Is it too much work? A historical thing? Is there some way to \"fake\" it that I

4条回答
  •  孤城傲影
    2020-12-18 19:02

    I believe it's a historical thing. ActionScript is based on ECMAScript (JavaScript is also based on ECMAScript). ECMAScript is a dynamically typed language, meaning that variables don't have their type declared. Generics are more useful in statically typed languages, wherein the type of variable is declared upfront. In a statically typed language, without generics you're stuck casting all the time from the root object (for example, Object in Java). This is not a problem in ECMAScript, because you can put anything you want into any data structure.

    So why didn't ActionScript add generics when they added static typing to ECMAScript? I can't be sure of that, but I think the premise of your question is a bit off - there are generic-esque containers, such as Vector. I might think they'd keep the dynamically-typed containers of ECMAScript (objects and arrays) for backwards-compatibility, but they already broke that between AS2 and AS3, so I'm not sure.

提交回复
热议问题