标签: implicit-conversion

Linq - 无法隐式转换类型'System.Collections.Generic.IEnumerable

我收到以下错误:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'Munchkin.Model.PlayerProfiles.Profile'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)

我的代码是:

Profile currentProfile;

public Profile ActiveProfile()
{
    currentProfile = new Profile();        
    return currentProfile = 
        (from profiles in xmlDoc.Element("PlayerPofiles").Element("Online").Elements("Player")
        where (string)profiles.Element("Active") == "True"
        select new Profile
        {
            Name = (string)profiles.Element("Name"),
            Sex = (string)profiles.Element("Sex"),
            Avatar = (string)profiles.Element("Avatar").Attribute("path") ?? "",
            Created = (DateTime)profiles.Element("Created"),
            Birthday = (string)profiles.Element("Birthday"),
            Wins = (string)profiles.Element("Ratio").Element("Win"),
            Losses = (string)profiles.Element("Ratio").Element("Loss"),
            Abandoned = (string)profiles.Element("Ratio").Element("Abandoned")
        });
}
Run Code Online (Sandbox Code Playgroud)

c# linq error-handling implicit-conversion

1
推荐指数
1
解决办法
6705
查看次数

为什么这些重载函数调用不明确?

为什么以下重载函数调用不明确?编译错误:

调用重载'test(long int)'是模棱两可的,候选者是:void test(A)| 无效测试(B)|

代码:

class A
{
    public:
        A(int){}
        A(){}
};

class B: public A
{
    public:
        B(long){}
        B(){}
};

void test(A a)
{
}

void test(B b)
{
}

void main()
{
    test(0L);
    return;
}
Run Code Online (Sandbox Code Playgroud)

c++ overload-resolution implicit-conversion

1
推荐指数
1
解决办法
808
查看次数

C++拷贝初始化+隐式构造函数call = fail

这段代码:

class foo
{
    int x;
public:
    foo(int x) : x(x) { }
    int get() const { return x; }
    //...
};

class bar
{
    int x;
public:
    bar(const foo& x) : x(x.get()) { }
    int get() const { return x; }
    bar& operator =(const foo& rhs) { x = rhs.get(); return *this; }
    //...
};

void func()
{
    foo f = 3;
    bar b = 3;
    b = 7;
    //...
}
Run Code Online (Sandbox Code Playgroud)

在线上出错bar b = 3(g ++ 4.7.1 …

c++ constructor implicit-conversion copy-initialization c++11

1
推荐指数
1
解决办法
138
查看次数

隐式值的隐式转换是否可以满足隐式参数?

我正在定义一些Scala implicits,以便更轻松地使用特定的不可更改的Java类集.下面的Scala代码是一个简单的例子,显然看起来很疯狂,在现实世界中,我试图从Monkey,Tree&Duck中隐含地获取特定资源(而不是数字时代),以便在各种方法中使用purchaseCandles():

// actually 3 Java classes I can not change:
case class Monkey(bananas: Int) 
case class Tree(rings: Int)
case class Duck(quacks: Seq[String])

// implicits I created to make my life easier...
implicit def monkey2Age(monkey: Monkey): Int = monkey.bananas / 1000
implicit def tree2Age(tree: Tree): Int = tree.rings
implicit def duck2Age(duck: Duck): Int = duck.quacks.size / 100000

// one of several helper methods that I would like to define only once,
// only useful if they can use …
Run Code Online (Sandbox Code Playgroud)

scala implicit-conversion implicit-parameters

1
推荐指数
1
解决办法
272
查看次数

SQL Server中隐式转换和显式转换的区别

你能告诉我SQL Server中的隐式转换和显式转换之间的区别吗?

我用谷歌搜索了这个,但我无法得到它.

sql sql-server implicit-conversion

1
推荐指数
1
解决办法
4万
查看次数

我可以在C#中实现从字符串到布尔值的隐式"转换"吗?

有什么办法可以实现一个隐含的从转换stringbool使用C#?

例如,我有str值为Y的字符串,当我尝试转换(强制转换)时boolean,必须返回true.

.net c# extension-methods type-conversion implicit-conversion

1
推荐指数
2
解决办法
611
查看次数

如何(正确)丰富标准库?

我想定义一个从Iterator[T]我定义的类的隐式转换:ProactiveIterator[A].

问题不在于如何做到,而是如何正确地做到这一点,即在何处放置方法,以使其尽可能透明和不引人注目.理想情况下它应该是从隐式转换StringStringOpsin scala.Predef如果转换是从库中的类转换到其他类,那么它可以在该类中定义,但是AFAIK在这里是不可能的.

到目前为止,我已经考虑添加一个包含这些转换对象,类似于JavaConversions,但是更好的选项可能是可能的.

methodology scala implicit-conversion

1
推荐指数
1
解决办法
83
查看次数

具有多个参数的隐式转换以及运算符重载

我目前正在阅读"The C++ Programming Language"一书.以下是相关代码

class complex {
public:
  complex(double r, double i): re{r}, im{i} {}
  complex(double r): complex{r,0} {}
  complex(): complex{0,0} {}

  complex& operator-=(complex z) {
    this->re -= z.re;
    this->im -= z.im;
    return *this;
  } 
private:
  double re,im;
};

inline complex operator-(complex a, complex b) {
  return a -= b;
}

inline complex operator-(complex z) {
  return{ 0, 0 } - z;
}
Run Code Online (Sandbox Code Playgroud)

一元operator-给出错误 -

语法错误:缺少';' 在' - '之前

但是,编译器认为以下两种变体都是正确的

inline complex operator-(complex z) {
  return 0 - …
Run Code Online (Sandbox Code Playgroud)

c++ implicit-conversion c++11

1
推荐指数
1
解决办法
72
查看次数

删除/停用Ruby/Rails隐式转换

有没有办法在Ruby/Rails中删除/停用/猴子补丁隐式转换?

我厌倦了像这样的代码生成的错误:

t = Time.now
t + 3600 == t + 3600.seconds
Run Code Online (Sandbox Code Playgroud)

dt = DateTime.now 
dt + 3600 == dt + 3600.days #(why it's days here and not seconds as with Time ?)
Run Code Online (Sandbox Code Playgroud)

根据添加(或减法)中的日期类型,结果是不同的,因为在时间的情况下,Integer被隐式转换为几秒,而在DateTime的情况下则是天.

编辑:

好.我在这里有一些很棒的答案.
也许更好的方法来"纠正"这种非常不一致的Ruby行为,如果有人试图将Integer/Fixnum添加到日期/时间,则会引发异常.只接受持续时间,你不这么认为吗?

有没有办法做到这一点 ?

ruby ruby-on-rails implicit-conversion

1
推荐指数
1
解决办法
79
查看次数

Null类型的隐式转换

我正在尝试在Scala中重现C#语法:

C#代码:

val myValue = myObject ?? new MyClass();
Run Code Online (Sandbox Code Playgroud)

基本上'myValue'将包含'myObject'(如果它不为null),或者一个新的'MyClass'实例(如果它为null).

这是我试图运行的Scala代码:

case class ShortCond(original: Any){
  def ??(fallback : Any) {
    if (original == null)
      return fallback
    return original
  }
}
implicit def any2ShortCond(original: Any) = ShortCond(original)


val myString = null
val value = myString ?? "myString is null"
Run Code Online (Sandbox Code Playgroud)

它在myString不为null时有效,但在myString为null时失败,它返回:

error: value ?? is not a member of Null
val value = myString ?? "myString is null"
Run Code Online (Sandbox Code Playgroud)

我理解错误,但有没有办法在Scala中实现这一点,或者它是不可能的?

谢谢

scala implicit-conversion

1
推荐指数
1
解决办法
183
查看次数