D是否具有足够表达的类型系统以使动态工作变得可行?

Arl*_*len 4 type-systems d

D是否具有足够表达的类型系统,以使在静态类型框架内动态工作(即,具有多个值类别)是可行的

我问,阅读动态语言后是静态语言.示例代码(如果有)非常受欢迎.

Pet*_*der 9

如果你只使用std.variant.Variant那么D本质上是一种动态类型的语言.以下是从库参考页面使用它的示例:

Variant a; // Must assign before use, otherwise exception ensues

// Initialize with an integer; make the type int
Variant b = 42;
assert(b.type == typeid(int));

// Peek at the value
assert(b.peek!(int) !is null && *b.peek!(int) == 42);

// Automatically convert per language rules
auto x = b.get!(real);

// Assign any other type, including other variants
a = b;
a = 3.14;
assert(a.type == typeid(double));

// Implicit conversions work just as with built-in types
assert(a > b);

// Check for convertibility
assert(!a.convertsTo!(int)); // double not convertible to int

// Strings and all other arrays are supported
a = "now I'm a string";
assert(a == "now I'm a string");
a = new int[42]; // can also assign arrays
assert(a.length == 42);
a[5] = 7;
assert(a[5] == 7);

// Can also assign class values
class Foo {}
auto foo = new Foo;
a = foo;
assert(*a.peek!(Foo) == foo); // and full type information is preserved
Run Code Online (Sandbox Code Playgroud)