val var1: Any = "Carmelo Anthony"
当我执行以下操作时,我的印象是::class.simpleName返回对象的变量类型:
val var1Type = var1::class.simpleName
print(var1Type)
我得到了String,但没有得到Any
但当我这样做时
val var2: String = var1
我得到一个Type mismatch: inferred type is Any but String was expected
type-inference variable-assignment type-mismatch assignment-operator kotlin
我有一个包含可变值的 Kotlin 类。
class StringWrapper(
var value: String = ""
) {
override fun toString(): String = value
}
Run Code Online (Sandbox Code Playgroud)
我使用这个包装器作为自定义数据持有者类中的属性
class DataHolder {
val name = StringWrapper()
override fun toString(): String = "Data(name=$name)"
}
Run Code Online (Sandbox Code Playgroud)
我想让它更容易地为内容赋值StringWrapper
val dataAlpha = DataHolder()
// the '.value' is unnecessary noise
dataAlpha.name.value = "alpha"
// I want to directly assign a string value, but I get an error
dataAlpha.name = "alpha" // ERROR Type mismatch.
// Required: StringWrapper
// Found: String
Run Code Online (Sandbox Code Playgroud)
我还想让复制一个StringWrapper到另一个变得更容易。
val …Run Code Online (Sandbox Code Playgroud) operator-overloading assignment-operator kotlin gradle-kotlin-dsl
为什么我不能分配给构造函数,当我可以分配给"看起来"像构造函数的函数?
例:
struct Bar {
Bar() : b_(false) {}
Bar(bool b) : b_(b) {}
};
struct Foo {
Foo(Bar const & bar) : bar_(bar) {}
Foo operator=(Foo const & f) { return Foo(f.bar_); }
const Bar & bar_;
}
Foo bool_to_foo(bool b) { return Foo(Bar(b)); }
Foo MakeFoo(Bar const & bar) { return Foo(bar); }
Run Code Online (Sandbox Code Playgroud)
为什么这样做:
Bar bar;
MakeFoo(bar) = bool_to_foo(true);
Run Code Online (Sandbox Code Playgroud)
什么时候不起作用?
Bar bar;
Foo(bar) = bool_to_foo(true); // Error: 'bar': redefinition; different basic types
Run Code Online (Sandbox Code Playgroud)
MakeFoo(.)和Foo(.)具有相同的签名和相同的功能.
构造函数有什么特别之处?
假设num是一个数组,
@num = (1 .. 10);
Run Code Online (Sandbox Code Playgroud)
要找到它的长度,我们使用以下表达式,
$num = @num;
Run Code Online (Sandbox Code Playgroud)
然后我$num在下面的作业中使用了相同的内容.
@slicenum = $num[1 .. 6];
Run Code Online (Sandbox Code Playgroud)
我知道,对于切片数组,我们使用@num而不是$num.它运行良好,没有任何错误.但是当我打印出@slicenum它的值时,它给了我2个结果.
@slicenum = $num[1 .. 6, 10 .. 20];
Run Code Online (Sandbox Code Playgroud)
对于上述作业,我得到了1.
虽然遗嘱值为$num10,但请解释上述两项任务中发生的情况.
我正在编写Write Yourself a Scheme教程,一个代码块让我想知道绑定和赋值之间的区别:
parseAtom = do first <- letter <|> symbol
rest <- many (letter <|> digit <|> symbol)
let atom = first:rest
return $ case atom of
"#t" -> Bool True
"#f" -> Bool False
_ -> Atom atom
Run Code Online (Sandbox Code Playgroud)
为什么let atom =而不是atom <-?因此,我试过:
parseAtom = do first <- letter <|> symbol
rest <- many (letter <|> digit <|> symbol)
atom <- first : rest
return $ case atom of
"#t" -> Bool …Run Code Online (Sandbox Code Playgroud) monads haskell compiler-errors assignment-operator do-notation
如何操作foo = int可以由foo(int)(转换构造函数)和foo::operator=(int)(重载赋值运算符)完成?当一个人被召唤而不是其他人(也许一个是基本的)?
#include <iostream>
class foo
{
public:
foo(){}
foo(int r_value)
{
std::cout << "\nfoo::foo(int)\n";
}
void operator=(int r_value)
{
std::cout << "\nfoo::operator=(int)\n";
}
};
int main()
{
foo f;
f = 57;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码使得operator=(int)当同时存在来运行和foo(int)如果operator=(int)被注释(或相反).
c++ constructor operator-overloading assignment-operator operator-keyword
这篇文章解释了为什么我使用这样的代码时会发出警告:
var htmlCollection = document.getElementsByClassName("class-name"),
i = htmlCollection.length,
htmlElement;
// Because htmlCollection is Live, we use a reverse iteration.
while (htmlElement = htmlCollection[--i]) { // **Warning?! Why?!**
htmlElement.classList.remove("class-name");
}
Run Code Online (Sandbox Code Playgroud)
但这并没有解释为什么在一段时间内分配表达式是一种不好的做法?».
我也读过这个stackoverflow的答案,指出这种做法是好的.所以...
while (element = element.parentNode)类似语法的性能问题还是样式代码推荐?
顺便说一句,似乎«--i»运算符也是一种不好的做法.我在这篇文章中读到:
已知++(递增)和 - (递减)运算符通过鼓励过多的诡计来导致错误的代码.
这是某种玩笑?
我有一个枚举,但我希望有一个赋值运算符,以便能够分配一个不是原始枚举的类型.例如
enum class X : int
{
A, B, C, D
}
enum class Y : char
{
A, B, C, D
}
Y& operator=(Y& lhs, X rhs)
{
return Y = static_cast<Y>(X);
}
Run Code Online (Sandbox Code Playgroud)
但我得到了一个'operator =' must be a non-static member.有没有办法做到这一点?
标题说明了一切.但是,请string作为任何课程的占位符.
std::string s1("hello"); // construct from arguments
std::string s2 = "hello"; // ???
std::string s3; // construct with default values
s3 = "hello"; // assign
Run Code Online (Sandbox Code Playgroud)
我想知道声明s2是否与for s1或for 相同s3.
c++ initialization variable-assignment copy-constructor assignment-operator
我有以下代码:
#include <iostream>
using namespace std;
class A{
int x;
public:
A(int x =1) : x(x) {cout << "A() ";}
A(const A& a) {x =a.x; cout << "A(const A&) ";}
A& operator=(const A& a){cout << "op= "; x=a.x; return *this;}
~A(){cout << "~A()";}
int getX() {return x;}
void setX(int x){this->x = x;}
};
A g(A a){
//a = 2;
cout << "g() ";
a.setX(3);
return a;
}
int main()
{
A a;
a = 2;
}
Run Code Online (Sandbox Code Playgroud)
我期望有以下输出:A() op= ~A() …
c++ ×5
constructor ×3
kotlin ×2
c++11 ×1
destructor ×1
do-notation ×1
enums ×1
haskell ×1
javascript ×1
monads ×1
perl ×1
while-loop ×1