initializer

How to default-initialize a struct containing an array in Rust?

笑着哭i 提交于 2019-12-19 16:19:12
问题 What is the recommended way to declare a struct that contains an array, and then create a zero-initialized instance? Here is the struct: #[derive(Default)] struct Histogram { sum: u32, bins: [u32; 256], } and the compiler error: error[E0277]: the trait bound `[u32; 256]: std::default::Default` is not satisfied --> src/lib.rs:4:5 | 4 | bins: [u32; 256], | ^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `[u32; 256]` | = help: the following implementations were found: <

When global_variables_initializer() is actually required

早过忘川 提交于 2019-12-18 18:49:10
问题 import tensorflow as tf x = tf.constant(35, name='x') y = tf.Variable(x + 5, name='y') # model = tf.global_variables_initializer() with tf.Session() as session: print("x = ", session.run(x)) # session.run(model) print("y = ", session.run(y)) I was not able to understand when global_variables_initializer() is actually required. In the above code, if we uncomment lines 4 & 7, I can execute the code and see the values. If I run as-is, I see a crash. My question is which variables it is

Rails how to create an initializer inside a gem

ⅰ亾dé卋堺 提交于 2019-12-18 16:45:31
问题 I am trying to build a gem in Rails 3 and inside it i am trying to pass an initializer: Credentials.configure do |config| file = File.read("#{Rails.root}/config/twitter.yaml") file_config = YAML.load(file) config.consumer_key = file_config[Rails.env][:consumer_key] config.consumer_secret = file_config[Rails.env][:consumer_secret] config.callback_url = URI.escape(file_config[Rails.env][:callback_url]) config.time_stamp = Time.now.to_i end and then i am trying to call it like this: Credentials

Advanced C question: Please explain C construct *({ foo(&bar); &bar; })

白昼怎懂夜的黑 提交于 2019-12-18 13:37:42
问题 This is ultimately a C question that arose when studying code in completion.h of the Linux kernel source, where I see a C technique I've never used in C before. Although have a vague sense what it's doing, I'd like to fine tune my understanding with a precise description, and I'm not quite sure how to search for the answer with Google without a potentially a long ordeal. The relevant lines of code from the linux kernel's completion.h: struct completion { unsigned int done; wait_queue_head_t

How to initialize static pointer with malloc in C?

血红的双手。 提交于 2019-12-18 04:47:10
问题 I'm trying to initiate a static variable (inside a function) with malloc in C, but I'm getting the "initializer not constant error". I know that I can't initiate a static with non constants in C, but can anyone think of a solution? I need the code to have the same effect as this: static int *p = (int *)malloc(sizeof(int)); Is there a trick/workaround? EDIT: I have a function that is called every time a flag goes high. In this function, I'm creating and starting a new thread. I declare a

Static constructor equivalent in Objective-C?

折月煮酒 提交于 2019-12-17 15:14:25
问题 I'm new to Objective C and I haven't been able to find out if there is the equivalent of a static constructor in the language, that is a static method in a class that will automatically be called before the first instance of such class is instantiated. Or do I need to call the Initialization code myself? Thanks 回答1: The +initialize method is called automatically the first time a class is used, before any class methods are used or instances are created. You should never call +initialize

In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

烈酒焚心 提交于 2019-12-17 04:18:51
问题 What I want is: obj = Foo.new(0) # => nil or false This doesn't work: class Foo def initialize(val) return nil if val == 0 end end I know in C/C++/Java/C#, we cant return a value in a constructor. But I'm wondering whether it is possible in Ruby. 回答1: In Ruby, what's the relationship between ' new ' and ' initialize '? new typically calls initialize . The default implementation of new is something like: class Class def new(*args, &block) obj = allocate obj.initialize(*args, &block) # actually

In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

拈花ヽ惹草 提交于 2019-12-17 04:17:34
问题 What I want is: obj = Foo.new(0) # => nil or false This doesn't work: class Foo def initialize(val) return nil if val == 0 end end I know in C/C++/Java/C#, we cant return a value in a constructor. But I'm wondering whether it is possible in Ruby. 回答1: In Ruby, what's the relationship between ' new ' and ' initialize '? new typically calls initialize . The default implementation of new is something like: class Class def new(*args, &block) obj = allocate obj.initialize(*args, &block) # actually

Class methods which create new instances

巧了我就是萌 提交于 2019-12-17 02:31:55
问题 Apart from the standard [[MyClass alloc] init] pattern, some objects are built from static methods like MyClass *obj = [MyClass classWithString:@"blabla"] According to widespread memory management guides (including Apple's), you're only responsible for releasing the objects that you alloc . Can anyone provide me with a template for such methods? How do you return the allocated object ( [self alloc]; return self; , perhaps)? How do you make sure that it will be released? 回答1: They are class

Why can in-class initializers only use = or {}?

微笑、不失礼 提交于 2019-12-16 20:03:06
问题 In-class initializers (C++11 feature) must be enclosed in curly braces or follow a = sign. They may not be specified inside parenthesis. What is the reason for this? 回答1: I am not 100% positive about this, but this might be to prevent a syntax ambiguity. For example, consider the following class: class BadTimes { struct Overloaded; int Overloaded; // Legal, but a very strange idea. int confusing(Overloaded); // <-- This line }; What does the indicated line mean? As written, this is a