在D中定义关联数组时出错 [英] Error in Defining an associative array in D

查看:95
本文介绍了在D中定义关联数组时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想实现一个哈希查找,以将D中的密码子翻译为氨基酸。

So I would like to implement a hash lookup for translating codons to amino acids in D. When i write

int[string] codon_table = [
  "ATG": 'M',
  "TTT": 'F', "TTC": 'F', "TTA": 'L',
  "TTG": 'L', "CTT": 'L', "CTC": 'L',
  "CTA": 'L', "CTG": 'L', "ATT": 'I',
  "ATC": 'I', "ATA": 'I', "GTT": 'V',
  "GTC": 'V', "GTA": 'V', "GTG": 'V',
  "TCT": 'S', "TCC": 'S', "TCA": 'S',
  "TCG": 'S', "CCT": 'P', "CCC": 'P',
  "CCA": 'P', "CCG": 'P', "ACT": 'T',
  "ACC": 'T', "ACG": 'T', "GCT": 'A',
  "GCC": 'A', "GCA": 'A', "GCG": 'A',
  "TAT": 'Y', "TAC": 'Y', "TAA": '*',
  "TAG": '*', "CAT": 'H', "CAC": 'H',
  "CAA": 'Q', "CAG": 'Q', "AAT": 'N',
  "AAC": 'N', "AAA": 'K', "AAG": 'K',
  "GAT": 'D', "GAC": 'D', "GAA": 'E',
  "GAG": 'E', "TGT": 'C', "TGC": 'C',
  "TGA": '*', "TGG": 'W', "CGT": 'R',
  "CGC": 'R', "CGA": 'R', "CGG": 'R',
  "AGT": 'S', "AGC": 'S', "AGA": 'R',
  "AGG": 'R', "GGT": 'G', "GGC": 'G',
  "GGA": 'G', "GGG": 'G'
  ];

代码无法编译,我得到

Error: no-constant expression

我认为这是有点奇怪,因为我虽然将关联数组写为常量并将其用作查找表是微不足道的。例如,当我追加枚举时;

I think this is slightly odd because I though it would trivial to write an associate array as a constant and use it as a lookup table. When I append enum for example;

enum int[string] codon_table = [...]

它似乎有效。

使用枚举 static int [string] 我在做什么错了?

What are the tradeoffs of using enum vs static int[string] when defining this sort of associative array? What am I doing wrong?

推荐答案

尝试一下:

immutable int[string] codon_table;
static this() {
    codon_table = [
        "ATG": 'M',
        // ...
        "GGA": 'G', "GGG": 'G'
    ];
}

我使表格不可变,因为您不太可能需要更改查询表。

I made the table immutable since it's unlikely that you're going to need to change a lookup table.

也就是说,您的初始值设定项无法正常工作有点奇怪。您可能希望将此发布到D论坛上。

That said, it's a bit weird that your initializer doesn't work. You might want to post this on the D forums.

这篇关于在D中定义关联数组时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆