例如,我想让它以设置jDName为"John Doe"和jDAge32的方式工作:
case class Person(name : String, surname : String, age : Int)
val johnDoe = Person("John", "Doe", 32)
val jDName : String = johnDoe
val jDAge : Int = johnDoe
Run Code Online (Sandbox Code Playgroud)
我可以在Person类中编写函数来提供对String,Int和其他(自定义)类型的隐式转换吗?另一件事是显式的强制转换操作 - 也很有趣,但我不确切地知道如何在Scala中编写这个例子.
我正在编写一个程序,该程序应该处理c字符串(char *)和c ++字符串(std :: string)。我出于关注而孤立了以下示例。
#include <iostream>
#include <string>
void hello(std::string s) {
std::cout << "STRING FUNCTION" << std::endl;
}
void hello(char* c) {
std::cout << "CHAR FUNCTION" << std::endl;
}
int main(int argc, char* argv[]) {
hello("ambiguous");
hello((std::string)"string");
hello((char*)"charp");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译该程序时,我得到警告:
test.cpp:14: warning: deprecated conversion from string constant to ‘char*’
Run Code Online (Sandbox Code Playgroud)
关于首次致电hello。运行该程序可以得到:
CHAR FUNCTION
STRING FUNCTION
CHAR FUNCTION
Run Code Online (Sandbox Code Playgroud)
显示对的第一个调用hello与签名匹配hello(char* c)。
我的问题是,如果作为c ++程序,字符串文字("ambiguous")是std :: string,为什么将其强制转换为a char*然后匹配函数,hello(char* c)而不是停留在std :: …
我已经定义了自定义值类型MyCustomValueType,隐式转换运算符从long到MyCustomValueType.
public struct MyCustomValueType
{
private readonly long number;
public MyCustomValueType(long? number)
{
this.number = number.GetValueOrDefault(0);
}
public static implicit operator MyCustomValueType(long number)
{
return new MyCustomValueType(number);
}
}
Run Code Online (Sandbox Code Playgroud)
然后编译器允许我执行以下操作:
// ...
long? value = null;
MyCustomValueType myCustomValueType = (MyCustomValueType)value;
Console.WriteLine(myCustomValueType);
Run Code Online (Sandbox Code Playgroud)
在引擎盖下,编译器将使用强制转换的语句转换为:
MyCustomValueType myCustomValueType = ((long?)null).Value;
Run Code Online (Sandbox Code Playgroud)
我想知道(或更好地说WHY)是怎么发生的?为什么编译器甚至允许显式转换没有定义任何人.编译器适用的规则是什么?
我可能还应该提到,当MyCustomValueType仅定义用于强制转换的显式运算符时,也可以进行此类强制转换,例如:
public static explicit operator MyCustomValueType(long number)
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我以某种方式接受编译器的作用并理解它.隐式运算符的情况确实令人困惑.有人可以解释一下吗?
我的String类提供了一个运算符char*重载,允许您将字符串传递给C函数.
不幸的是,我的一位同事无意中发现了一个错误.
他实际上有以下代码.
StringT str;
// Some code.
delete str;
Run Code Online (Sandbox Code Playgroud)
反正是否有阻止删除将字符串对象强制转换为char*以防止将来出现这样的错误? std::string通过不提供char运算符重载来解决这个问题,但理想情况下,我想保持重载但阻止该删除工作.
为什么foo1失败而foo2成功?编译器不应该自动检查Blah的所有超类型吗?
trait Foo[A] {
def bar: A
}
trait Bleh;
case class Blah extends Bleh;
implicit object BlehFoo extends Foo[Bleh]
def foo1[A:Foo](a:A) = a
def foo2[A,B:Foo](a:A)(implicit aToB: A => B) = aToB(a)
// Shouldn't it automatically use Bleh?
foo1(Blah())
// Failure: could not find implicit value for evidence parameter of type Foo[Blah]
foo2(Blah())
// Success: Bleh = Blah()
Run Code Online (Sandbox Code Playgroud) 只是想了解C#.仅考虑以下简化示例.
void Main()
{
IList<IAnimal> animals = new List<IAnimal>
{
new Chicken(),
new Cow(),
};
// Shouldn't this line result in a compile-time error?
foreach (Chicken element in animals)
{
}
}
public interface IAnimal
{
}
public class Cow : IAnimal
{
}
public class Chicken : IAnimal
{
}
Run Code Online (Sandbox Code Playgroud)
虽然第一次迭代成功,但第二次迭代不成功.老实说,我预计这会在编译时失败.有谁理解为什么它只在运行时失败?