我有这种遗产
sealed abstract class MyValue
case class MyString(s:String) extends MyValue
case class MyBoolean(b:Boolean) extends MyValue
case class MyR(m1:MyValue, m2:MyValue) extends MyValue
case class MyU(m1:MyValue, m2:MyValue) extends MyValue
/* ... */
Run Code Online (Sandbox Code Playgroud)
和
implicit def string2myString(s:String) = MyString(s)
implicit def boolean2myBoolean(b:Boolean) = MyBoolean(b)
Run Code Online (Sandbox Code Playgroud)
但是,我想这样做:
"hello" MyR true // R(MyString("hello"), MyValue(true))
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
REPL会话中的以下代码:
case class Foo(x : Int)
case class Bar(x : Int)
case class Converter(y : Int) {
def convert(x : Int) = x + y
}
implicit def fooFromBar(b : Bar)(implicit c : Converter) = Foo(c convert (b x))
def roundaboutFoo(x : Int, converter : Converter) : Foo = Bar(x)
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
错误:找不到参数c的隐含值:转换器def roundaboutFoo(x:Int,converter:Converter):Foo = Bar(x)
如果它不明显(暗示),我正在尝试做的是Bar(x)隐式转换为a Foo.隐式转换本身虽然是通过隐式参数化的Converter.我想要使用此转换的时间都有一个Converter可用的实例作为方法的参数.
我半预计将不能够从发现的隐式转换死Bar到Foo,由于fooFromBar不是从一个简单的函数Foo来Bar,但我读的这个问题是隐式转换可以有隐含参数,实际上编译器似乎有部分出来了.
我发现了另一个问题,详细解答了Scala寻找填充内容的地方.但它只证实了我之前的理解:Scala在直接范围内首先看起来,然后是其他一些与此无关的地方. …
我需要使用其一般形式绘制2D椭圆 (x-c)'A(x-c)=1
我想知道如何使用ezplot在MATLAB中有效地完成这项工作.
如何在Java中调用数组函数?
目前它看起来像:
public static void WriteLine(Object Array[]) {
for (int I = 0; I < Array.length; ++I) {
Out.println(Array[I]);
}
}
public static void WriteLine(Object Text) {
Out.println(Text);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
public static <T> void WriteLine(T Array[]) {
for (int I = 0; I < Array.length; ++I) {
Out.println(Array[I]);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主要,我做:
int[] I = new int[]{1, 2, 3, 4, 5};
WriteLine(I);
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
WriteLine<int[]>(I);
Run Code Online (Sandbox Code Playgroud)
不起作用..
它打印:[I @ 2f56f920
又名int数组的地址.如何显式调用特定的数组函数或如何让编译器知道哪一个自动调用(隐式)?
我还不习惯Java/Generics/Object ..刚刚从C++移动并用于模板:(
以下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某种方式块的隐式转换str1和str2到WrappedString.为什么?有没有一种方法来修改stringToNode这样的zip工作,但是stringToNode当我调用一个需要带Node参数的函数时仍然使用String?
由于好奇的定义和范围,typedef我在2 .c文件中写下了C代码:
main.c中
#include <stdio.h>
int main()
{
int a = 5, b = 6;
printf("a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
}
Run Code Online (Sandbox Code Playgroud)
swap.c
typedef T;
void swap(T* a, T* b)
{
T t = *a;
*a = *b;
*b = t;
}
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,代码文件可以用Visual Studio C编译器编译(cl.exe /Tc main.c swap.c)
程序运行正常!根据我的理解,typedef需要2个参数,但为什么这个代码编译并运行?
为了进一步分析,在main函数中,我声明了另外2个浮点变量,并尝试在交换2个整数后交换两个,但这次它无法编译(使用cl.exe).令人惊奇的是,代码可以使用Tiny C(tcc.exe main.c swap.c)编译和运行,因此它就像模板方法一样工作!
我是Slick和Scala的新手.首先看一下我的示例表,其中包含用于查询的case类映射和帮助器SuitsManager.现在SuitsManager玩的方法被Play调用!DBAction中的控制器(我正在使用play-slick 0.6.0.1).
package models
import play.api.db.slick._
import play.api.db.slick.Config.driver.simple._
import scala.collection.immutable.HashMap
import scala.slick.jdbc.JdbcBackend
case class Suit(id:Option[Long],
complainant: String,
defender: String,
litigation: Long,
litigationValue: BigDecimal,
status: Long)
class Suits(tag: Tag) extends Table[Suit](tag, "SUITS") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def complainant = column[String]("complainant")
def defender = column[String]("defender")
def litigation = column[Long]("litigation")
def litigationValue = column[BigDecimal]("litigationValue")
def status = column[Long]("status")
def * = (id.?, complainant, defender, litigation, litigationValue, status) <> (Suit.tupled, Suit.unapply)
}
object SuitsManager {
val suits …Run Code Online (Sandbox Code Playgroud) 我在scala中创建伴随对象并尝试object在class不导入的情况下使用implictis函数.但无论何时,尝试编译代码我都会收到错误:type mismatch;似乎无法自动导入implictis.以下是我的代码:
object ImplicitTest5 {
implicit def dollarToRupa(dollar: Dollar): Rupa = {
println("calling .... dollarToEuro")
Rupa(dollar.value)
}
implicit def dollarToEuro(dollar: Dollar): Euro = {
println("calling .... dollarToEuro")
Euro(dollar.value)
}
}
case class Dollar(value: Double)
case class Euro(value: Double)
case class Rupa(value: Double)
class ImplicitTest5 {
private val value = "String"
def conversion = {
val euro: Euro = Dollar(3.1)
println(s" ----- $euro")
}
}
Run Code Online (Sandbox Code Playgroud)
当我import ImplicitTest5._在我的班级使用时,它将编译并运行正常.根据Scala中的Programming,Page:478它将正常工作,并且不需要定义导入. …
scala compiler-errors implicit implicit-conversion companion-object
我正在使用Scala创建一个处理固定长度的库.
对于编码和解码,strings我使用基于类型的系统.我提供了自己的Read[A]并Write[A]处理这些行动.
我Write型类使用Show从Cats引擎盖下.它有效,但它要求用户明确导入猫的暗示,如:
import com.github.atais.util.Read._
import cats.implicits._
import com.github.atais.util.Write._
Run Code Online (Sandbox Code Playgroud)
这个例子可以在Github项目上看到:https:
//github.com/atais/Fixed-Length/blob/702d7d242e5b1f6e1c6b581ad7356f36ca6ed8d9/src/test/scala/com/github/atais/fixedlength/simple/CodecTest.scala
有走动吗?我想隐藏cats导入或(如果可能的话)将所有三个合并到一个隐式对象中.
我有一个简单的PartialFunction
type ChildMatch = PartialFunction[Option[ActorRef], Unit]
def idMatch(msg: AnyRef, fail: AnyRef)(implicit ctx: ActorContext): ChildMatch = {
case Some(ref) => ref forward msg
case _ => ctx.sender() ! fail
}
Run Code Online (Sandbox Code Playgroud)
但是当我试图使用它时 - 编译器需要这样的声明:
...
implicit val ctx: ActorContext
val id: String = msg.id
idMatch(msg, fail)(ctx)(ctx.child(id))
Run Code Online (Sandbox Code Playgroud)
你可以看到它想要ctx作为第二个参数而不是隐含的
我怎么能改变我的idMatch函数使用它像这样:
...
implicit val ctx: ActorContext
val id: String = msg.id
idMatch(msg, fail)(ctx.child(id))
Run Code Online (Sandbox Code Playgroud)
?