使用Casting null并不能作为灵感编译,并且来自Eric Lippert的评论:
这表明了一个有趣的案例."uint x =(int)0;" 即使int不能隐式转换为uint也会成功.
我们知道这不起作用,因为object无法分配到string:
string x = (object)null;
Run Code Online (Sandbox Code Playgroud)
但这确实如此,但直觉上它不应该:
uint x = (int)0;
Run Code Online (Sandbox Code Playgroud)
为什么不编译器允许这种情况下,int不隐式转换为uint?
.net c# compiler-construction type-conversion implicit-conversion
我有一个类,我想轻松写出字符串(例如用于记录目的).我可以使用隐式运算符隐式地将对象强制转换为字符串而不是覆盖ToString方法吗?
例如,我有一个人带班姓名和年龄:
public class Person
{
public string Name { get; set; }
public int Age { get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我可以覆盖ToString:
public override string ToString()
{
return String.Format("Name: {0}, Age: {1}", this.Name, this.Age);
}
Run Code Online (Sandbox Code Playgroud)
或者我可以使用隐式运算符:
public static implicit operator string(Person p)
{
return String.Format("Name: {0}, Age: {1}", p.Name, p.Age);
}
Run Code Online (Sandbox Code Playgroud)
现在,将此对象传递给期望字符串的方法,而不是
Log(Person.ToString());
Run Code Online (Sandbox Code Playgroud)
我可以打电话
Log(Person);
Run Code Online (Sandbox Code Playgroud)
我甚至可以在隐式转换中调用重写的ToString
public static implicit operator string(Person p)
{
return p.ToString();
}
Run Code Online (Sandbox Code Playgroud)
这是对String的隐式运算符强制转换的错误用法吗?需要此功能时的最佳做法是什么?我怀疑只是重载ToString将是最佳实践答案,如果是这样,我有几个问题:
在哪里sqlContext.implicits._定义$"字符串"来表示对父数据帧列的数据帧调用?具体来说,我对看到以下内容感到困惑:
import sqlContext.implicits._
df.where($"type".isin("type1","type2") and $"status".isin("completed","inprogress"))
Run Code Online (Sandbox Code Playgroud) 是否可以让编译器自动将我的Enum值转换为字符串,这样我就可以避免每次都显式调用ToString方法.这是我想做的一个例子:
enum Rank { A, B, C }
Rank myRank = Rank.A;
string myString = Rank.A; // Error: Cannot implicitly convert type 'Rank' to 'string'
string myString2 = Rank.A.ToString(); // OK: but is extra work
Run Code Online (Sandbox Code Playgroud) 在广泛阅读ISO/IEC 14882,编程语言 - C++后,我仍然不确定为什么const需要使用单个参数构造函数隐式转换为用户定义的类型,如下所示
#include <iostream>
class X {
public:
X( int value ) {
printf("constructor initialized with %i",value);
}
}
void implicit_conversion_func( const X& value ) {
//produces "constructor initialized with 99"
}
int main (int argc, char * const argv[]) {
implicit_conversion_func(99);
}
Run Code Online (Sandbox Code Playgroud)
从第4节第3行开始
当且仅当声明T t = e时,表达式e可以隐式转换为类型T. 对于一些发明的临时变量t(8.5),其形式良好.某些语言结构要求将表达式转换为布尔值.在这样的上下文中出现的表达式e被称为在上下文中转换为bool并且当且仅当声明bool t(e)时才是格式良好的; 对于一些发明的临时变量t(8.5),其形式良好.隐式转换的效果与执行声明和初始化相同,然后使用临时变量作为转换的结果.如果T是左值引用类型(8.3.2),则结果是左值,否则为右值.当且仅当初始化将其用作左值时,表达式e用作左值.
接下来,我在8.5行6中找到了与用户定义类型相关的初始化器部分
如果程序要求对const限定类型T的对象进行默认初始化,则T应为具有用户提供的默认构造函数的类类型.
最后,我以12.3第2行结束了有关用户定义的转换的信息
用户定义的转换仅在明确无误的情况下应用(10.2,12.3.2).
不用说,10.2和12.3.2没有回答我的问题.
const隐含转换的影响吗?const12.3第2行使转换"明确"?const以某种方式影响第4节中谈到的左值与右值?如果我有一个C#类隐式转换为double,如下所示:
public class Parameter
{
private double _value;
public Parameter(double value) { _value = value }
public static implicit operator double(Parameter p) { return _value; }
}
Run Code Online (Sandbox Code Playgroud)
F#不喜欢我试图使用它,就好像它是float:
let a = Parameter(4.0)
let b = Parameter(2.0)
let c = a * Math.Sin(b) <-- 'expected float, here Parameter'
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点(我猜不会,基于这个问题/答案),如果没有,什么是一个体面的解决方法?
我很难以我能够轻易理解的方式找到关于这个主题的信息,所以我要求对我发现的内容进行审查.这只是关于转换和转换的.
在示例中,我将指的是:
(signed/unsigned) int bigger;
(signed/unsigned) char smaller;
Run Code Online (Sandbox Code Playgroud)
截断整数.(超大化>小)
biggersmaller
如果较大的值太大而不适合较小的类型,则会导致未定义的行为(纠正我).但是我的规则应该是在所有机器上工作(对此也是正确的),结果应该是可预测的.
加宽整数(小 - >更大)
a)signed char- >signed int
b)signed char- >unsigned int
c)unsigned char- >signed int
d)unsigned char- >unsigned int
哪些未定义/未指定的行为,我没有提到可以弹出?
感谢Martin Odersky在Coursera上的最新产品,我已经开始玩了scala.util.Try.但是,我很惊讶地发现它并不一定能很好地与集合monad一起使用,因为它没有实现scala.collection.GetTraversableOnce.
这可能会派上用场.例如,您可以将字符串列表转换为整数,同时抛弃坏的字符串,如下所示:
def ints(strs:List[String]):List[Int] = strs.flatMap(s => Try(s.toInt))
Run Code Online (Sandbox Code Playgroud)
解决方法很简单.只需将其转换Try为an Option并让其隐式转换为我们工作:
def ints(strs:List[String]):List[Int] = strs.flatMap(s => Try(s.toInt).toOption)
Run Code Online (Sandbox Code Playgroud)
在我看来,Try它将实现GenTraversableOnce或具有自己的隐式转换.谁能解释为什么不呢?事实上,这实际上Try并不是一个单子吗?
在下面显示的代码中,没有任何内容被打印,这意味着for循环中的条件失败.可能是什么原因?
我想知道,因为当我TOTAL_ELEMENTS单独打印时,它给出了5,所以它必须是5-2=3 => -1<=3,所以它应该打印一些东西.
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = { 23, 34, 12, 17, 204, 99, 16 };
int main()
{
int d;
for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++) {
printf("%d\n", array[d + 1]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释这段代码吗?
这是C++程序:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int test_string(const string & str) {
return str.size();
}
void main() {
test_string(""); //can compile
vector<string> v;
string sum = accumulate(v.cbegin(), v.cend(), ""); //cannot compile
}
Run Code Online (Sandbox Code Playgroud)
我想使用隐式转换,从const char *以string仿制STL函数的调用accumulate.我知道转换const char *为字符串不是显式的,因此我们可以将const char *参数传递给需要string类型的调用.这可以通过上述test_string功能证明.但当我做同样的事情时accumulate,编译器抱怨:
Run Code Online (Sandbox Code Playgroud)error C2440: '=': cannot convert from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'const char *'
代码工作只有当我更换""使用string("").我不明白为什么隐式转换适用于我的自定义函数但不起作用accumulate.你能解释一下吗?非常感谢.
PS:我正在使用Visual Studio 2015.