我正在尝试通过智能克隆和借用来优化应用程序,并且我正在观察以下行为。下面的程序将无法运行:
fn f( string: String) {
println!("{}", string );
}
fn main() {
let my_string: String = "ABCDE".to_string();
f( my_string );
f( my_string );
}
Run Code Online (Sandbox Code Playgroud)
它会生成众所周知的“移动后使用”错误。
fn f( string: String) {
println!("{}", string );
}
fn main() {
let my_string: String = "ABCDE".to_string();
f( my_string );
f( my_string );
}
Run Code Online (Sandbox Code Playgroud)
这个可以通过克隆来解决my_string。下面的程序运行良好:
fn f( string: String) {
println!("{}", string );
}
fn main() {
let my_string: String = "ABCDE".to_string();
f( my_string.clone() );
f( my_string.clone() );
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您在多线程环境中使用相同的方法,克隆就不再有帮助了。当函数调用嵌入到线程中时: …
如果我在 lambda 中捕获“this”-ptr,我就可以毫无问题地调用成员函数。但是,当我明确捕获指针(不提及“this”)时,它会停止工作。难道我做错了什么?根据我的理解,指针应该是一样的,所以这真的让我很惊讶。是否有一些编译器魔法以特殊的方式处理“this”?
#include <cstdio>
#include <string>
struct client
{
auto foo(std::string&& other)
{
printf("%s!\n", other.data());
}
void local()
{
std::string str = "Hello World this is a sentence to long for sso!";
auto lambda = [this, other = std::move(str)]() mutable {
foo(std::move(other));
}();
}
static auto external(void* ptr) {
std::string str = "Hello World this is a sentence to long for sso!";
client* conv_ptr = static_cast<client*>(ptr);
auto lambda = [conv_ptr, other = std::move(str)]() mutable {
foo(std::move(other));
}();
} …Run Code Online (Sandbox Code Playgroud) 我有一个关于 kotlin lambda 和对this. 让我们看一下这段代码:
class MyViewModel: ViewModel {
val eventFlow = mutableSharedFlow<Unit>()
}
class MyFragment: Fragment() {
val viewModel = MyViewModel()
val lambda_1: (Unit) -> Unit = { Log.v("TAG", "Event happened") }
val lambda_2: (Unit) -> Unit = { processEvent() }
fun processEvent() {
//do smth
}
override fun onCreate(bundle: Bundle?) {
lifecycleScope.launchWhenStarted {
viewModel.eventFlow.collect(lambda_1)
}
lifecycleScope.launchWhenStarted {
viewModel.eventFlow.collect(lambda_2)
}
}
}
Run Code Online (Sandbox Code Playgroud)
请告诉我我的说法是否正确:
lambda_1不调用 的任何方法或变量MyFragment,因此它不保存对MyFragment对象的引用。lambda_2确实从 调用方法MyFragment,因此它确实持有对对象的引用MyFrgament。 …我正在用C++编写一个愚蠢的小应用程序来测试我的一个库.我希望应用程序向用户显示命令列表,允许用户键入命令,然后执行与该命令关联的操作.听起来很简单.在C#中,我最终会编写一个命令列表/映射,如下所示:
class MenuItem
{
public MenuItem(string cmd, string desc, Action action)
{
Command = cmd;
Description = desc;
Action = action;
}
public string Command { get; private set; }
public string Description { get; private set; }
public Action Action { get; private set; }
}
static void Main(string[] args)
{
var items = new List<MenuItem>();
items.Add(new MenuItem(
"add",
"Adds 1 and 2",
()=> Console.WriteLine(1+2)));
}
Run Code Online (Sandbox Code Playgroud)
有关如何在C++中实现此目的的任何建议?我真的不想为每个命令定义单独的类/函数.我可以使用Boost,但不能使用TR1.
我正在创建自己的JavaScript类数组对象,我有调用闭包的方法.我只是想知道定义闭包的最有效位置在哪里.
例如,假设我有一个map函数和一个chop函数:
MyObject.prototype =
{
map: function(fn) { ... applies fn to each element ... };
chop: function()
{ this.map(
function(element)
{
... chop off last character ...
}
)};
}
Run Code Online (Sandbox Code Playgroud)
这样做更有效率吗?
MyObject.prototype =
{
map: function(fn) { ... applies fn to each element ... };
__chop: function(element)
{
... chop off last character ...
}
chop: function()
{ this.map(this.__chop) };
}
Run Code Online (Sandbox Code Playgroud) 目前的设置:
var placeId;
function selectPlace(place) {
$('#selectPlace').html('Selected Place: <b>' + place.Name + '</b>');
$('#map').hide(400);
placeId = place.Id;
}
$(document).ready(function()
{
$('#postMessage').click(function() {
alert("PlaceId: " + placeId);
});
});
Run Code Online (Sandbox Code Playgroud)
可以/我应该使用闭包吗?
是否可以使用闭包在Javascript中模拟常量?如果是的话,你能告诉我一个例子吗?
如何从Java匿名类中获取输出?在.Net中我会使用闭包.
executor = Executors.newSingleThreadExecutor();
final Runnable runnable = new Runnable() {
public Exception exception;
@Override
public void run() {
try {
doSomething();
}
catch (Exception exception) {
// I'd like to report this exception, but how?
// the exception member is not readable from outside the class (without reflection...)
this.exception = exception;
}
}
};
executor.submit(runnable);
// Here I'd like to check if there was an exception
Run Code Online (Sandbox Code Playgroud) 当点击动态生成的导航"节点"时,我试图传递变量的当前值.这需要只是一个整数,但它总是导致最后一个节点的值..尝试了一些不同的方法来传递值,一个自定义事件监听器,一个setter,但我怀疑它是一个闭包问题..帮助将不胜感激;-)
function callGrid():void {
for (var i:Number = 0; i < my_total; i++) {
var gridnode_url = my_grid[i].@gridnode;
var news_category= my_grid[i].@category;
var newstitle = my_grid[i].@newstitle;
var news_content = my_grid[i]..news_content;
var news_image = my_grid[i]..news_image;
var gridnode_loader = new Loader();
container_mc.addChild(gridnode_loader);
container_mc.mouseChildren = false;
gridnode_loader.load(new URLRequest(gridnode_url));
gridnode_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, gridLoaded);
gridnode_loader.name = i;
text_container_mc = new MovieClip();
text_container_mc.x = 0;
text_container_mc.mouseEnabled = false;
var textY = text_container_mc.y = (my_gridnode_height+18)*y_counter;
addChild(text_container_mc);
var tf:TextSplash=new TextSplash(newstitle,10,0,4 );
container_mc.addChild(tf);
tf.mouseEnabled = false;
tf.height = my_gridnode_height;
text_container_mc.addChild(tf);
var text_container_mc_tween …Run Code Online (Sandbox Code Playgroud) 如何在该方法中调用test()?这是可能的?
(function() {
tinymce.create('tinymce.plugins.WrImagerPlugin', {
init : function(editor, url) {
editor.addCommand('mceWrImagerLink', function() {
//--> how can i refer to test() here?
});
},
test: function () {alert('test');}
}
});
tinymce.PluginManager.add('wr_imager', tinymce.plugins.WrImagerPlugin);
})();
Run Code Online (Sandbox Code Playgroud)