what is the point of heterogenous arrays?

后端 未结 8 1146
忘掉有多难
忘掉有多难 2020-12-16 13:43

I know that more-dynamic-than-Java languages, like Python and Ruby, often allow you to place objects of mixed types in arrays, like so:

[\"hello\", 120, [\"w         


        
相关标签:
8条回答
  • 2020-12-16 14:24

    Here is a simple answer:

    N.B. I am not talking about arrays that include different objects that all implement the same interface or inherit from the same parent, e.g.:

    Everything extends java.lang.Object... and that's plenty. There is no reason not to have Object[] and put anything you like in. Object[] are exceptionally useful in any middleware like persistence layer.

    0 讨论(0)
  • 2020-12-16 14:27

    Applying a multimethod to the array might make some sense. You switch the strategy to a more functional style in which you focus on a discrete piece of logic (i.e. the multimethod) instead of a discrete piece of data (i.e. the array objects).

    In your shapes example, this prevents you from having to define and implement the Shape interface. (Yes, it's not a big deal here, but what if shape was one of several superclasses you wanted to extend? In Java, you're SOL at this point.) Instead, you implement a smart draw() multimethod that first examines the argument and then dispatches to the proper drawing functionality or error handling if the object isn't drawable.

    Comparisons between functional and object-oriented styles are all over the place; here are a couple relevant questions that should provide a good start: Functional programming vs Object Oriented programming and Explaining functional programming to object-oriented programmers and less technical people.

    0 讨论(0)
  • 2020-12-16 14:31

    Is there reason to use the former over the latter in languages that support it?

    Yes, there is a very simple reason why you can do this in Python (and i assume the same reason in Ruby):

    How would you check that a list is heterogenous?

    • It can't just compare the types directly because Python has duck typing.
    • If all the object have some common typeclass Python has no way to guess that either. Everything supports being represented anyways, so you should be able to put them in a list together too.
    • It wouldn't make any sense to turn lists into the only type that needs a type declaration either.

    There is simply no way to prevent you from creating a heterogenous list!

    Or is there some bigger reason to use heterogenous arrays?

    No, I can't think of any. As you already mentioned in your question, if you use a heterogenous arrays you're just making things harder than they have to be.

    0 讨论(0)
  • 2020-12-16 14:41

    In Lua an object and an array are the same thing so the reason is more clear. Let's say that Lua takes the weak typing to the extreme

    Apart from that, I had a Google Map object and I needed to delete all markers created so far in that map. So I ended up creating an array for markers, an array for circles and an array for places. Then I made a function to iterate over those three arrays and call .remove() on each of them. I then realized that I could just have a single non homogeneous array and insert into them all the objects and iterate once over that array

    0 讨论(0)
  • 2020-12-16 14:43

    There is no reason not to support heterogeneous lists. It's a limitation for technical reasons, and we don't like those.

    Not everything needs to be a class!

    In Python, a class is basically a souped up dictionary with some extra stuff anyway. So making a class User is not necessarily any clearer than a dictionary {"name": ..., "id": ...}.

    0 讨论(0)
  • 2020-12-16 14:44

    As katrielalex wrote: There is no reason not to support heterogeneous lists. In fact, disallowing it would require static typing, and we're back to that old debate. But let's refrain from doing so and instead answer the "why would you use that" part...

    To be honest, it is not used that much -- if we make use of the exception in your last paragraph and choose a more liberal definition of "implement the same interface" than e.g. Java or C#. Nearly all of my iterable-crunching code expects all items to implement some interface. Of course it does, otheriwise it could do very little to it!

    Don't get me wrong, there are absolutely valid use cases - there's rarely a good reason to write a whole class for containing some data (and even if you add some callables, functional programming sometimes comes to the rescue). A dict would be a more common choice though, and namedtuple is very neat as well. But they are less common than you seem to think, and they are used with thought and discipline, not for cowboy coding.

    (Also, you "User as nested list" example is not a good one - since the inner lists are fixed-sized, you better use tuples and that makes it valid even in Haskell (type would be [(String, Integer)]))

    0 讨论(0)
提交回复
热议问题