小编Vic*_*aev的帖子

在Dart中修改类中的最终字段

Dart文档阅读:

如果您从不打算更改变量,请使用final或const,而不是var或类型.最终变量只能设置一次;

好的,这意味着第二次分配最终变量将不起作用,没有任何关于修改的说法,即

void main() {
  final List<int> list = [1,2,3];  

  list = [10, 9, 8]; //error!

  list
  ..clear()
  ..addAll([10, 9, 8]); //works!
}
Run Code Online (Sandbox Code Playgroud)

从本质上讲,我可以看到,我重新分配了最终变量list.它是否与最终变量的整个概念相矛盾?

variables final dart

7
推荐指数
1
解决办法
926
查看次数

为什么在使用 TryFuture 而不是等效的 Future 时会收到有关类型不匹配的错误?

我有以下使用的工作代码Future

use futures::{future, Future};

fn fut_res() -> impl Future<Output = Result<(), failure::Error>> {
    future::ok::<(), failure::Error>(())
}

#[tokio::main]
async fn main() -> Result<(), failure::Error> {
    if let Err(e) = fut_res().await {
        println!("{:?}", e);
    }

    Ok(())
}
Run Code Online (Sandbox Code Playgroud)

根据我在文档中阅读的内容,我应该能够更改要使用的代码,TryFuture如下所示:

use futures::{future, TryFuture};

fn try_fut() -> impl TryFuture<Ok = (), Error = failure::Error> {
    future::ok::<(), failure::Error>(())
}

#[tokio::main]
async fn main() -> Result<(), failure::Error> {
    if let Err(e) = try_fut().await {
        println!("{:?}", e);
    }

    Ok(())
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨try_fut必须返回关联的类型Output …

future rust

5
推荐指数
1
解决办法
830
查看次数

如何从Dart列表中删除类似的元素?

我想从飞镖列表中删除相似的元素,这些相似性是由某些布尔函数给出的。例如,在Mathematica中,我将实现以下目标:

Union[{2, -2, 1, 3, 1}, SameTest -> (Abs[#1] == Abs[#2] &)]
Run Code Online (Sandbox Code Playgroud)

该语句产生以下列表-{-2,1,3}。实际上,我想从每个对等类中保留一个元素。

我知道有一个函数list.retainWhere((e)=> bool test(e)),不幸的是,此测试一次只能对一个值进行操作。当然,另一种选择是我可以做这样的事情(只是从脑子里写)

i=0;
for(final E in list) {
  i++;      
  for(j=i; j<list.skip(i).length; j++) {
     if sameTest(e, E) then list.removeAt(i+j);
  }  
}
Run Code Online (Sandbox Code Playgroud)

但是我觉得这有点丑。

有什么建议吗?

更新 我将更详细地阐明我的问题,然后说明如何使用下面给出的建议解决问题。

  class Pair<T> {
    final T left;
    final T right;
    Pair(this.left, this.right);    
  }
Run Code Online (Sandbox Code Playgroud)

现在,我想拥有一个包含这样的对或的结构,并且我不想保持彼此足够接近的点。为此我采用亚历山大Ardhuin和他的评论的解决方案也是如此,这实际上使得对于更复杂的情况有所不同:考虑2种元素e1e2您需要定义hashCode,以确保e1.hashCode == e2.hashCode如果e1 == e2

所以就这样:

int N=1000;

LinkedHashSet<Pair<double>> myset =
    new LinkedHashSet<Pair<double>>(
      equals: (Pair<double> e1, Pair<double> e2) …
Run Code Online (Sandbox Code Playgroud)

list elements equivalent dart

3
推荐指数
1
解决办法
1063
查看次数

在默认特征实现中使用关联常量

我想完成以下工作

trait Trait {
    const CONST: f64;
    fn fun(&self) -> f64 {
        1.0 + self.CONST
    }
}
Run Code Online (Sandbox Code Playgroud)

然后定义一堆struct-s 实现Trait不同的常量。如

struct Struct {}
impl Trait for Struct {
    const CONST: f64 = 1.0;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,前面的代码片段无法编译。我可以同时拥有关联常量和默认实现,但似乎我不能在默认实现中使用 const。这可能吗?

constants traits rust

3
推荐指数
1
解决办法
440
查看次数

Dart:列表列表

有人可以解释这段代码出了什么问题吗?从代码中(至少我自己)可以预期,在运行此代码后,数字列表看起来像numbers = [[0], [1]],但它看起来像numbers= [[0,1], [0,1]].

void main() {
  int n = 2;
  List<List<int>> numbers = new List.filled(n, []);

  for(int i=0; i<n; i++)
    for(int j=i+1; j<n; j++ ){
      numbers[i].add(0);
      numbers[j].add(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:这不仅仅是一种心理锻炼.

list dart

2
推荐指数
2
解决办法
3426
查看次数

C#:由List索引的字典

我尝试编写一个程序,其中Dictionary由List索引.(相信我,我有,并且有选项,但我喜欢按列表编制索引).有一个最小的工作(实际上不工作,只有一个最后一行是一个问题)示例:

using System;
using System.Collections.Generic;

namespace test
{
    class Program
    {
    static void Main(string[] args)
    {
        Dictionary<List<String>, int> h = new Dictionary<List<string>,int>();

        List<String> w = new List<string> {"a"};
        h.Add(w, 1);

        w = new List<string>{"b"};
        h.Add(w,2);

        w = new List<string>{"a"};

        int value = 0;
        h.TryGetValue(w, out value);
        Console.WriteLine(value+" "+h[w]);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果调试这个程序,他会清楚地看到h中有两个元素,但仍然无法通过正确的索引访问这些元素--- h [w].我错了还是有什么奇怪的事情发生?

c# dictionary indexed list

1
推荐指数
1
解决办法
912
查看次数

标签 统计

dart ×3

list ×3

rust ×2

c# ×1

constants ×1

dictionary ×1

elements ×1

equivalent ×1

final ×1

future ×1

indexed ×1

traits ×1

variables ×1