TLDR:我有两个模板化类Outer和Inner.Inner<X>可以隐式构造X,Outer<Y>也可以隐式构造Y.应该Outer<Inner<X>> = X()工作?
更多细节:
假设我有以下两个类:
template<typename T>
class Inner {
public:
Inner(const T& value) {}
Inner(T&& value) {}
};
template<typename T>
class Outer {
public:
Outer(const T& value) {}
Outer(T&& value) {}
};
Run Code Online (Sandbox Code Playgroud)
考虑以下功能:
struct SomeType{};
Outer<Inner<SomeType>> DoSomethingFails() {
SomeType value;
return value;
}
Run Code Online (Sandbox Code Playgroud)
g ++抱怨:
no viable conversion from 'SomeType' to 'Outer<Inner<SomeType> >'
note: candidate constructor not viable: no known conversion from 'SomeType' to …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建模仿无形类型的自定义类型类.它看起来像这样:
trait View[Record] {
type Result <: HList
def apply(r: Record): Result
}
object View extends LowPriorityLiftFunction1{
type Aux[Record, L <: HList] = View[Record] {type Result = L}
implicit def atView[Record: View] = at[Record](implicitly[View[Record]].apply)
}
Run Code Online (Sandbox Code Playgroud)
假设我提供它的功能如下:
object toHView extends ->( (_:Int) + 1)
implicit def provideView[Record, L <: HList]
(implicit generic: Generic.Aux[Record, L],
mapper: Mapper[toHView.type, L])
: View.Aux[Record, mapper.Out] =
new View[Record] {
type Result = mapper.Out
def apply(r: Record) = mapper(generic.to(r))
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我们定义:
case class Viewable(x: Int, y: Int, …Run Code Online (Sandbox Code Playgroud) 以下函数定义有什么问题。
def f[A: Ordered] (a: A, b: A): Boolean = a < b
Run Code Online (Sandbox Code Playgroud)
我得到无法解析符号 <。由于 Ordered 是 A 的上下文类,因此它不应该能够解析 < 吗?我错过了什么?
我一直在讨论从3种方法中重构公共代码的问题,makeRequest()但是我从编译器得到了模糊的隐式匹配.我不确定这是否来自隐式方法或其他问题的默认值,但我的目标是getRequest/deleteRequest/postRequest可以简单地调用makeRequest("GET")/ makeRequest("DELETE")/ makeRequest("POST") ).以前没有任何参数是隐含的,我只是试图通过使用implicits来达到目标
def makeRequest(method: String)(implicit path: String, base: String, params: Seq[(String, String)], body: Option[String], retriesLeft: Int): Future[WSResponse] = ???
def getRequest()(implicit path: String, base: String = baseUrl, params: Seq[(String, String)] = Seq(), body: Option[String] = None, retriesLeft: Int = retries): Future[WSResponse] = makeRequest("GET")
def deleteRequest()(implicit path: String, base: String = baseUrl, params: Seq[(String, String)] = Seq(), body: Option[String] = None, retriesLeft: Int = retries): Future[WSResponse] = makeRequest("GET")
def postRequest[T]()(path: String, body: T, base: String = baseUrl, …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class Item<TItem>
where TItem : Item<TItem>
{
void GetReference()
{
TItem item = this;
}
}
Run Code Online (Sandbox Code Playgroud)
这里TItem item = this;生成编译器错误"无法Item<TItem>隐式转换为TItem".
但为什么我们需要转换呢?我们已经定义了约束where TItem : Item<TItem>,因此可以认为根本不需要转换,因为这两种类型是相同的,不是吗?
顺便说一下,显式转换是可用的.这也在编译器错误中说明.
我正在学习Scala,并且尝试创建类型类来解决“每种动物都吃食物,但是食物的类型取决于动物”的问题。我有一个Eats带有上下文范围的类型类:
trait Eats[A <: Animal, B <: Edible]
object Eats {
def apply[A, B]: Eats[A, B] = new Eats[A, B] {}
}
Run Code Online (Sandbox Code Playgroud)
既Animal和Edible为抽象类。(简化的)Animal界面看起来像这样
abstract class Animal {
type This // concrete type
def eat[A <: Edible](food: A)(implicit e: Eats[This, B]) = // ...
}
Run Code Online (Sandbox Code Playgroud)
我的目标是animal.eat(food)仅在给定类型的动物和食物存在实例(范围内的隐含值)的情况下才允许调用。为此,我创建了一个EatingBehaviour对象,该对象基本上包含所有关系的实例。例如 宣布奶牛吃草,我添加一行
implicit val cowEatsGrass = Eats[Cow, Grass]
Run Code Online (Sandbox Code Playgroud)
类似于您instance Eats Cow Grass在Haskell中的编写方式。但是,现在我需要为ThisAnimal类的所有子类型指定抽象类型,以便Animal接口中的签名起作用:
class Cow extends Animal { type …Run Code Online (Sandbox Code Playgroud) 我正在查看页面Dotty下的文档Contextual Abstractions,我看到了Given Instances.
给定实例(或简单地说,“给定”)定义了某些类型的“规范”值,用于将参数合成给给定的子句。例子:
trait Ord[T] {
def compare(x: T, y: T): Int
def (x: T) < (y: T) = compare(x, y) < 0
def (x: T) > (y: T) = compare(x, y) > 0
}
given intOrd: Ord[Int] {
def compare(x: Int, y: Int) =
if (x < y) -1 else if (x > y) +1 else 0
}
given listOrd[T]: (ord: Ord[T]) => Ord[List[T]] {
def compare(xs: List[T], ys: List[T]): Int = …Run Code Online (Sandbox Code Playgroud) 我正在阅读 Scala 3 文档。他们引入了given关键字,被认为是 Scala 2 的替代品implicit。代码在这里
trait Ord[T] {
def compare(x: T, y: T): Int
def (x: T) < (y: T) = compare(x, y) < 0
def (x: T) > (y: T) = compare(x, y) > 0
}
given intOrd: Ord[Int] {
def compare(x: Int, y: Int) =
if (x < y) -1 else if (x > y) +1 else 0
}
given listOrd[T]: (ord: Ord[T]) => Ord[List[T]] {
def compare(xs: List[T], ys: List[T]): Int …Run Code Online (Sandbox Code Playgroud) 一个非常简单的用例,假设我有一个Foo接受 2 个参数的类,1 个是普通参数,1 个是隐式参数。
class Foo(val msg: String, implicit val n: Int) {
def multiplier = msg * n
}
implicit val num: Int = 4
val foo = new Foo("yo")
println(foo.msg)
Run Code Online (Sandbox Code Playgroud)
我知道如果我将隐式参数移动到另一个列表(即 curried ),它会起作用class Foo(val msg: String)(implicit val n: Int)。但是,出于某种原因,我不想这样做。
有人可以解释为什么当前版本的实现不起作用吗?
我确实遇到了带有隐式方法的 Scala 编译器问题。情节很简单。隐式方法的任务是将案例类 A 的对象转换为案例类 B 的对象。隐式方法实现访问一个不存在的案例类成员。如果 case 类成员在 case 类 A 或 B(例如 foobar)中根本不存在,则编译器会抛出错误。如果案例类成员确实存在于案例类 B 中,则编译器不会抛出错误,即使我使用此名称(即成员 x)访问案例类 A。
我在 2.13.1 版中使用 Scala。目前,2.13.2 是最新版本。
以下代码更详细地显示了该场景。以下代码将导致编译器错误。
package Hokuspokus
object ImplicitMagic extends App {
case class A(a: String, b: String, c: String)
case class B(d: String, e: String, f: String, x: String)
implicit def AtoB: A => B = a => B(a.a, a.b, a.c, a.foobar)
def print(b: B): Unit = {
System.out.println("Print" + b.d)
}
val a = A("foo", "bar", "asdf")
print(a) …Run Code Online (Sandbox Code Playgroud) implicit ×10
scala ×8
dotty ×2
scala-3 ×2
typeclass ×2
types ×2
c# ×1
c++ ×1
constraints ×1
constructor ×1
generics ×1
hlist ×1
sbt ×1
shapeless ×1
type-members ×1