我有库方法采用变量参数列表和生成数据
class Data
def process(elems: String*): Data = new Data
Run Code Online (Sandbox Code Playgroud)
我希望我的字符串被隐式转换为 Data
implicit def strToData(ts: String): Data = process(t)
Run Code Online (Sandbox Code Playgroud)
所以我可以写类似的东西
val data: Data = "one"
Run Code Online (Sandbox Code Playgroud)
但是我想要隐式转换字符串元组.我添加了另一个隐含的内容
implicit def strsToData(ts: String*): Data = process(ts: _*)
Run Code Online (Sandbox Code Playgroud)
编译很好,但转换失败
val data: Data = ("one", "two")
val dat3: Data = ("one", "two", "three")
val dat4: Data = ("one", "two", "three", "four")
Run Code Online (Sandbox Code Playgroud)
同
found : Seq[java.lang.String]
required: this.Data
val data: Data = Seq("one", "two")
Run Code Online (Sandbox Code Playgroud)
有没有办法隐式转换元组,或者为什么可以实现它?
更新:元组可以是任何arity.
我想让applycase类的自动伴侣类构造函数为我执行隐式转换,但无法弄清楚如何这样做.我一直在搜索,我能找到的最接近的答案就是这个问题(我将解释为什么它不是我正在寻找的).
我有一个类似于下面的案例类:
case class Container(a: Long, b: Long, c: Long)
Run Code Online (Sandbox Code Playgroud)
我正在使用容器来计算某些条件适用的实例,所以我希望能够让构造函数自动将布尔参数转换为longs(if (boolean) 1L else 0L).
当然,真实的案例类有很多参数,因此制作我自己的伴随对象和重载apply接受Boolean参数会很繁琐且非常重复.此外,类似下面的代码是不理想的(如果它以某种方式正确实现),因为它只接受布尔参数:
object Container {
def apply(args: Boolean*) = {
// doesn't REALLY work since number of arguments not enforced
Container(args map { if (_) 1L else 0L } toArray: _*)
}
}
val c1 = Container(1, 0, 1) // works
val c2 = Container(true, false, true) // might be workable if done correctly
val c3 …Run Code Online (Sandbox Code Playgroud) 我注意到你可以在C#中做这样的事情:
XNamespace c = "http://s.opencalais.com/1/pred/";
Run Code Online (Sandbox Code Playgroud)
请注意,字符串值被隐式转换为不同的类型.还有其他地方可以做到吗?围绕这类事情有哪些常见的模式和做法?
我有一个Collection,我正在尝试使用Distinct方法删除重复项.
public static Collection<MediaInfo> imagePlaylist
imagePlaylist = imagePlaylist.Distinct(new API.MediaInfoComparer());
Run Code Online (Sandbox Code Playgroud)
我得到错误"无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.ObjectModel.Collection'.存在显式转换(您是否错过了转换?)"
imagePlaylist曾经是一个List(我可以使用.ToList()),但为了符合"CA1002不公开通用列表"我想将List转换为Collection.
-谢谢
在我们公司,我们有一个编译策略-Wconversion,产生一些转换警告.虽然我同意这个额外的检查可以防止错误,但是在速记操作符上看到警告很烦人,例如:
uint8_t byte;
byte += 8; // conversion to 'uint8_t' from 'int' may alter its value [-Wconversion]
Run Code Online (Sandbox Code Playgroud)
现在可以通过重写来解决这个问题byte = (uint8_t)(byte+8),从而降低代码的可读性.
有没有更好的方法来做到这一点?
我正在使用Scala中一个简单的复数案例类,并希望创建一个在复数,双精度和整数之间工作的加法函数.以下是工作解决方案的简单示例:
case class Complex(re: Double, im: Double)
implicit def toComplex[A](n: A)(implicit f: A => Double): Complex = Complex(n, 0)
implicit class NumberWithAdd[A](n: A)(implicit f: A => Complex) {
def add(m: Complex) = Complex(n.re + m.re, n.im + m.im)
}
Run Code Online (Sandbox Code Playgroud)
注意我故意不在复杂的case类中包含add函数.使用上面我可以做到这一切:
scala> val z = Complex(1, 2); val w = Complex(2, 3)
z: Complex = Complex(1.0,2.0)
w: Complex = Complex(2.0,3.0)
scala> z add w
res5: Complex = Complex(3.0,5.0)
scala> z add 1
res6: Complex = Complex(2.0,2.0)
scala> 1 add z
res7: …Run Code Online (Sandbox Code Playgroud) 我基本上有以下类(在C#上创建的示例为泛型类创建隐式转换?).
class MyClass<T>
{
public MyClass(T val)
{
Value = val;
}
public T Value { get; set; }
public static implicit operator MyClass<T>(T someValue)
{
return new MyClass<T>(someValue);
}
public static implicit operator T(MyClass<T> myClassInstance)
{
return myClassInstance.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
一个人可以做到
MyClass<IFoo> foo1 = new Foo();
MyClass<Foo> foo2 = new Foo();
//But not
MyClass<IFoo> foo3 = (IFoo)new Foo();
Run Code Online (Sandbox Code Playgroud)
当尝试做类似的事情时,会出现真正的问题
void Bar(IFoo foo)
{
Bar2(foo);
//What should be the same as
Bar2<IFoo>(new MyClass<IFoo>(foo));
}
void Bar2<T>(MyClass<T> …Run Code Online (Sandbox Code Playgroud) 当我写这样的代码时:
struct foo {
operator int() const { return 1; }
};
int main() {
foo a, b;
auto tmp = (a < b);
}
Run Code Online (Sandbox Code Playgroud)
它工作,但我写这样的代码:
struct foo {
operator string() const { return string("foo"); }
};
int main() {
foo a, b;
auto tmp = (a < b);
}
Run Code Online (Sandbox Code Playgroud)
编译器(clang ++)说 error: invalid operands to binary expression ('foo' and 'foo')
我想知道为什么,因为string类型和int类型都有比较运算符,但是当foo有用户定义的int转换时,它会隐式转换int为比较,但是当foo只有用户定义的string转换时,编译器不会进行隐式转换,但(string)a<(string)b效果很好.
非常简化的示例(不必理会类A和运算符在做什么,仅举例来说):
#include <iostream>
using namespace std;
template <bool is_signed>
class A {
public:
// implicit conversion from int
A(int a) : a_{is_signed ? -a : a}
{}
int a_;
};
bool operator==(A<true> lhs, A<true> rhs) {
return lhs.a_ == rhs.a_;
}
bool operator==(A<false> lhs, A<false> rhs) {
return lhs.a_ == rhs.a_;
}
int main() {
A<true> a1{123};
A<false> a2{123};
cout << (a1 == 123) << endl;
cout << (a2 == 123) << endl;
return 0;
} …Run Code Online (Sandbox Code Playgroud) c++ templates implicit-conversion template-argument-deduction
我有一个包装器类,其行为应类似于指针。我已经过载operator T*和operator bool。布尔进行了一些额外的验证。我尝试在if中使用该对象,但我注意到该operator T*对象称为和bool。有人可以解释我为什么吗?是在标准中以某种方式指定的吗?我在MSVC,clang和gcc中测试了以下示例代码,它们都调用operator T*。另外,根据我在此页面上阅读的内容(https://en.cppreference.com/w/cpp/language/implicit_conversion),if应该尝试转换为bool。
#include <stdio.h>
class MyClass
{
public:
MyClass(int i)
: m(i)
{}
operator bool() const
{
printf("operator bool()\n");
return m;
}
operator int* ()
{
printf("operator int* ()\n");
return &m;
}
private:
int m;
};
int main()
{
MyClass a(5);
MyClass b(0);
if (a)
printf("a is true\n");
else
printf("a is false\n");
if (b)
printf("b is true\n");
else
printf("b is false\n");
return 0; …Run Code Online (Sandbox Code Playgroud) c++ class operator-overloading conversion-operator implicit-conversion