在我写的应用程序中,我需要编写许多基类型,这很可能是不可变的.但我想知道可变类型在并行应用程序中如何与不可变类型进行比较.
你可以使用带有可变对象的锁,对吧?它与并行应用程序中不可变类型使用的其他技术相比如何?
你至少不使用具有不可变类型的锁,对吧?
我在OCaml中创建了一个可变数据结构,但是当我去访问它时,它会产生一个奇怪的错误,
这是我的代码
type vector = {a:float;b:float};;
type vec_store = {mutable seq:vector array;mutable size:int};;
let max_seq_length = ref 200;;
exception Out_of_bounds;;
exception Vec_store_full;;
let vec_mag {a=c;b=d} = sqrt( c**2.0 +. d**2.0);;
let make_vec_store() =
let vecarr = ref ((Array.create (!max_seq_length)) {a=0.0;b=0.0}) in
{seq= !vecarr;size=0};;
Run Code Online (Sandbox Code Playgroud)
当我在ocaml顶级做这个
let x = make _ vec _store;;
Run Code Online (Sandbox Code Playgroud)
然后尝试做x.size我得到这个错误
Error: This expression has type unit -> vec_store
but an expression was expected of type vec_store
Run Code Online (Sandbox Code Playgroud)
什么似乎是问题?我不明白为什么这不起作用.
谢谢,费萨尔
对于提出这样一个微不足道的问题我感到有点惭愧,但在这里我走了.
我需要一个函数来增加一个全局定义的可变变量.
let seed_index = ref 0;;
let incr_seed() =
seed_index := !seed_index + 1;;
Run Code Online (Sandbox Code Playgroud)
但是,我无法让它在翻译中工作.
# incr_seed();;
- : unit = ()
# seed_index;;
- : int ref = {contents = 0}
Run Code Online (Sandbox Code Playgroud) 当我输入以下代码时
x=[1,2,4]
print(x)
print("x",id(x))
x=[2,5,3]
print(x)
print("x",id(x))
Run Code Online (Sandbox Code Playgroud)
它给出了输出
[1, 2, 4]
x 47606160
[2, 5, 3]
x 47578768
Run Code Online (Sandbox Code Playgroud)
如果列表是可变的,那么为什么在更改列表x时它会给出2个内存地址?
我真的不知道如何克服这个问题.据我所知,它words被移入封闭(这对我来说很好,它是唯一可以在此之后使用的地方),但需要根据而来typed_some.错误提示听起来像一个体面的想法,只是该部分在一个库中,我不知道它是否是他们可以实现的东西.
on_edit文档.
extern crate cursive;
extern crate rand;
use cursive::Cursive;
use cursive::views::{Dialog, TextView, EditView, LinearLayout};
use cursive::traits::Identifiable;
use rand::Rng;
fn main() {
// This really messes with stdout. Seems to disable it by default but when
// siv is running println prints in random places on the screen.
let mut siv = Cursive::new();
siv.add_global_callback('q', |s| s.quit());
let mut words = WordBar::new();
siv.add_layer(Dialog::around(LinearLayout::vertical()
.child(TextView::new(words.update_and_get_bar()).with_id("target_field"))
.child(EditView::new()
.on_edit(move |s, input, _| words.typed_some(s, input))
.with_id("input_field")))
.title("Keyurses")
.button("Quit", |s| s.quit())); …Run Code Online (Sandbox Code Playgroud) 我没有找到任何与mutable constSO 相关的主题.我已经将代码减少到最小的工作代码(在visual studio上).如果我们取消注释//*data = 11;,编译器会抱怨const-ness.我想知道怎么mutable const运作.
class A
{
public:
void func(int & a) const
{
pdata = &a;
//*pdata = 11;
}
mutable const int * pdata;
};
int main()
{
const A obj;
int a = 10;
obj.func(a);
}
Run Code Online (Sandbox Code Playgroud) 我在下面有一个快速测试:
#include<iostream>
using namespace std;
int main(){
int i=2;
auto f=[=]()mutable{++i;};
f();
f();
cout<<i<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但结果仍然打印"2".为什么我没有在可变lambda中修改?我正在使用clang --std = c ++ 1z.
谢谢!
在OCaml中是否有一种方法可以使函数内部的变量在函数调用之间保持其值?它应该像Pythons默认参数一样工作,它是在每个函数调用中对同一个对象的引用,或者函数应该是yield而不是显式返回一个值.效果应该如下(如果函数是返回自然数):
foo ();;
0
foo ();;
1
Run Code Online (Sandbox Code Playgroud) 我正在尝试Rust并且在理解"借用"方面存在问题.
struct Foo<T> {
data: T,
}
impl<T> Foo<T> {
fn new(data: T) -> Self {
Foo {
data: data,
}
}
}
fn main() {
let mut foo = Foo::new("hello");
let x = &mut foo;
let y = &mut foo;
println!("{}", foo.data);
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码编译没有错误?毕竟,我得到了多个可变引用foo.以下内容写入文档:
参考规则
让我们回顾一下我们讨论的关于参考文献的内容:
a)在任何给定时间,您可以拥有(但不是两个)一个可变引用或任意数量的不可变引用.
b)参考文献必须始终有效.
这种行为的原因是什么?谢谢!
我想知道在OCaml中是否有可能在同一记录的另一个字段中使用一个记录字段.
基本上,我有字段功能,我想在其中使用同一记录的其他,值,字段,所以当值更改时,函数将使用新值.
我可以设置功能字段mutable并在创建记录后更新它,例如
type 'a cell =
{ mutable value: 'a
; mutable fn: unit -> 'a }
let create_cell ~(value : 'a) : 'a cell =
let c = {value; fn= (fun () -> value + 42)} in
let _ = c.fn <- (fun () -> c.value + 42) in
c
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能没有fn字段是可变的并且一气呵成.