标签: implicit

在 Scala 2.13 中,为什么伴随对象的隐式作用域有时可能会错位?如何纠正?

下面是一个测试 scala 2.13 编译器隐式特性的简单示例:

object OverridingScope {

  trait System {

    trait Handler[T]
  }

  object Sys1 extends System {

    object Handler {

      implicit def handle1: Handler[Int] = new Handler[Int] {}
    }

    implicit def handle2: Handler[Long] = new Handler[Long] {}
  }
}
Run Code Online (Sandbox Code Playgroud)

根据这篇文章:

Scala 在哪里寻找隐式?

Handler当使用 type 时,trait 的伴生对象可以作为隐式作用域的一部分自动导入Handler[_],外部对象Sys1应该是无关紧要的。这意味着 handle1 应该是可见的,而 handle2 应该是隐藏的。

但是下面的测试用例显示了完全相反的情况:

class OverridingScope extends BaseSpec {

  import OverridingScope._

  it("implicits in companion is in scope") {

//    val hh = implicitly[Sys1.Handler[Int]]
    // Doesn't work
  } …
Run Code Online (Sandbox Code Playgroud)

scala implicit typeclass companion-object

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

为什么编译器不能链式转换?

T1, T2, T3三种类型。我们还定义了该类的两个给定实例Conversion,以便编译器可以从T1toT2和从T2to进行转换T3

下面的代码可以正常编译:

type T1
type T2
type T3

given Conversion[T1, T2] with
    override def apply(x: T1): T2 = ???

given Conversion[T2, T3] with
    override def apply(x: T2): T3 = ???

val t1: T1 = ???
val t2: T2 = t1
val t3: T3 = t2
Run Code Online (Sandbox Code Playgroud)

但是当我们尝试从T1到 时会发生什么T3?编译器不会让我们:

val t3: T3 = t1
             ^^
             Found:    (t1: T1)
             Required: T3
Run Code Online (Sandbox Code Playgroud)

我的问题:编译器无法本机(参见解决方法)链转换是否有特定原因?

我的解决方法 …

scala implicit type-conversion scala-3 given

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

为什么 std::make_unique<A>(*this) 可以调用 A 的隐式声明的复制构造函数

源问题来自Usage of this* in make_unique

代码如下,最佳答案是:

在 中clone()*this是对 的左值引用,因此您正在从 (对的左值引用) (在 内部)A构造 a ,因此您正在使用 的隐式声明的复制构造函数:AAstd::make_uniqueA

A(A const&);
Run Code Online (Sandbox Code Playgroud)

我很困惑类A有一个虚拟析构函数virtual ~A(){},编译器将不再生成复制构造函数。那么为什么std::make_unique<A>(*this)可以调用 的隐式声明的复制构造函数呢A

class Base {
    public:
        virtual ~Base() {}
        virtual std::unique_ptr<Base> clone() = 0;
        virtual void print() = 0;
};

class A: public Base {
        std::string name_;
    public:
        A(std::string name ){name_ = name;};
        std::unique_ptr<Base> clone() override{
            return std::make_unique<A>(*this);
        };
        void print( ) override{ …
Run Code Online (Sandbox Code Playgroud)

c++ constructor implicit

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

为什么这段 Java 代码隐式调用 toString() 方法?

为什么是输出?:球体 0

它以某种方式隐式调用 toString() 方法?这是如何运作的 ?

class BerylliumSphere {
    private static long counter = 0;
    private final long id = counter++;
    public String toString() { 
        return "Sphere " + id;
    }
}

public class Test {
    public static void main (String[] args) {
        BerylliumSphere spheres = new BerylliumSphere();
        System.out.println(spheres);
    }
}

// output: Sphere 0 
Run Code Online (Sandbox Code Playgroud)

java printing tostring implicit

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

运算符的显式和隐式(int)

public static explicit operator int(Author a)
    {
        return a.Publications.Length;
    }
    public static implicit operator int(Author a)
    {
        return a.Publications.Length;
    }
Run Code Online (Sandbox Code Playgroud)

为什么我不这样做?我的老师让我覆盖了Author类的operator int的隐式和显式转换.+我可以得到深拷贝的解释:D?

c# overriding explicit implicit

-5
推荐指数
1
解决办法
380
查看次数