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
1 answer
Calling an initializer having only the class name in Swift
I wanted to know if there was a way to call an initializer by only having the class name in Swift.
class Shape {
let name: String
var numberOfSides: Int?
init(name: String) {
self.name = name
}
}
Pseudo code:
let shapeClass =…

AJ_1310
- 3,303
- 3
- 19
- 26
4
votes
0 answers
Rails initializers only running in certain context (eg. server and not rake, generator, etc.)
I'm in the process of writing a Rails engine that requires some setup config and also does a check to see if some required attributes are defined on a model. The engine class in lib that defines this is as follows.
module Kiosk
mattr_accessor…

richard
- 1,565
- 2
- 18
- 35
4
votes
2 answers
Cocoa Objective-C init oddity
I was reviewing some code and came across something that looked like this (assume it is defined for the TestObject class)
-(id) init
{
if (self == [super init])
{
self.testString = @"Hello";
}
return self;
}
I promptly…

jbat100
- 16,757
- 4
- 45
- 70
4
votes
2 answers
C# how to invoke a field initializer using reflection?
Say I have this C# class
public class MyClass {
int a;
int[] b = new int[6];
}
Now say I discover this class using reflection and while looking at the fields I find that one of them is of type Array (ie: b)
foreach( FieldInfo fieldinfo in…

Éric
- 181
- 2
- 15
4
votes
2 answers
Rails: Run initializer before creating classes
Basically I have an initializer class at RAILS_ROOT/config/initialiers/app_constant.rb to make everything easy to control.
class AppConstant
APIURL = 'http://path.to.api'
end
And in the RAILS_ROOT/model/user.rb, I have the settings:
class User <…

jwall
- 759
- 1
- 8
- 17
3
votes
5 answers
Object Initializer force compile error
Is it possible to enforce rules or throw an error when using object initializers in C#? I'd like to throw a compiler error or warning if an object is initialized but is missing a certain property.
public class Party
{
public string Name { get;…

daviddeath
- 2,483
- 2
- 17
- 16
3
votes
1 answer
How to get the server port in a Rails initializer
I have multiple instances of Rails servers and there is a need for each one of them to know its own listening port in environment.rb. request.port will work in the controllers but not in the context of the environment.rb. Is there a way? Thanks!

timeon
- 2,194
- 4
- 18
- 19
3
votes
2 answers
Config variables available in rails environment file
I'm currently using an initializer to load a config.yml file into an AppConfig hash which offers access to variables for the environment. For production I am using environmental variables set on the server. I am using the following code to fallback…

Undistraction
- 42,754
- 56
- 195
- 331
3
votes
4 answers
static() method (without any decleration)
I have the following class:
public abstract class A()
{
public static final SomeString = null;
static()
{
SomeString = "aaa";
}
}
When is this static method invoked and how?
What is the purpose in creating such a static method…

akaspi
- 275
- 1
- 3
- 12
3
votes
1 answer
Does the default argument for a function parameter is considered as an initializer for that parameter?
Suppose I have function declarations like these:
static const int R = 0;
static const int I = 0;
void f(const int& r = R);
void g(int i = I);
Per [dcl.fct.default]/1:
If an initializer-clause is specified in a parameter-declaration…

mada
- 1,646
- 1
- 15
3
votes
1 answer
Create a LINQ expression in F# for C# object initializer
I am using a .NET framework which allows to pass an object initializer LINQ expression when called from C#:
db.UpdateAdd( () => new InvoiceAccount { Quantity = 3 } );
The framework uses the expression from the argument to detect which fields should…

Marek Vaculciak
- 31
- 3
3
votes
2 answers
Java Initialization Block
Can someone help me understand the following construct? I am having trouble understanding if this is an initializer or an anonymous class. I am not familiar with this syntax.
JTable jt = new JTable(data, fields) **{
public…

EdgeCase
- 4,719
- 16
- 45
- 73
3
votes
2 answers
@ViewChild - initializer error on Angular 13
I am creating app in Angular 13. I want to call the method show() of ChildComponent from ParentComponent, using @ViewChild, but I get errors. The older questions and answers are not working in this case.
Parent:

Weronika
- 370
- 1
- 4
- 16
3
votes
2 answers
Use of parenthesis or curly braces in C++ constructor initializer list
I have a question on constructor initializer list as follows :
while specifying the initial values of members, initial values are written in ()- parenthesis according to C++ Primer book (author - Stanley Lippman). However, I have also seen {} being…

Sunil Puranik
- 87
- 1
- 4
3
votes
1 answer
C++ array initializer. Using enum type
class ARouter {
enum directions {north, neast, east, seast, south, swest, west, nwest};
static directions gon[] = {north, neast, nwest, east, west, seast, swest, south};
};
Hi, does anyone know what is the matter with the above code?
I am…

John
- 6,433
- 7
- 47
- 82