2

我正在尝试做

var myVar = "my var";

match myVar {
    string s => { io:println("string"); }
    any k => { io:println("any var"); }
}

似乎这是不正确的。var和有什么区别any。我想当我var在芭蕾舞演员下面使用时会产生一个any?正确的?

4

2 回答 2

5

“any”是表示芭蕾舞女演员程序可以操作的所有值的类型。

any myVal = "this is a string value";

// Unsafe type cast, hence the union type.
string | error myStr = <string> myVal; 

// Following is also valid based on the definition of the "any" type. 
any myVal = 10;

"var" 是一种声明变量的方式,该变量的类型是从右侧表达式中推断出来的。一旦派生了变量的类型,您就只能分配该类型的值。

// This is equivalent to 'string a = "this is a string value";'
var a = "this is a string value"; 

// Now the following will result in a compilation failure. 
a = 10;  
于 2018-05-11T01:42:11.657 回答
0

想我找到了我的问题的答案。

““var”类型,类型是从右侧推断出来的,通过查看右侧的静态类型来确定。而“any”类型代表所有类型的集合。”

我不阅读文档的坏处:(

于 2018-05-11T01:07:06.963 回答