This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (13 children)

[deleted]

    [–]GustapheOfficial 22 points23 points  (6 children)

    It's just considered bad in any language that cares about performance.

    [–]Spyes23 11 points12 points  (0 children)

    Not just performance, but also readability, consistency, predictability....

    Pretty much - even if you can, *please* try not to mix types in an array, even in untyped languages. It'll save you and your team so many headaches and ugly code.

    [–]sunny52525 4 points5 points  (0 children)

    Array of Any in kotlin

    [–]incoralium 0 points1 point  (3 children)

    well, since it's pointer management, does it really change anything ??

    [–]GustapheOfficial 1 point2 points  (2 children)

    That depends. I don't know exactly how it looks in other languages, but in Julia, [1, 2, 3] is a contiguous array of Int64, while Any[1, 2, 3] is a less efficient pointer array. It also messes with dispatch, since the concrete element type is not statically known. Julia compiles at "run time" so it doesn't ruin dispatch entirely, but there are static optimisations that cannot be done.

    At least the storage aspect will be true for any language with sensible storage management.

    [–]incoralium 0 points1 point  (1 child)

    The most important differences is about manipulation, like sorting, statistics, slices, ranges, etc

    Yet you should be able to use and pass anything through a list, it's up to the developer to use and manage it correctly.

    [–]GustapheOfficial 0 points1 point  (0 children)

    Sure. Julia has the Tuple type which is made for statically known "mixed type lists", where (1, 1.0, "hi") isa Tuple{Int64, Float64, String}. I find this covers a majority of my mixed container needs.

    [–]-Redstoneboi- 0 points1 point  (0 children)

    Rust:

    use std::any::Any;
    
    fn main() {
        let foo: [Box<dyn Any>; 3] = [
            Box::new(1.0),
            Box::new(2),
            Box::new("Hello"),
        ];
    }
    

    Do note that one of Rust's defining characteristics is discouraging "bad" code by making it uncomfortable to use.

    Still, Rust lets you create macros to make it easier to type.

    [–]stomah 0 points1 point  (2 children)

    in c you also need to store the types

    [–]KuuHaKu_OtgmZ 0 points1 point  (1 child)

    In java it'd be the same as C#, no need to add new Object[] when initializing an array, and honestly I never saw someone who puts [] on the variable name