initializer

Inheritance when __new__() doesn't return instance of class

隐身守侯 提交于 2019-12-06 21:07:39
When __new__ return instance of class, everything is ok, we can create subclass with no problems: class A: def __new__(cls, p1, p2): self = object.__new__(cls) return self def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 class B(A): def __new__(cls, p3): self = super().__new__(cls, 1, 2) return self def __init__(self, p3): super().__init__(1, 2) self.p3 = p3 a = A(1, 2) print(a.p2) # output: 2 b = B(3) print(b.p3) # output: 3 But, If __new__() does not return an instance of cls , then the new instance’s __init__() method will not be invoked. Looks like we have to call __init__() inside _

How to extend Array for generic type?

拈花ヽ惹草 提交于 2019-12-06 08:49:24
I have a class for a linked list declared like this: class LinkedNode<T> { let data: T var next: LinkedNode<T>? func traverseList(process: (LinkedNode<T>) -> ()) { ... } } What I want to do is extend Array to have an initialiser that converts my LinkedNode class to an array of linked nodes. I tried this: extension Array where Element == LinkedNode<T> { init(node: LinkedNode<T>) { var result = [LinkedNode<T>]() traverseList { result.append($0) } return result } } But that gives errors that T is undeclared. I have tried taking it out and doing other things, but nothing has worked. I was able to

Rails: Get hostname in an initializer

梦想与她 提交于 2019-12-06 07:23:12
问题 I'm using Sorcery for Authentication, and I need to setup third party authentication in its initializer. The initializer has a line that looks like this: config.twitter.callback_url "http://example.dev/auth/callback?provider=twitter" ...where example.dev is the hostname when I'm using Pow in local development. This needs to be example.com if the app is in production, or staging.example.com if it's in staging, etc. I would like to set this line to be something like this: config.twitter

How to generate initializer in Ruby?

会有一股神秘感。 提交于 2019-12-06 01:39:18
问题 It's time to make it shorter: class Foo attr_accessor :a, :b, :c, :d, :e def initialize(a, b, c, d, e) @a = a @b = b @c = c @d = d @e = e end end We have 'attr_accessor' to generate getters and setters. Do we have anything to generate initializers by attributes? 回答1: Easiest: Foo = Struct.new( :a, :b, :c ) Generates both accessors and initializer. You can further customize your class with: Foo = Struct.new( … ) do def some_method … end end 回答2: We can create something like def_initializer

Swift2.2 failable initializer允许提前返回nil以及和Java的不同

做~自己de王妃 提交于 2019-12-05 19:46:31
发现swift和java有一个完全不一样的地方 在swift中, 子类必须先初始化子类的所有属性, 然后才能调用父类的构造器. 而在java中.super调用必须出现在构造函数的第一行. java代码 public class Dog { String name; Dog(String name){ this.name = name; } } class NoisyDog extends Dog { int age; NoisyDog(String name) { // 交换以下两行的顺序会报错: Constructor call must be the first statement in a constructor super(name); this.age = 5; } } 对应的swift代码: class Dog { var name: String; init(name: String){ self.name = name; } } class NoisyDog: Dog { var age: Int override init(name: String) { //交换以下两行的顺序会报错error: property 'self.age' not initialized at super.init call self.age = 5; super.init(name:

How can I properly use breakpoints when using an object initializer?

谁说我不能喝 提交于 2019-12-05 18:54:22
For example, doing something like this: foreach (DataRow row in data.Rows) { Person newPerson = new Person() { Id = row.Field<int>("Id"), Name = row.Field<string>("Name"), LastName = row.Field<string>("LastName"), DateOfBirth = row.Field<DateTime>("DateOfBirth") }; people.Add(newPerson); } Setting a breakpoint to an individual assignation is not possible, the breakpoint is set to the entire block. If I want to see specifically where my code is breaking, I have to use: foreach (DataRow row in data.Rows) { Person newPerson = new Person(); newPerson.Id = row.Field<int>("Id"); newPerson.Name = row

Rails initializers are running while migrating database

女生的网名这么多〃 提交于 2019-12-05 18:13:27
问题 It is very surprising that Rails's initializers run while running any rake task, including db:migrate and db:seed . An initializer in my app starts a background thread (a kind of worker process), and it should be executed only when the application is running in debug and production mode. How to prevent a specific initializer from running when doing rake db:migrate or how to detect in initializer that a rake task is running? 回答1: Here is a solution how to prevent an initializer from running in

In what order are initializer block and variable definitions and etc. executed? (in java)

◇◆丶佛笑我妖孽 提交于 2019-12-05 17:51:31
问题 I have problem understanding the order in which initialization happens. this is the order I assumed: *Once per 1. Static variable declaration 2. Static block *Once per object 3. variable declaration 4. initialization block 5. constructor but according to this code I am obviously wrong: class SomethingWrongWithMe { { b=0; //no. no error here. int a = b; //Error: Cannot reference a field before it is defined. } int b = 0; } And the error would disappear if I do this: class SomethingWrongWithMe

How to initialize CBCentralManager in Swift when a self reference is necessary

六月ゝ 毕业季﹏ 提交于 2019-12-05 13:27:50
What are good ways to initialize an instance of CBCentralManager, which requires a delegate and is often pointing to the owning class? I could declare the property as an implicitly unwrapped optional but doing so as a general practice seems rather not Swift-like and not very safe. Alternatively, I can declare the property as an optional. But since CBCentralManager's initializers are not declared as failable, it doesn't seem to make sense to declare the instance as such. Implicitly Unwrapped Optional: class MyUnwrappedOptional: NSObject, CBCentralManagerDelegate { var centralManager:

How to define extern variable along with declaration?

…衆ロ難τιáo~ 提交于 2019-12-05 12:48:31
问题 Wiki says: The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable . That means, an extern declaration that initializes the variable serves as a definition for that variable . So, /* Just for testing purpose only */ #include <stdio.h> extern int y