Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters
Questions tagged [initializer]
684 questions
4
votes
2 answers
How does the initialization of classes in Scala work?
The code below throws a java.lang.NullPointerException because the trait is initialized prematurely.
trait DummyTrait {
def intSeq: Seq[Int]
require(intSeq.exists(_ > 2))
}
object Dummy extends DummyTrait {
val extraIntSeq: Seq[Int] =…

Pedro Igor A. Oliveira
- 132
- 1
- 8
4
votes
1 answer
How to set seed value of kernel initializer (glorot_uniform) in Keras
I'd like to set seed value of glorot_uniform kernel initializer in Keras.
model.add(Dense(50, input_dim=self.state_size, activation='relu', kernel_initializer='glorot_uniform(seed=0)'))
When I use above code, error message is below.
ValueError:…

WKK
- 159
- 2
- 13
4
votes
9 answers
Why is an Add method required for { } initialization?
To use initialization syntax like this:
var contacts = new ContactList
{
{ "Dan", "dan.tao@email.com" },
{ "Eric", "ceo@google.com" }
};
...my understanding is that my ContactList type would need to define an Add method that takes two…

Dan Tao
- 125,917
- 54
- 300
- 447
4
votes
5 answers
Why would my array would be filled out to zero, when I initialised it to -1
#include
using namespace std;
long long int memo[20] = {-1}; //create memo table and initialise to -1
long long int fibo(long long int n)
{
if(memo[n]>-1) //changing this to if(memo[n]>0) works fine
return…

Pavithran Ravichandiran
- 1,711
- 1
- 17
- 20
4
votes
4 answers
Basic C++: How do I initialize a struct member of a class?
I've looked all over the place, but haven't found an answer to this.
I have a C++ class with these protected members:
struct tm _creationDate;
struct tm _expirationDate;
struct tm _lockDate;
I want to initialize them at instantiation time. …

Oscar
- 2,039
- 2
- 29
- 39
4
votes
5 answers
initialise object with equal operator
In the class defined as below named foo
class foo{
private:
string str;
public:
foo operator = (string s){
str = s;
}
};
int main(){
foo a = "this initialization throwing an error";
foo b;
b = "but assignment after…

Abhay Jatin Doshi
- 464
- 4
- 17
4
votes
2 answers
Why it is called the Memberwise Initialiser
Quote from the office Swift Document
All structure have an automatically-generated memberwise initializer, which you can use to initialize the member properties of new structure instances ...
Question 1: What is so special about the default…

SLN
- 4,772
- 2
- 38
- 79
4
votes
2 answers
How to populate a const member array based on a constructor argument?
Let's say I have this class:
template
class A {
public:
A(const char *s) ...
private:
const char buf[N];
};
The template is there so that I can configure the array size without dynamic memory allocation (a requirement). The…
user1002430
4
votes
0 answers
error within custom initialiser in extension of class
Here is my sample code which brings my questions:
protocol Decodable {
init?(json: [String: AnyObject]) // remove this line will solve the problem
}
extension UIFont: Decodable { // remove `Decodable` will also solve the problem
convenience…

user3379127
- 271
- 2
- 6
4
votes
1 answer
Difference in explicit and non-explicit C# class initializers
What is the resulting difference between these two class initializers? Both seem to be syntactically correct in C#. Is the second one a shortcut for the first?
Class1 class1 = new Class1()
{
Boolean1 = true,
Class2Instance = new Class2
…

Itzalive
- 370
- 4
- 14
4
votes
1 answer
What is the point of using implicitly unwrapped optional in this example?
This is from the docs, section Failable Initializers for Classes:
class Product {
let name: String!
init?(name: String) {
self.name = name
if name.isEmpty { return nil }
}
}
if let bowTie = Product(name: "")…

Whirlwind
- 14,286
- 11
- 68
- 157
4
votes
1 answer
C++11 and generalized initializers conventions
C++11 brings the new generalized initializers which can be nice. Question: Is any of the old syntax for initializing objects considered deprecated. In C++03 an object can be initialized as
Foo bar(x)
Foo bar=Foo(x)
Option (1) is preferred since it…

user877329
- 6,717
- 8
- 46
- 88
4
votes
3 answers
How can I use an array-initializer syntax for a custom vector class?
I have a class roughly designed as such:
class Vector3
{
float X;
float Y;
float Z;
public Vector3(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
}
I have other classes…

Lazlo
- 8,518
- 14
- 77
- 116
4
votes
0 answers
Java 9 Static Initializers in interfaces
When Java 8 came out, I was disappointed to see that they still didn't allow static initializers in interfaces. I guess the justification was that it was effectively like adding a private static method to the interface, which in Java 8 was not…

HesNotTheStig
- 549
- 1
- 4
- 9
4
votes
4 answers
Difference between variable declaration and definition in Swift
The terms "declaration" and "definition" are being used synonymously in Apple's Swift documentation and it's getting me confused.
Under the "Initialization" section (which talks about class initializers), Apple states:
You can set an initial value…

Walter M
- 4,993
- 5
- 22
- 29