Why Can't I Instantiate an Empty Array of a Nested Class

Why can't I instantiate an empty array of a nested class?

This definitely looks like a bug in the compiler, especially as you're allowed to instantiate an empty array of a nested class just fine; it simply doesn't work with the initialiser syntax.

I'll raise a bug. In the meantime, for anyone experiencing the problem, you can work around it by using assignment syntax with an empty array and a specified class for the variable, rather than constructor syntax:

 var foobar: [Wrapper.InsideClass] = []

Why can't I use the short Array constructor syntax when creating an Array of a nested Struct?

It's just a hole in the language. After all, the [Type] syntax is just syntactic sugar; as you rightly say, if you use the real syntax with Array<Type>, or use [Type] but not as a constructor, there's no problem. You can also work around it with type alias:

struct Struct1 {
struct Struct2 {
var name: String?
}
}

typealias Struct2 = Struct1.Struct2

var struct2Array = [Struct2]()

Java Instantiating Array of Class in class space yields NullPointerException

It seems to me the issue is in the difference in the order that instance variables are initialized compared to the constructor between JAVA and C#.

Why can't I append an element in a PHP class' nested array?

You try to access element 0 in an associative array you cannot do that.

public function App($elm) {
$V = $this->cms[0]['V']; // here

How to initialize a 2D C-style array from a nested std::initializer_list?

You should provide exact type to the Matrix(std::initializer_list</* here */> list);.
Then to fill the array, you need to iterate through the list passed and fill the array. (See live online)

#include <initializer_list> // std::initializer_list

template<typename T, std::size_t R, std::size_t C>
class Matrix
{
T mArray2D[R][C]{};

public:
Matrix(const std::initializer_list<T[C]> list)
// ^^^^^ --> type of the row
{
auto iter{ list.begin() }; // returns pointer to T[C]
for (std::size_t row{}; row < R; ++row)
{
const auto& rowElements{ *iter };
for (std::size_t col{}; col < C; ++col)
{
mArray2D[row][col] = rowElements[col];
}
++iter;
}
}
};

Now in the main() you can initialize Matrix using an initializer list as follows:

Matrix<int, 2, 2> mat{ {1,2}, {3,4} };
// or
// Matrix<int, 2, 2> mat = { {1,2}, {3,4} };

If you could use std::array, you could do the following. (See live online)

#include <iostream>
#include <array> // std::array
#include <initializer_list> // std::initializer_list

template<typename T, std::size_t R, std::size_t C>
class Matrix
{
using RowType = std::array<T, C>;
T mArray2D[R][C]{};

public:
Matrix(const std::initializer_list<RowType> list)
{
auto iter{ list.begin() };
for (std::size_t row{}; row < R; ++row)
{
const RowType& rowElements{ *iter };
for (std::size_t col{}; col < C; ++col)
{
mArray2D[row][col] = rowElements[col];
}
++iter;
}
}
};

If the multidimensional array in the class could be an std::array of std::array of type T, you could use std::copy as well. (See live online)

#include <iostream>
#include <array> // std::array
#include <initializer_list> // std::initializer_list
#include <algorithm> // std::copy

template<typename T, std::size_t R, std::size_t C>
class Matrix
{
using RowType = std::array<T, C>;
using Array2D = std::array<RowType, R>;
Array2D mArray{ RowType{} }; // initialize the matrix

public:
Matrix(const std::initializer_list<RowType> list)
{
std::copy(list.begin(), list.end(), mArray.begin());
}
};

Why can't we instantiate an abstract class in Java?

This is not a technical limitation, rather (as you have pointed out) a logical one. Java (and many other languages) enforce various rules not because they are impossible to break, but because this is an intentional part of the language.

How to instantiate model class from nested classes with kotlin reflection

Here's how you can do it if all of the nested classes you care about have empty constructors. You have to instantiate an instance to be able to read values from the properties, because the expression in your property initialization is not accessible by reflection.

val commandModels = commandList.mapNotNull { kClass ->
val instance = kClass.constructors.firstOrNull { it.parameters.isEmpty() }
?.call() as SquibCommand?
instance?.let { CommandModel(it.title, it.description) }
}


Related Topics



Leave a reply



Submit