标签: implicit-conversion

你如何以现代方式定义隐式转换?

愚蠢的问题,但这些例子都不适合我; 经典文章" 皮条客我的图书馆 "是错误的,甚至最简单的代码也有问题.

顺便说一句.我假设您必须在对象中放置转换方法(很多片段都省略了该部分).根据PiS的书,似乎挂隐式def是可以的,但这也给了我错误.

object Minutes
{
  implicit def toMinutes(x : Int) = new Minutes(x)
}

class Minutes(private val x : Int)
{
  def minutes = x.toString+"m"
}

object MainApp {

  def main(args : Array[String])
  {
     println(5.minutes)
     ...
Run Code Online (Sandbox Code Playgroud)

错误 - "值分钟不是Int的成员".

我错过了什么?Scala 2.9.1.

scala implicit implicit-conversion

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

Scala中隐含的碰撞

以下Scala代码正常工作:

val str1 = "hallo"
val str2 = "huhu"
val zipped: IndexedSeq[(Char, Char)] = str1.zip(str2)
Run Code Online (Sandbox Code Playgroud)

但是,如果我导入隐式方法

implicit def stringToNode(str: String): xml.Node = new xml.Text(str)
Run Code Online (Sandbox Code Playgroud)

然后Scala(2.10)编译器显示错误: value zip is not a member of String

似乎存在stringToNode某种方式块的隐式转换str1str2WrappedString.为什么?有没有一种方法来修改stringToNode这样的zip工作,但是stringToNode当我调用一个需要带Node参数的函数时仍然使用String

scala implicit implicit-conversion

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

尝试在List上定义新的隐式方法时出错

我试图使用以下代码添加newMethodList类上调用的方法.

implicit class ListCompanionOps(f: List.type) extends AnyVal {
  def newMethod(p: String) = {
    println(p)
  }
}
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨以下错误:

$ scalac test.scala
test.scala:3: error: ListCompanionOps is already defined as (compiler-generated) method ListCompanionOps
implicit class ListCompanionOps(f: List.type) extends AnyVal {
               ^
one error found
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢

scala implicit-conversion

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

gcc是否将uint8_t转换为int以获取单个值?

如果我编译以下程序:

#include <vector>
#include <cstdint>
#include <stdio.h>

int main() {
    constexpr std::size_t N = 10;
    uint8_t int8Value = 42;

    std::vector<int> IntVector(N, 0);
    for (int & ele:IntVector) {
        ele += int8Value;
    }

    std::vector<uint8_t> Int8Vector(N, 0);
    for (uint8_t & ele:Int8Vector) {
        ele += int8Value;
    }

    for (std::size_t i = 0; i < N; i++) {
        printf("%i %i\n",IntVector[i],Int8Vector[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

g++ test.cpp -o test -std=c++11 -Wconversiongcc 4.9它吐出来的是如下因素警告:

test.cpp: In function ‘int main()’:
test.cpp:16:7: warning: conversion to ‘uint8_t {aka unsigned …
Run Code Online (Sandbox Code Playgroud)

c++ gcc type-conversion implicit-conversion c++11

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

.NET中不明确的隐式用户定义转换

我最近在.NET中编写了几个结构,然后我添加了隐式转换运算符

例:

public struct Alpha
{
    internal string value;

    public static implicit operator Alpha(Beta b)
    {
        return new Alpha() { value = b.value };
    }

    public static implicit operator Beta(Alpha a)
    {
        return new Beta() { value = a.value };
    }
}

public struct Beta
{
    internal string value;

    public static implicit operator Alpha(Beta b)
    {
        return new Alpha() { value = b.value };
    }

    public static implicit operator Beta(Alpha a)
    {
        return new Beta() { value = a.value }; …
Run Code Online (Sandbox Code Playgroud)

.net c# implicit-conversion

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

scala隐式转换和箭头

我正在努力完成我的DSL构建尝试

我的方法应该采取多对(A,B),理想情况下我正在寻找这种代码:

myMethod(
  a1 -> b1,
  a2 -> b2,
  a3 -> b3)
Run Code Online (Sandbox Code Playgroud)

我的A和B类经常使用单个字符串参数构造,所以我添加了隐式转换器,如下所示:

implict def stringToA(s: String): A = new A(s)
implict def stringToB(s: String): B = new B(s)
Run Code Online (Sandbox Code Playgroud)

但箭头操作符(对于构造)不会获取隐式转换器:

object Test {
  case class A(s: String)
  case class B(s: String)

  def myMethod(pairs: (A, B)*): Unit = Unit

  implicit def stringToA(s: String): A = A(s)
  implicit def stringToB(s: String): B = B(s)

  myMethod(new Tuple2("a", "b")) // works just fine
  myMethod("a" -> "b") // Error: Type mismatch, expected: (Test.A, Test.B), actual: …
Run Code Online (Sandbox Code Playgroud)

scala implicit-conversion

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

C++隐式转换不起作用

一直在用g ++ 5.3.1尝试C++ 11 ......我以为我理解了隐式转换运算符.一般来说,如果我定义Class3::operator Class2()那么我可以Class3 直接传递给任何需要的函数Class2.这似乎在99%的时间都有效......但我遇到了一个简单的例子,其中并非如此.

class Class1 {};

class Class2 {
public:
  inline friend Class1 & operator<<(Class1 & a, Class2 const& c)
  {return a;}
};

class Class3 {
public:
  inline operator Class2() const {return Class2();}
};

void Foo(Class2 c) {}

int main()
{
  Class1 c1;
  Class3 c3;

  // g++ does not like this:
  // error: no match for 'operator<<' (operand types are 'Class1' and 'Class3')
  // c1 << c3;

  // g++ likes …
Run Code Online (Sandbox Code Playgroud)

c++ casting implicit-conversion c++11

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

C++双指针数组浮点转换

在c ++中将double转换为float的正确方法是什么.转换是隐含的吗?

问题1:考虑double d = 5.0;float f;

哪一个是正确的?

  • f = d;
  • f = (float)d;
  • f = static_cast<float>(d);

问题2:现在考虑一下

char *buffer = readAllBuffer(); 
double *d = (double*)(buffer + offset);
float f;
Run Code Online (Sandbox Code Playgroud)

哪一个现在正确?

  • f = d[0];
  • f = (float)d[0];
  • f = static_cast<float>(d[0]);

提前致谢!

c++ type-conversion implicit-conversion c++11

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

隐式转换不能分配给接口

我有一个隐式转换的类string定义为:

class TestClass : ITestInterface
{
    public static implicit operator TestClass(string value)
    {
        return new TestClass(value);
    }

    public TestClass(string value)
    {
        Value = value;
    }
    public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

它实现了一个标记界面:

public interface ITestInterface { }
Run Code Online (Sandbox Code Playgroud)

我有另一个类的方法定义为:

public void DoSomething(ITestInterface thing) { }
Run Code Online (Sandbox Code Playgroud)

我试图调用该方法时遇到错误:

public void Test()
{
    TestClass a = "This works fine.";
    DoSomething("Why doesn't this work?");
}
Run Code Online (Sandbox Code Playgroud)

无法从'string'转换为'Tests.ITestInterface'

所有代码都大大简化了; 我的实际需求要复杂得多,但这似乎是阻碍我想实现的模式的核心.

什么阻止这种工作?(C#规范中的东西?)
我可以对我的代码进行任何更改以允许这种类型的转换工作吗?

c# implicit-conversion

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

Scala:为什么类型转换隐式破坏代码

我有一个简单的代码

private def convertFieldsNames (fieldsNames: Array[String]): Array[String] =
  fieldsNames.map(convertFieldName)

private def convertFieldName (fieldName: String): String = s"!!! $fieldName"

val res = convertFieldsNames(Array("123", "456"))
res.map(println)
Run Code Online (Sandbox Code Playgroud)

它工作正常,但是当我添加类型转换功能时,我将在其他功能中使用

implicit def fromStringToEitherStringOrArray (str: String): Either[String, Array[String]] = Left(str)
implicit def fromArrayToEitherStringOrArray (arr: Array[String]): Either[String, Array[String]] = Right(arr)
Run Code Online (Sandbox Code Playgroud)

我一行出现错误

fieldsNames.map(convertFieldName)

type mismatch;
 found   : String => String
 required: Array[String] => ?
Run Code Online (Sandbox Code Playgroud)

我希望这些转换仅在需要转换为Either值时才会生效,所以我无法理解为什么此错误在根本没有Either类型的行中冒出

scala implicit implicit-conversion

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

标签 统计

implicit-conversion ×10

scala ×5

c++ ×3

c++11 ×3

implicit ×3

c# ×2

type-conversion ×2

.net ×1

casting ×1

gcc ×1