Why does `Array` have an inline constructor; when ...
# announcements
d
Why does
Array
have an inline constructor; when the compiler says this is disallowed in my own code?
v
darkmoon_uk: what constructor are you referring to?
(AFAIK
inline constructor
is not a thing)
d
Exactly my point...
but look at kotlin.Array
public inline constructor(size: Int, init: (Int) -> T)
v
👀
d
And something about it is really ruining my day; something I thought would be easy... I want to create an Array<T> inside MyClass<T>'s initializer. It won't have it, because calling Array<T> it's insisting it be reified, and I can't have reified T inside a constructor!
I can pass in KClass, but that doesn't help with creating Array<T> either.
I can make a reified create() method in Companion class, but that requires I expose the Array<T> as part of MyClass's public constructor, which I don't want to do.
It looks like a bit of a hole in the language TBH
v
Arrays ARE objects, bth.
inline
there is for the lambda performance
d
Hmm, I guess this is no more restrictive than Java, will have to consider using
Array<Any?>
and casting inside my class.
Thx.
v
@darkmoon_uk I know I've mentioned it, but why would you use
Array
instead of
List
or
MutableList
? Their performance are exactly the same, but lists have normal generics
d
...with it's immediate, fixed size specification and initialization function, this is the exact API I would like to store image data.
I can recreate this for list but it will be extra work, it's a shame the generic behaviour of Array is peculiar. Especially when it's all just same-sized object pointers at the end of the day.
d
Shame, gratitude... thanks.
List fixes all 🙂
v
Shame on us as a community, because those function are really tough to find without knowing they exist. We need new ideas for docs, and fast
r
Actually inline for performance purpose and you can inline kind of tailrecursion for example
2
Kindly watch hadi hariri presentation about tailrecursion