9 ms·
On containers, while it's entirely possible to constrain the type, it's also possible to use AnyObject var example = Dictionary<Int,AnyObject>() I'll miss
by afternooner 12y ago
On containers, while it's entirely possible to constrain the type, it's also possible to use AnyObject
var example = Dictionary<Int,AnyObject>()
I'll miss the duck typing, but having bought my house off C# work, I can work with this.
- steveklabnik 12y agoThe first time I saw this idea was in Rust, which calls its type Any. I haven't seen the need to use it yet, but this seems like a decent case. What other languages have this? How is it often used? I've never heard of a similar type in, say, Haskell.
- Xlythe 12y agoIn Java, everything extends Object, so something like List<Object> will let you put anything you want into it. I should play with more languages, I didn't realize that was an uncommon feature.
- steveklabnik 12y agoOh, duh, of course that's true. For some reason "the parent class of every class" is different than "a type explicitly for any type" in my brain.
- noblethrasher 12y ago>For some reason "the parent class of every class" is different than "a type explicitly for any type" in my brain. Good, because types and classes are different things despite what languages like Java and my beloved C# would have us to think.
- brandonbloom 12y agoTo clarify: "Object" as in Java is a type for any tagged box type. Primitives, such as "int", don't derive from Object, but can be promoted to a corresponding boxed type, such as "Integer". The story is a little more complex in C#, where ValueType derives from Object and "value types", both primitive and user-defined, are truly subclasses of Object. Types not derived from ValueType are considered reference types.
- pjmlp 12y agoThis is a consequence of lack of generics and comes from Smalltalk. In languages where genericity is supported, you don't need a common base class with a pre-defined set of methods, as you can give type constraints. Java could also have done it with interfaces, but those were the days OO was becoming mainstream and interface (component) based programming wasn't yet well understood.
- dantiberian 12y agoHaskell does have an Any type, but because of it's functional nature it's not as useful as in an OO language. http://stackoverflow.com/questions/6479444/is-there-a-type-any-in-haskell http://stackoverflow.com/questions/6479444/is-there-a-type-a...
- biftek 12y agoJulia also has a useful Any type.
- p4lindromica 12y agoScala also has a unified type system. The root is Any. AnyVal (primitives) and AnyRef (AKA java's Object) extend Any. Int is always Int instead of thinking about int and Integer. Scala handles boxing when the JVM requires it, but it is invisible to the programmer.