我遇到了一个代码片段,但无法理解它。片段是:
implicit val dummyVisit = Visit("", 1L, 1, 1, 1, 1L)
implicit val dummyOrder = Order("", 1L, 1, 1, 1, 1L)
def process[T](events : Array[T])(implicit t: T):Unit = {
println(t)
if(!events.isEmpty)
t match {
case r: Order => processOrder(events.asInstanceOf[Array[Order]])
case r: Visit => processVisit(events.asInstanceOf[Array[Visit]]);
}
}
def processOrder(arr: Array[Order]): Unit = { println(arr.size) }
def processVisit(arr: Array[Visit]): Unit = { println(arr.size) }
Run Code Online (Sandbox Code Playgroud)
变量, 要求&存在implicit。tdummyVisitdummyOrder
问题:
这是正确的使用方法吗implicit parameter?
有没有更好的方法来获取 的类类型T,而不使用隐式参数?
在下面最简单的示例中,我没有编译错误:
object App {
def main(args: Array[String]) = {
test[Int]()
}
def test[T <: Int : ClassTag]() = println(implicitly[ClassTag[T]])
}
Run Code Online (Sandbox Code Playgroud)
程序打印Int. ClassTag[T]但我不明白为什么可以找到类型的对象来implicitly[ClassTag[T]]调用?我所做的唯一一件事就是提供泛型类型参数。它从哪里来ClassTag[Int]?
我想将一些测试数据放入一些隐式函数中。
我想将一些参数拟合到椭圆方程 f(x,y)=a 中,其中 a 是已知变量。我的测试数据和函数更复杂,但是我得到的数据点比变量更多。将我想要拟合的方程转换为像 f(x)=y 这样的显式形式是不可能的,因此我附加了一些代码来获得基本思想。
Test = {{0, 1}, {0.1, 0.9}, {1.1, 0}};
Ftest = a*x^2 + b*y^2
FindFit[Test, Ftest == 2, {a, b}, {x, y}];
Run Code Online (Sandbox Code Playgroud)
然而,这会导致错误:坐标数 (1) 不等于变量数 \ (2)。>>
如果我有代码:
int f(int a) { return a; }
double f(double g) { return g; }
int main()
{
int which = f(1.0f);
}
Run Code Online (Sandbox Code Playgroud)
调用f的哪个重载,为什么?
我有这段代码,它有效:
val directions = rs.map(_.direction) // Direction extends Enumeration
directions == directions.sorted.reverse
Run Code Online (Sandbox Code Playgroud)
我想改为做这样的事情:
ratings.map(_.direction).isInBackwardsOrder
class RichSeq[T](seq: Seq[T]) {
def isInBackwardsOrder = seq == seq.sorted.reverse
}
object RichSeq {
implicit def seq2richSeq[T](seq: Seq[T]) = new RichSeq[T](seq)
}
Run Code Online (Sandbox Code Playgroud)
我不断收到以下编译错误:
could not find implicit value for parameter ord: Ordering[T]
def isInBackwardsOrder = seq == seq.sorted.reverse
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它可以找到参数ord的隐含值,当它是原始形式时,但是一旦我将它拉入实用程序类就找不到它.
谢谢你的帮助,Alex
例如,我想让它以设置jDName为"John Doe"和jDAge32的方式工作:
case class Person(name : String, surname : String, age : Int)
val johnDoe = Person("John", "Doe", 32)
val jDName : String = johnDoe
val jDAge : Int = johnDoe
Run Code Online (Sandbox Code Playgroud)
我可以在Person类中编写函数来提供对String,Int和其他(自定义)类型的隐式转换吗?另一件事是显式的强制转换操作 - 也很有趣,但我不确切地知道如何在Scala中编写这个例子.
我想定义一个带有一个显式参数和一个隐式参数的函数,如下所示:
def foo(a: Int)(implicit b: Int) : Int
Run Code Online (Sandbox Code Playgroud)
但作为一个类或对象,就像这样
object Foo extends ((Int,Int) => Int) {
def apply(a: Int)(implicit b: Int) : Int = { ... }
}
Run Code Online (Sandbox Code Playgroud)
这样可以像这样调用函数:
implicit val b = 2
val g = Foo(1)
Run Code Online (Sandbox Code Playgroud)
我没有得到哪个类Foo应该扩展的基础声明.如何才能做到这一点?
我写了一个小事务助手,它传递了一个闭包并在事务中执行它:
object Transaction {
val emf = Persistence.createEntityManagerFactory("defaultPersistenceUnit")
def execute(action: EntityManager => Unit) {
val em = emf.createEntityManager()
em.getTransaction.begin()
action(em)
em.getTransaction.commit()
em.close()
}
}
Run Code Online (Sandbox Code Playgroud)
然后我ItemRepository就是这样的:
object ItemRepository {
def add(implicit entityManager: EntityManager, item: Item) {
entityManager.persist(item)
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我想要隐式传递的EntityManager执行存储库方法:
Transaction.execute(implicit em => ItemRepository.add(item))
Run Code Online (Sandbox Code Playgroud)
但是编译器告诉我:
没有足够的方法添加参数:(隐式实体管理器:javax.persistence.EntityManager,隐含项:models.Item)单位.未指定的值参数项.
如果我显式传递参数,一切都有效:
Transaction.execute(em => ItemRepository.add(em, item))
Run Code Online (Sandbox Code Playgroud)
这有什么不对?它看起来与这个答案几乎相同.
我收到了这个错误
"Error 8 Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<DataRow>'
Run Code Online (Sandbox Code Playgroud)
"
产生问题的代码行是这个......
List<DataRow> dRowList = target.SearchFor(searchPhrase, searchField, matchesAny);
Run Code Online (Sandbox Code Playgroud)
target.SearchFor返回DataRow列表.我无法弄清楚出了什么问题.
有没有人有一个雄辩的解决方案,因为缺乏对C#中非静态隐式运算符的支持?以下代码显示了我当前的问题:
class Foo
{
public int x { get; set; }
public int y { get; set; }
public Foo()
{
}
public static implicit operator Foo(Bar b)
{
Foo newFoo = new Foo();
newFoo.y = b.y;
return newFoo;
}
}
class Bar
{
public int y { get; set; }
public Bar()
{
}
}
Foo foo = new Foo();
foo.x = 42;
Bar bar = new Bar();
bar.y = 52;
foo = bar;
Console.WriteLine(foo.x); // THIS PRINTS 0 …Run Code Online (Sandbox Code Playgroud)