假设我有两组类,第一组继承自Foo,第二组继承自Bar.
class Foo
class Baz extends Foo
class Bar
class Qux extends Bar
Run Code Online (Sandbox Code Playgroud)
我想创建一个通用的隐式转换函数,将任何Foo转换为Bar,因为范围内有隐式转换器类型.
trait Converter[A <: Foo, B <: Bar] {
def convert(a : A) : B
}
implicit object BazToQuxConverter extends Converter[Baz, Qux] {
def convert(a : Baz) : Qux = new Qux
}
implicit def FooToBar[A <: Foo, B <: Bar](a : A)(implicit converter : Converter[A, B]) : B = converter.convert(a)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎不像我期望的那样有效.当我将以下行插入REPL时:
val a : Baz = new Baz
val b : Qux = a
Run Code Online (Sandbox Code Playgroud)
...我收到以下错误:
<console>:17: …Run Code Online (Sandbox Code Playgroud) 我一直在阅读有关Scala类型级编程的一些内容.主要是Apocalisp博客,也是Alexander Lehmann的youtube演讲.
我有点卡在我认为可能非常基本的东西上,这是使用隐式比较两种类型,如下所示:
implicitly[Int =:= Int]
Run Code Online (Sandbox Code Playgroud)
Apocalisp博客上的Mark说:
这对于捕获范围内的隐式值并且具有类型T非常有用.
我知道如何使这项工作,但我不知道为什么它的工作,所以不想继续前进.
在上面的情况下,在范围内有一个隐式的"Int"类型,"隐式"从以太中获取,允许代码编译?这与'function1'的返回类型如何相符?
res0: =:=[Int,Int] = <function1>
Run Code Online (Sandbox Code Playgroud)
此外,这隐含的来自哪里?在我的特质'Foo'的情况下,为什么呢
implicitly[Foo =:= Foo]
Run Code Online (Sandbox Code Playgroud)
编译?在这种情况下,'Foo'隐含在哪里?
如果这是一个非常愚蠢的问题,请提前道歉,并感谢您的帮助!
有没有办法为两个不同的类定义相同的隐式排序?
我尝试沿着以下几行做一些事情,但它没有检测到排序.
abstract class Common
case class A extends Common
case class B extends Common
implicit val KeyOrdering = new Ordering[Common] {
override def compare(x: Common, y: Common): Int = {
x.toString.compareTo(y.toString)
}
}
Run Code Online (Sandbox Code Playgroud) Ctrl + Q 找到隐式转换
Shift + Cmd + P 找到隐式参数实例的位置
如何找到隐含值/ def的使用位置?
有一个同事问我这个问题,在我脑子里迷茫的状态我没有答案:
你为什么这样做:
string ham = "ham " + 4;
Run Code Online (Sandbox Code Playgroud)
但不是:
string ham = 4;
Run Code Online (Sandbox Code Playgroud)
如果在连接时存在字符串转换的隐式转换/操作,为什么在将其指定为字符串时不一样?(当然,没有做一些运算符重载)
我正在尝试在scala中使用非常轻量级的组合子演算编码.最初,我只是实现S和K组合器,应用程序和常量值.后来我希望提升scala函数并允许将表达式作为scala函数进行求值.但是,这是为了以后.这是我到目前为止所拥有的.
/** Combinator expression */
sealed abstract class CE
/** Application: CE| (x y) <=> LC| (x:(A=>B) y:A) : B */
case class Ap[A <: CE, B <: CE, X](e1: A, e2: B) extends CE
/** A raw value with type */
case class Value[T](v: T) extends CE
/** Some combinator */
sealed abstract class Comb extends CE
/** The S combinator: CE| S x y z
* LC| ?x:(A=>B=>C).?y:(A=>B).?z:A.(x z (y z)) : C
* S : ?A.?B.?C. (A …Run Code Online (Sandbox Code Playgroud) 我想知道为什么List(3,2,1).toIndexedSeq.sortBy(x=>x)不起作用:
scala> List(3,2,1).toIndexedSeq.sortBy(x=>x) // Wrong
<console>:8: error: missing parameter type
List(3,2,1).toIndexedSeq.sortBy(x=>x)
^
<console>:8: error: diverging implicit expansion for type scala.math.Ordering[B]
starting with method Tuple9 in object Ordering
List(3,2,1).toIndexedSeq.sortBy(x=>x)
^
scala> Vector(3,2,1).sortBy(x=>x) // OK
res: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)
scala> Vector(3,2,1).asInstanceOf[IndexedSeq[Int]].sortBy(x=>x) // OK
res: IndexedSeq[Int] = Vector(1, 2, 3)
scala> List(3,2,1).toIndexedSeq.sortBy((x:Int)=>x) // OK
res: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3)
Run Code Online (Sandbox Code Playgroud) 好吧,我一直试图找到有关这方面的任何信息.我构建了一个小类来查看为字符串实现类型安全枚举的难度,因为我想将它们用于数据库字段名等.我从来不喜欢枚举只能用于整数的事实.
但是,即使我已经implicit operator为这个类实现了一个,每次我尝试使用它时,它都会给我一个无效的强制转换异常.我很茫然,因为我觉得我的代码在这一点上没有任何问题.
这是班级:
/// <summary>
/// SBool - type-safe-enum for boolean strings
/// </summary>
public sealed class SBool
{
private readonly String name;
private readonly int value;
// these guys were for conversions. They might work better in another case,
// but for this one, they weren't very helpful.
// ((I.e. they didn't work either.))
//private static readonly Dictionary<SBool, String> stringsByBool = new Dictionary<SBool, String>();
//private static readonly Dictionary<String, SBool> boolsByString = new Dictionary<String, SBool>();
public static …Run Code Online (Sandbox Code Playgroud) 我想将scala的值类应用于我的一个项目,因为它们使我能够丰富某些原始类型而没有很大的开销(我希望)并保持类型安全.
object Position {
implicit class Pos( val i: Int ) extends AnyVal with Ordered[Pos] {
def +( p: Pos ): Pos = i + p.i
def -( p: Pos ): Pos = if ( i - p.i < 0 ) 0 else i - p.i
def compare( p: Pos ): Int = i - p.i
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:每当我使用它们时,继承Ordered强制Pos对象的分配(从而引入很大的开销)吗?如果是这样的话:有没有办法绕过这个?
implicitScala 2 中的关键字和Scala 3 中的given+有什么区别?using是否只是implicit被分成了两个关键字,或者语义也不同,如果是,是如何不同的?
implicit ×10
scala ×8
c# ×2
asp.net-4.0 ×1
casting ×1
explicit ×1
generics ×1
int ×1
performance ×1
scala-3 ×1
string ×1
types ×1
value-class ×1