`
hqs7636
  • 浏览: 215248 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Traits 特征 2.014

阅读更多
6.14 翻译

(d语言的反射,刚刚翻译了2/3了,浏览器异常退出,气死我。  6.14)

重来了
1.028里没有,谁能翻译一下

请参考:
C++ Traits http://www.cnblogs.com/hush/archive/2004/03/10/2717.html
C++ Type traits http://jjhou.csdn.net/programmer-6-type-traits-ddj.htm

Traits are extensions to the language to enable programs, at compile time, to get at information internal to the compiler. This is also known as compile time reflection. It is done as a special, easily extended syntax (similar to Pragmas) so that new capabilities can be added as required.

特征(Traits)是对程序语言的扩展,在编译期,对编译器内部的信息得到成为可能。 这也被称之为编译期反射。 它被作为特殊的,容易扩展的语法(类似于Pragmas ),这样新的能力能根据需要被添加。

TraitsExpression:
    __traits ( TraitsKeyword , TraitsArguments )

TraitsKeyword:
    isAbstractClass
    isArithmetic
    isAssociativeArray
    isFinalClass
    isFloating
    isIntegral
    isScalar
    isStaticArray
    isUnsigned
    isVirtualFunction
    isAbstractFunction
    isFinalFunction
    hasMember
    getMember
    getVirtualFunctions
    classInstanceSize
    allMembers
    derivedMembers
    isSame
    compiles

TraitsArguments:
    TraitsArgument
    TraitsArgument , TraitsArguments

TraitsArgument:
    AssignExpression
    Type



isArithmetic

If the arguments are all either types that are arithmetic types, or expressions that are typed as arithmetic types, then true is returned. Otherwise, false is returned. If there are no arguments, false is returned.

如果变元是是算术类型,或者表达式是算术类型,那么返回true。 否则,返回false。 如果没有变元,返回false。

import std.stdio;

void main()
{
    int i;
    writefln(__traits(isArithmetic, int));
    writefln(__traits(isArithmetic, i, i+1, int));
    writefln(__traits(isArithmetic));
    writefln(__traits(isArithmetic, int*));
}


Prints:

true
true
false
false


1、isFloating

Works like isArithmetic, except it's for floating point types (including imaginary and complex types).

像isArithmetic一样的工作,浮点类型除外(包括虚数和复数类型)。


2、isIntegral

Works like isArithmetic, except it's for integral types (including character types).

3、isScalar
Works like isArithmetic, except it's for scalar(标量?) types.

4、isUnsigned
Works like isArithmetic, except it's for unsigned无符号 types.

5、isStaticArray
Works like isArithmetic, except it's for static array types.

isAssociativeArray
Works like isArithmetic, except it's for associative关联 array types.


6、isAbstractClass

If the arguments are all either types that are abstract classes, or expressions that are typed as abstract classes, then true is returned. Otherwise, false is returned. If there are no arguments, false is returned.

如果变元是抽象类,或者表达式(的结果)是抽象类,那么返回true。 否则,返回false。 如果没有变元,返回false。

import std.stdio;

abstract class C { int foo(); }

void main()
{
    C c;
    writefln(__traits(isAbstractClass, C));
    writefln(__traits(isAbstractClass, c, C));
    writefln(__traits(isAbstractClass));
    writefln(__traits(isAbstractClass, int*));
}


Prints:

true
true
false
false


7、isFinalClass

Works like isAbstractClass, except it's for final classes.
final classes出外


8、isVirtualFunction

Takes one argument. If that argument is a virtual function, true is returned, otherwise false.

接受一个变元。 如果那变元是一个虚函数,返回true,否则(返回)false。

import std.stdio;

struct S
{
  void bar() { }
}

class C
{
  void bar() { }
}

void main()
{
    writefln(__traits(isVirtualFunction, C.bar));  // true
    writefln(__traits(isVirtualFunction, S.bar));  // false
}



9、isAbstractFunction  (抽象函数)

Takes one argument. If that argument is an abstract function, true is returned, otherwise false.

import std.stdio;

struct S
{
  void bar() { }
}

class C
{
  void bar() { }
}

class AC
{
  abstract void foo();
}

void main()
{
    writefln(__traits(isAbstractFunction, C.bar));   // false
    writefln(__traits(isAbstractFunction, S.bar));   // false
    writefln(__traits(isAbstractFunction, AC.foo));  // true
}



10、isFinalFunction

Takes one argument. If that argument is a final function, true is returned, otherwise false.

import std.stdio;

struct S
{
  void bar() { }
}

class C
{
  void bar() { }
  final void foo();
}

final class FC
{
  void foo();
}

void main()
{
    writefln(__traits(isFinalFunction, C.bar));	  // false
    writefln(__traits(isFinalFunction, S.bar));	  // false
    writefln(__traits(isFinalFunction, C.foo));	  // true
    writefln(__traits(isFinalFunction, FC.foo));  // true
}



11、hasMember

The first argument is a type that has members, or is an expression of a type that has members. The second argument is a string. If the string is a valid property of the type, true is returned, otherwise false.

(接受两个变元)第一个变元是一个有成员的类型,或者表达式(的结果)是有成员的类型(class、struct等)。 第二个变元是一个字符串。 如果字符串是类型的一种有效的属性,返回true,否则(返回)false。

import std.stdio;

struct S
{
    int m;
}

void main()
{   S s;

    writefln(__traits(hasMember, S, "m")); // true
    writefln(__traits(hasMember, s, "m")); // true
    writefln(__traits(hasMember, S, "y")); // false
    writefln(__traits(hasMember, int, "sizeof")); // true
}



12、getMember

Takes two arguments, the second must be a string. The result is an expression formed from the first argument, followed by a '.', followed by the second argument as an identifier.

接受两个变元,第二个必须是一个字符串。 结果是由第一个变元构成的表达式,其后是一‘.’,其后第二个变元是一个标识符。(象 s.mx 这样)


import std.stdio;

struct S
{
    int mx;
    static int my;
}

void main()
{ S s;

  __traits(getMember, s, "mx") = 1;  // same as s.mx=1;
  writefln(__traits(getMember, s, "m" ~ "x")); // 1

  __traits(getMember, S, "mx") = 1;  // error, no this for S.mx
                                     //mx对象实例成员,需要实例化后才能得到
  __traits(getMember, S, "my") = 2;  // ok
                                     //my 是static的,是类成员。 
}



13、getVirtualFunctions

The first argument is a class type or an expression of class type. The second argument is a string that matches the name of one of the functions of that class. The result is an array of the virtual overloads of that function.

第一个变元是一个class类型,或者class类型的表达式, 第二个变元是与类函数名(之一)匹配的字符串。 结果是有效重载函数的一个数组。(大概是把所有有效重载的函数名放在一个数组里)



import std.stdio;

class D
{
    this() { }
    ~this() { }
    void foo() { }
    int foo(int) { return 2; }
}

void main()
{
    D d = new D();

    foreach (t; __traits(getVirtualFunctions, D, "foo"))
	writefln(typeid(typeof(t)));

    alias typeof(__traits(getVirtualFunctions, D, "foo")) b;
    foreach (t; b)
	writefln(typeid(t));

    auto i = __traits(getVirtualFunctions, d, "foo")[1](1);
    writefln(i);
}



Prints:

void()
int()
void()
int()
2


14、classInstanceSize

Takes a single argument, which must evaluate to either a class type or an expression of class type. The result is of type size_t, and the value is the number of bytes in the runtime instance of the class type. It is based on the static type of a class, not the polymorphic type.

接受一个单一的变元,这必须对clss类型或者class类型表达式求值。 结果是类型size_t,而值是class类型的运行时实例中的字节数。 它基于一个类的静态类型,不是多态类型。


15、allMembers

Takes a single argument, which must evaluate to either a type or an expression of type. An array of string literals is returned, each of which is the name of a member of that type combined with all of the members of the base classes (if the class is a type). No name is repeated. Builtin properties are not included.

接受一个单一的变元,这必须对类型或者类型表达式求值。 返回一个(字符)串文字数组,

其中每个名称是由那个类型的(及其?)基类(直接继承的类?待测试)的所有成员构成。?(如果class是一个类型)。

没有名称被重复。 构建属性?不被包括在内。

import std.stdio;

class D
{
    this() { }
    ~this() { }
    void foo() { }
    int foo(int) { return 0; }
}

void main()
{
    auto a = __traits(allMembers, D);
    writefln(a);
    // [_ctor,_dtor,foo,print,toString,toHash,opCmp,opEquals]
}


The order in which the strings appear in the result is not defined.
字符串在结果中出现的顺序没有定义

16、derivedMembers 派生成员

Takes a single argument, which must evaluate to either a type or an expression of type. An array of string literals is returned, each of which is the name of a member of that type. No name is repeated. Base class member names are not included. Builtin properties are not included.

接受一个单一的变元,这必须对类型或类型表达式求值。 返回一个字符串文字的数组,其中每个名称是那类型的一个成员。 名称不被重复。 基类成员名称不被包括。 构建属性不被包括。

import std.stdio;

class D
{
    this() { }
    ~this() { }
    void foo() { }
    int foo(int) { return 0; }
}

void main()
{
    auto a = __traits(derivedMembers, D);
    writefln(a);	// [_ctor,_dtor,foo]
}


The order in which the strings appear in the result is not defined.


17、isSame

Takes two arguments and returns bool true if they are the same symbol, false if not.

接受两个变元,如果他们是相同的符号,就返回true ,否则(返回)false。

import std.stdio;

struct S { }

int foo();
int bar();

void main()
{
    writefln(__traits(isSame, foo, foo)); // true
    writefln(__traits(isSame, foo, bar)); // false
    writefln(__traits(isSame, foo, S));   // false
    writefln(__traits(isSame, S, S));     // true
    writefln(__traits(isSame, std, S));   // false
    writefln(__traits(isSame, std, std)); // true
}


If the two arguments are expressions made up of literals or enums that evaluate to the same value, true is returned.

如果两个变元是有由文字literals或者枚举enums组成的表达式,那么计算到相同的值,就返回true。


18、compiles

Returns a bool true if all of the arguments compile (are semantically correct). The arguments can be symbols, types, or expressions that are syntactically correct. The arguments cannot be statements or declarations.

如果所有变元编译(是语义上正确的),返回true。 变元可以是句法上正确的的符号,类型,或者表达式。 变元不能是语句或者声明。

If there are no arguments, the result is false.

如果没有,结果是false

import std.stdio;

struct S
{
    static int s1;
    int s2;
}

int foo();
int bar();

void main()
{
    writefln(__traits(compiles));                      // false
    writefln(__traits(compiles, foo));                 // true
    writefln(__traits(compiles, foo + 1));             // true
    writefln(__traits(compiles, &foo + 1));            // false
    writefln(__traits(compiles, typeof(1)));           // true
    writefln(__traits(compiles, S.s1));                // true
    writefln(__traits(compiles, S.s3));                // false
    writefln(__traits(compiles, 1,2,3,int,long,std));  // true
    writefln(__traits(compiles, 3[1]));                // false
    writefln(__traits(compiles, 1,2,3,int,long,3[1])); // false
}



This is useful for:
这是有益的:

Giving better error messages inside generic code than the sometimes hard to follow compiler ones.
在代码内部有时能比努力的遵从编译器给出更好的错误信息。?

Doing a finer grained specialization than template partial specialization allows for.
允许比模板部分做的更精细更专业化。  ?





。。。。。。。。。。。。。
加上废掉的部分总共用了6个小时。  14:30完成
分享到:
评论
4 楼 hqs7636 2008-06-15  
哈哈,漂亮,是有点信达雅的意思,谢谢。我翻译的有点像文言文
3 楼 Colorful 2008-06-15  
我理解的意思就是指虚函数,因为经过重载override的函数还是虚函数。

在这里没必要抠字眼。

后面这一段,我们可以这样翻译。

allMembers只接受一个参数,而该参数必须是某类型或类型表达式。
它返回一个字符串数组,而其中每一项都是该类型的成员名称。如果该类型是个类类型的话,还包括基类的成员名称。
数组中所有名称都不重复[译注:这意味着不能确定是否有函数重载],并且不包含内置属性。[译注:这意味着数组中没有诸如stringof,sizeof等名称]
2 楼 hqs7636 2008-06-15  
感谢Colorful兄指教,13、The result is an array of the virtual overloads of that function
其中virtual overloads 怎么理解?虚重载?
感觉你的翻译是对的,符合 getVirtualFunctions 的本意。

还有
15、allMembers

Takes a single argument, which must evaluate to either a type or an expression of type. An array of string literals is returned, each of which is the name of a member of that type combined with all of the members of the base classes (if the class is a type). No name is repeated. Builtin properties are not included.

其中:
each of which is the name of a member of that type combined with all of the members of the base classes

翻成:
其中每个名称是由那个类型的(及其?)基类(直接继承的类?待测试)的所有成员构成。?(如果class是一个类型)。

还有点不踏实。

1 楼 Colorful 2008-06-14  
老实说,翻译的有些惨不忍睹。
翻译的专业词汇值得商榷,也不用逐字翻译。

我翻译了前13个,不敢说一定比楼主翻译的好,大家互相学习吧。

剩下的就交给楼主修正了,敬佩楼主的毅力。

引用
Traits
Traits 是 D 语言扩展,它使程序能在编译时便获得编译器内部信息。这也被称为编译时反射。 我们可以把它作为一项特别的,容易扩展的语法(类似于 Pragmas)以便于按需添加新能力。

TraitsExpression/a>:raitsKeyword , TraitsArguments )

TraitsKeyword:
    isAbstractClass
    isArithmetic
    isAssociativeArray
    isFinalClass
    isFloating
    isIntegral
    isScalar
    isStaticArray
    isUnsigned
    isVirtualFunction
    isAbstractFunction
    isFinalFunction
    hasMember
    getMember
    getVirtualFunctions
    classInstanceSize
    allMembers
    derivedMembers
    isSame
    compiles

TraitsArguments:
    TraitsArgument
    TraitsArgument , TraitsArguments

TraitsArgument:
    AssignExpression
    Type
isArithmetic
isArithmetic

如果参数所有类型都是算术类型,或表达式(结果类型)可以转型为算数类型,返回 true 。否则,返回 false 。如果没有参数,同样返回 false 。

importd main()
{
    int i;
    writefln(__traits(isArithmetic, int));
    writefln(__traits(isArithmetic, i, i+1, int));
    writefln(__traits(isArithmetic));
    writefln(__traits(isArithmetic, int*));
}
Prints:

true
true
false
false
isFloating
类似 isArithmetic, 只不过判断其是否为浮点类型 (包括虚数和复数类型)。

isIntegral
类似 isArithmetic, 只不过判断其是否为整型 (包括字符类型)。

isScalar
类似 isArithmetich, 只不过判断其是否为标量。

isUnsigned
类似 isArithmetic

isStaticArray
类似 isArithmetic, 只不过判断其是否为静态数组类型。.

isAssociativeArray
类似 isArithmetic, 只不过判断其是否为关联数组类型。

isAbstractClass
如果参数所有类型都是抽象类,或者表达式(结果类型) 可以转型为抽象类,返回 true 。否则,返回 false. 如果没有参数,同样返回 false 。

import { int foo(); }

void main()
{
    C c;
    writefln(__traits(isAbstractClass, C));
    writefln(__traits(isAbstractClass, c, C));
    writefln(__traits(isAbstractClass));
    writefln(__traits(isAbstractClass, int*));
}
Prints:true
true
false
false
isFinalClass类似 isAbstractClass, 只不过判断其是否为 final 类。isVirtualFunction判断参数是否为虚函数。如果是,返回 true。否则,返回 false. [译注:这里可以输入多个参数。下同。]import) { }
}

class C
{
  void bar() { }
}

void main()
{
    writefln(__traits(isVirtualFunction, C.bar));  // true
    writefln(__traits(isVirtualFunction, S.bar));  // false
}
isAbstractFunction判断参数是否为抽象函数。如果是,返回 true 。否则,返回 false. import) { }
}

class C
{
  void bar() { }
}

class AC
{
  abstract void foo();
}

void main()
{
    writefln(__traits(isAbstractFunction, C.bar));   // false
    writefln(__traits(isAbstractFunction, S.bar));   // false
    writefln(__traits(isAbstractFunction, AC.foo));  // true
}
isFinalFunction判断参数是否为 final 函数。如果是,返回 true 。否则,返回 false.  import) { }
}

class C
{
  void bar() { }
  final void foo();
}

final class FC
{
  void foo();
}

void main()
{
    writefln(__traits(isFinalFunction, C.bar));   // false
    writefln(__traits(isFinalFunction, S.bar));   // false
    writefln(__traits(isFinalFunction, C.foo));   // true
    writefln(__traits(isFinalFunction, FC.foo));  // true
}
hasMember第一个参数指拥有成员的类型或类型表达式。第二个参数是一个字符串。如果该字符串是一个合法的类型属性,返回 true 。否则返回 false. importass="d_keyword">void main()
{   S s;

    writefln(__traits(hasMember, S, "m")); // true
    writefln(__traits(hasMember, s, "m")); // true
    writefln(__traits(hasMember, S, "y")); // false
    writefln(__traits(hasMember, int, "sizeof")); // true
}
getMember同上。结果为两个参数的格式化表达式。形如第一个参数.第二个参数 = 结果; import> my;
}

void main()
{ S s;

  __traits(getMember, s, "mx") = 1;  // same as s.mx=1;
  writefln(__traits(getMember, s, "m" ~ "x")); // 1

  __traits(getMember, S, "mx") = 1;  // error, no this for S.mx
  __traits(getMember, S, "my") = 2;  // ok
}
getVirtualFunctions第一个参数为类类型或类类型表达式。第二个参数是匹配类中函数名之一的一个字符串。结果则为一个虚函数数组。importn> foo(int) { return 2; }
}

void main()
{
    D d = new D();

    foreach (t; __traits(getVirtualFunctions, D, "foo"))
writefln(typeid(typeof(t)));

    alias typeof(__traits(getVirtualFunctions, D, "foo")) b;
    foreach (t; b)
writefln(typeid(t));

    auto i = __traits(getVirtualFunctions, d, "foo")[1](1);
    writefln(i);
}
Prints:void()
int()
void()
int()
2

相关推荐

Global site tag (gtag.js) - Google Analytics