initializer

Swift- variable not initialized before use (but it's not used)

独自空忆成欢 提交于 2019-11-29 13:39:21
Currently I've got some swift code like this: class C { let type: Type; var num = 0; init() { self.type = Type({ (num: Int) -> Void in self.num = num; }); } } The Swift compiler refuses to permit it, saying that I've referenced self.type before it's initialized, even though that's clearly completely untrue. Furthermore, I can't employ the workaround found in other questions/answers, because the type is not optional, and it's immutable, so it can't be initialized with nil pointlessly first. How can I make the Swift compiler accept this perfectly valid code? This has nothing to do with returning

Reduce function with three parameters

纵饮孤独 提交于 2019-11-29 06:02:19
How does reduce function work in python3 with three parameters instead of two. So, for two, tup = (1,2,3) reduce(lambda x, y: x+y, tup) I get this one. This would just sum up all the elements in tup . However, if you give reduce function three parameters like this below, tup = (1,2,3) reduce(lambda x, y: x+y, tup, 6) this would give you a value of 12 . I checked up on the documentation for python3 and it says the third argument is an initializer. That said, then what is the default initializer if the third argument is not inserted? If you omit the third parameter, then the first value from tup

Rails 3.1 Deployment to Heroku Error

给你一囗甜甜゛ 提交于 2019-11-29 02:45:53
问题 I'm trying to deploy my app to Heroku, I've done this before on my Windows machine, and now I am currently using a mac. I'm trying to use Postgresql for the first time. I have the following in my Gemfile: gem 'pg' EDIT: AndrewDavis-OSX:lunchbox ardavis$ rvm list rvm rubies => ruby-1.9.2-p180 [ x86_64 ] AndrewDavis-OSX:lunchbox ardavis$ heroku rake db:migrate rake aborted! /app/config/initializers/session_store.rb:3: syntax error, unexpected ':', expecting $end App::Application.config.session

Multiple constructors: the Pythonic way? [duplicate]

柔情痞子 提交于 2019-11-28 16:35:25
问题 This question already has an answer here: How to overload __init__ method based on argument type? 10 answers I have a container class that holds data. When the container is created, there are different methods to pass data. Pass a file which contains the data Pass the data directly via arguments Don't pass data; just create an empty container In Java, I would create three constructors. Here's how it would look like if it were possible in Python: class Container: def __init__(self): self

How to initialize member-struct in initializer list of C++ class?

喜欢而已 提交于 2019-11-28 15:35:23
I have the following class definitions in c++: struct Foo { int x; char array[24]; short* y; }; class Bar { Bar(); int x; Foo foo; }; and would like to initialize the "foo" struct (with all its members) to zero in the initializer of the Bar class. Can this be done this way: Bar::Bar() : foo(), x(8) { } ... ? Or what exactly does the foo(x) do in the initializer list? Or is the struct even initialized automatically to zero from the compiler? icecrime First of all, you should (must !) read this c++ faq regarding POD and aggregates. In your case, Foo is indeed a POD class and foo() is a value

How to check if database schema matches Entity Framework schema?

妖精的绣舞 提交于 2019-11-28 12:08:13
For my surprise, using the CreateDatabaseIfNotExists context initializer, the line context.Database.Initialize(true) doesn't throw an exception if the schema does not match my code first schema. Is there a way to validate if the current database matches our schema before, for instance, we try to access a entity, whose table doesn't exist on the database anymore, and an exception is thrown by EF? You can call CompatibleWithModel to determine if the database matches the model. If you set the parameter to true it will throw an exception if no model data is found in the database. bool isCompatible

Initializing shared_ptr member variable, new vs make_shared?

旧巷老猫 提交于 2019-11-28 10:21:10
When initializing a shared_ptr member variable: // .h class Customer { public: Customer(); private: std::shared_ptr<OtherClass> something_; } // .cpp Customer(): something_(new OtherClass()) { } vs. Customer(): something_(std::make_shared<OtherClass>()) { } Is the make_shared version allowed? I always seem to see the first version, which is preferred? The only times when make_shared is not allowed are: If you're getting a naked pointer allocated by someone else and storing it in shared_ptr . This is often the case when interfacing with C APIs. If the constructor you want to call is not public

Swift- variable not initialized before use (but it's not used)

佐手、 提交于 2019-11-28 07:16:59
问题 Currently I've got some swift code like this: class C { let type: Type; var num = 0; init() { self.type = Type({ (num: Int) -> Void in self.num = num; }); } } The Swift compiler refuses to permit it, saying that I've referenced self.type before it's initialized, even though that's clearly completely untrue. Furthermore, I can't employ the workaround found in other questions/answers, because the type is not optional, and it's immutable, so it can't be initialized with nil pointlessly first.

Objective-C: init vs initialize

时光怂恿深爱的人放手 提交于 2019-11-28 03:03:34
In Objective-C, what is the difference between the init method (i.e. the designated initializer for a class) and the initialize method? What initialization code should be put in each? -init is an instance method, used to initialize a particular object. +initialize is a class method, run before any instances of the class are created and before other class methods are run. +initialize isn't something you use most of the time, but it's handy for setting up any static variables that the class as a whole might use, or for ensuring that certain conditions are met before any instances are created.

How to initialize member-struct in initializer list of C++ class?

不羁岁月 提交于 2019-11-27 19:47:24
问题 I have the following class definitions in c++: struct Foo { int x; char array[24]; short* y; }; class Bar { Bar(); int x; Foo foo; }; and would like to initialize the "foo" struct (with all its members) to zero in the initializer of the Bar class. Can this be done this way: Bar::Bar() : foo(), x(8) { } ... ? Or what exactly does the foo(x) do in the initializer list? Or is the struct even initialized automatically to zero from the compiler? 回答1: First of all, you should (must !) read this c++