假设我想要一个像Java这样的类Date.它唯一的数据成员是一个long,表示自1970年以来的毫秒数.
是/可能只是制作新的Scala类型的任何性能优势:
type PrimitiveDate = Long
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用隐式转换添加方法,就像对int一样RichInt.原始类型的这种"拳击"是否涉及任何开销(类创建)?基本上你可以有一个静态方法
def addMonth(date: PrimitiveDate, months: Int): PrimitiveDate = date + 2592000000 * months
Run Code Online (Sandbox Code Playgroud)
让类型系统弄清楚它必须d addMonth 5
在代码中出现时应用.
编辑
看来你通过编写创建的别名type PrimitiveDate = Long不是由scala编译器强制执行的.创建一个合适的类,包含Long,这是在Scala中创建强制类型的唯一方法吗?
您认为能够为基本类型创建强制类型别名有用吗?
拿这个代码:
class Register(var value:Int = 0) {
def getZeroFlag() : Boolean = (value & 0x80) != 0
}
object Register {
implicit def reg2int(r:Register):Int = r.value
implicit def bool2int(b:Boolean):Int = if (b) 1 else 0
}
Run Code Online (Sandbox Code Playgroud)
我想这样使用它:
val x = register.getZeroFlag + 10
Run Code Online (Sandbox Code Playgroud)
但我受到了欢迎:
type mismatch; found : Boolean required: Int
Run Code Online (Sandbox Code Playgroud)
怎么了?我是否需要定义一个隐式的函数返回一个bool?
scala> implicit def transitive[A, B, C](implicit f: A => B, g: B => C): A => C = f andThen g
transitive: [A, B, C](implicit f: A => B, implicit g: B => C)A => C
scala> class Foo; class Bar; class Baz { def lol = println("lol") }
defined class Foo
defined class Bar
defined class Baz
scala> implicit def foo2Bar(f: Foo) = new Bar
foo2Bar: (f: Foo)Bar
scala> implicit def bar2Baz(f: Bar) = new Baz
bar2Baz: (f: Bar)Baz …Run Code Online (Sandbox Code Playgroud) 在我的代码中的某个时刻,我想在A上调用C方法.这可能吗?我提出的一个修复是从A - > C转换,但这似乎有点多余.
注意:
谢谢 ...
关于以下编译器错误:
用户定义的运算符'Foo.implicit operator Foo(Bar)'必须声明为static和public
这是什么原因?为什么必须使用用户定义的转换运算符public?
提供以下代码,为什么这种转换不合法:
internal class Bar{}
internal class Foo
{
private Bar _myBar;
internal static implicit operator Bar(Foo foo)
{
return foo._myBar;
}
}
Run Code Online (Sandbox Code Playgroud)
我检查了C#语言规范,相关部分(10.10.3)没有提到这个要求.它是编译器而不是C#/ .Net限制吗?
为了使事情变得怪异,我可以作出上述转换操作符public,只要这两个Foo和Bar是internal,这是奇怪的,因为我认为你不能返回internal上一个公共方法的对象(虽然我想,如果整个类是internal它止跌没问题.但是,如果我创建public PublicFoo并保持Bar internal然后我得到inconsistent accessibility编译器错误:
internal class Bar { }
internal class Foo
{
private Bar _myBar;
/// <summary>
/// No inconsistent accessibility: return type
/// </summary>
public static implicit …Run Code Online (Sandbox Code Playgroud) Scala中2.11.2,下面的最小例如使用当仅编译类型归属于Array[String]:
object Foo {
def fromList(list: List[String]): Foo = new Foo(list.toArray : Array[String])
}
class Foo(source: IndexedSeq[String])
Run Code Online (Sandbox Code Playgroud)
如果我删除类型ascription in fromList,它将无法编译,并出现以下错误:
Error:(48, 56) polymorphic expression cannot be instantiated to expected type;
found : [B >: String]Array[B]
required: IndexedSeq[String]
def fromList(list: List[String]): Foo = new Foo(list.toArray)
^
Run Code Online (Sandbox Code Playgroud)
为什么编译器不能在Array[String]这里推断?或者这个问题是否必须对从Arrays到IndexedSeqs 的隐式转换做些什么呢?
arrays scala type-inference implicit-conversion scala-collections
使用以下代码:
Float a = 1.2;
Run Code Online (Sandbox Code Playgroud)
有一个错误,因为它将十进制作为double值,并且double是一个更大的数据类型float.
现在,它将整数作为默认int类型.那么,为什么以下代码没有给出任何错误?
Byte b = 20;
Run Code Online (Sandbox Code Playgroud) 使用g ++ 3.4和4.7,我观察到以下奇怪的行为:
如果需要用户定义的转换,则函数模板不匹配,其中普通函数将是.我在C++ 98标准中找不到相应的规则.g ++是否正确,(我假设)?或者这是一个错误?
template <class T>
int x(auto_ptr_ref<T> p)
{
return 1;
}
// this would match
/*
int x(auto_ptr_ref<int> p)
{
return 2;
}
*/
void dummy()
{
cout << x(auto_ptr<int>()) << endl;
}
Run Code Online (Sandbox Code Playgroud) c++ templates function overload-resolution implicit-conversion
我正面临MISRA C 2012违规,我无法理解.以下是代码:
#define I2C_CCRH_FS ((uint8_t)0x80)
#define I2C_CCRH_DUTY ((uint8_t)0x40)
#define I2C_CCRH_CCR ((uint8_t)0x0F)
typedef struct I2C_struct
{
volatile uint8_t CR1;
volatile uint8_t CR2;
volatile uint8_t CCRL;
volatile uint8_t CCRH;
} I2C_TypeDef;
#define I2C_BaseAddress 0x5210
#define I2C ((I2C_TypeDef *) I2C_BaseAddress)
I2C->CCRH &= ~(uint8_t)((I2C_CCRH_FS | I2C_CCRH_DUTY) | I2C_CCRH_CCR);
Run Code Online (Sandbox Code Playgroud)
在之前的代码中,PC-Lint抱怨说:
Unpermitted operand to operator '|' [MISRA 2012 Rule 10.1, required]
Mismatched essential type categories for binary operand [MISRA 2012 Rule 10.4, required]
Run Code Online (Sandbox Code Playgroud)
规则10.1规定ORing应该没有问题unsigned int.(PC-Lint通过第一次OR操作并抱怨第二次!)
规则10.4规定操作的操作数应具有相同的基本类型.
虽然所有操作数都被声明为,但我无法理解为什么存在这些违规行为uint8_t?
我试过在每两个ORed常量周围加上括号.我也尝试过将它们全部uint8_t …
在N4659中16.3.3.1隐式转换序列说
10如果存在几个不同的转换序列,每个转换序列都将自变量转换为参数类型,则与参数关联的隐式转换序列被定义为唯一的转换序列,称为歧义转换序列。为了对16.3.3.2中所述的隐式转换序列进行排名,歧义转换序列被视为与其他任何用户定义的转换序列都没有区别的用户定义的转换序列[注意:此规则可防止函数变为非-viable,因为其参数之一的转换顺序不明确。]如果选择使用歧义转换顺序的函数作为最佳可行函数,则该调用将格式错误,因为该调用中参数之一的转换是模棱两可的。
(当前草案的相应部分为12.3.3.1)
该规则的预期目的是什么,以及引入的歧义转换序列的概念是什么?
文本中提供的注释指出,此规则的目的是“防止功能由于其参数之一的转换顺序不明确而变得不可行 ”。嗯...这实际上是指什么?可行功能的概念在文档的前面部分中定义。它根本不取决于转换的歧义性(每个参数的转换必须存在,但不必明确)。而且似乎没有提供可行的功能以某种方式在以后“变得不可行”的规定(既不是出于某种模棱两可,也不是因为其他原因)。列举了可行的功能,它们相互竞争,成为“最佳”如果有一个“获胜者”,则解决方案成功。在此过程中的任何时候,可行的功能都可能(或需要)变成不可行的功能。
上述段落中提供的示例不是很有启发性(即尚不清楚上述规则在该示例中扮演什么角色)。
这个简单的例子最初引发了这个问题
struct S
{
operator int() const { return 0; };
operator long() const { return 0; };
};
void foo(int) {}
int main()
{
S s;
foo(s);
}
Run Code Online (Sandbox Code Playgroud)
让我们在这里机械地应用上述规则。foo是可行的功能。从参数类型S到参数类型有两个隐式转换序列int:S -> int和S -> long -> int。这意味着,根据上述规则,我们必须将它们“打包”成单个模糊的转换序列。然后我们得出结论,这foo是最好的可行功能。然后我们发现它使用了我们不明确的转换序列。因此,根据上述规则,代码格式错误。
这似乎没有任何意义。这里的自然期望是,S …
c++ conversion-operator language-lawyer overload-resolution implicit-conversion