在Javascript中,可以将成员函数传递给函数setInterval().希望这是有道理的,我将向您展示我想要做的代码示例,因为这样更容易解释.
我想每10毫秒调用以下函数,并且能够访问和更改该函数中的类成员this.myArray()(每次调用该函数时).
function myClass()
{
this.myArray = new Array()
setInterval(this.slideLoop, 10);
}
// THE WHOLE POINT OF ALL THIS IS SO I CAN ACCESS THE ARRAY this.myArray()
// INSIDE THE FOLLOWING FUNCTION WHEN ITS CALLED FROM setInterval() EVERY 10ms
myClass.prototype.slideLoop = function()
{
alert( this.myArray[0] );
this.myArray.slice(0,1);
}
Run Code Online (Sandbox Code Playgroud) 愚蠢的问题,但这些例子都不适合我; 经典文章" 皮条客我的图书馆 "是错误的,甚至最简单的代码也有问题.
顺便说一句.我假设您必须在对象中放置转换方法(很多片段都省略了该部分).根据PiS的书,似乎挂隐式def是可以的,但这也给了我错误.
object Minutes
{
implicit def toMinutes(x : Int) = new Minutes(x)
}
class Minutes(private val x : Int)
{
def minutes = x.toString+"m"
}
object MainApp {
def main(args : Array[String])
{
println(5.minutes)
...
Run Code Online (Sandbox Code Playgroud)
错误 - "值分钟不是Int的成员".
我错过了什么?Scala 2.9.1.
我有以下代码:
trait DBO
trait BSONWriter[S]
trait HasWriter {
implicit def writer[T <: BSONWriter[_ <: DBO]]: T
}
Run Code Online (Sandbox Code Playgroud)
一切都很好!除了当我把它混合到我的时候objects,map所有集合的方法以及隐含CanBuildFrom在所有这些对象及其伴随类中使用的其他东西现在显示错误消息,如:
- 模糊隐含值:特征中的方法编写器保存类型[T <:reactivemongo.bson.handlers.BSONWriter [_ <traits.DBO]] => T和方法canBuildFrom在类型为[A] => scala的对象缓冲区中. collection.generic.CanBuildFrom [scala.collection.mutable.Buffer.Coll,A,scala.collection.mutable.Buffer [A]]匹配预期类型scala.collection.generic.CanBuildFrom [scala.collection.mutable.Buffer [models. world.Star],traits.IsInWorld with org.bundlelib.traits.Groupable {def asBSON:reactivemongo.bson.AppendableBSONDocument},That]
现在我不明白,为什么?混淆隐式方法的签名是不同的!我该如何防止这种情况?
lazy val productService = BeanLookup [ProductDataService]
object BeanLookup {
def apply[T](implicit manifest: Manifest[T], context: ActorContext) = {
val beanLookup =
context.actorFor("/user/spring/beanLookup")
Await.result(
(beanLookup.ask(LookupBean(manifest.erasure))(timeout)).mapTo[T](manifest),
timeout.duration) }
def apply[T](implicit manifest: Manifest[T], system: ActorSystem) = {
val beanLookup =
system.actorFor("/user/spring/beanLookup")
Await.result(
(beanLookup.ask(LookupBean(manifest.erasure))(timeout)).mapTo[T](manifest),
timeout.duration) } }
Run Code Online (Sandbox Code Playgroud)
scalac抱怨:
scala: ambiguous reference to overloaded definition,
both method apply in object BeanLookup of type (implicit manifest: Manifest[com.tooe.core.service.LocationCategoryDataService], implicit system: akka.actor.ActorSystem)com.tooe.core.service.LocationCategoryDataService
and method apply in object BeanLookup of type (implicit manifest: Manifest[com.tooe.core.service.LocationCategoryDataService], implicit context: akka.actor.ActorContext)com.tooe.core.service.LocationCategoryDataService
Run Code Online (Sandbox Code Playgroud) 请参阅下面的源代码.所有源代码都在同一个包中定义.当我定义一个源文件中的所有代码ShowMain.scala,我得到一个编译错误,但是当object ShowMain在被定义ShowMain.scala并trait Show与object Show中定义Show.scala,也没有编译错误.
我的问题: 这是什么原因?我遇到了什么语言规则?
示例代码:
object ShowMain {
def main(args: Array[String]): Unit = {
output("hello")
}
def output[A](a: A)(implicit show: Show[A]) =
println(show.show(a))
}
trait Show[-A] {
def show(a: A): String
}
object Show {
implicit object StringShow extends Show[String] {
def show(s: String) = s"[String: $s]"
}
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
(ScalaIDE/Scala 2.11.2在线包含output("hello"))
Multiple markers at this line
- not enough arguments for method output: …Run Code Online (Sandbox Code Playgroud) object Test extends App {
def print(s: String)(implicit p: Prefixer) = {
println(p.prefix + s)
}
print("test")
}
case class Prefixer(prefix: String)
object Prefixer {
implicit val p = Prefixer("***")
}
Run Code Online (Sandbox Code Playgroud)
上面的代码无法编译,因为编译器无法为Prefixer找到隐式值.但是,如果我将case类Prefixer和伴随对象放在另一个文件中,它就可以工作.这是为什么?
我有地图的String,以FunctionS的所有细节的是在语言的有效功能.当我向地图添加一个函数时,我需要指定类型(在本例中Int).
var functionMap: Map[String, (Nothing) => Any] = Map[String, (Nothing) => Any]()
functionMap += ("Neg" -> expr_neg[Int])
def expr_neg[T: Numeric](value: T)(implicit n: Numeric[T]): T = {
n.negate(value)
}
Run Code Online (Sandbox Code Playgroud)
相反,我该怎么做:
functionMap += ("Neg" -> expr_neg)
Run Code Online (Sandbox Code Playgroud)
没有,[Int]并在我打电话后添加它:
(unaryFunctionMap.get("abs").get)[Int](-45)
Run Code Online (Sandbox Code Playgroud) 我想直接使用类型类的函数而不必引用隐式证据对象.
现在我已经以推荐的方式实现了类型类(至少我认为是这样):
object Main {
import Implicits._
import Implicits.monoidInt._
def main(args: Array[String]): Unit = {
println(addTwice(1,2))
}
}
object Implicits {
implicit object monoidInt extends Monoid[Int] {
def zero: Int = 0
def append(x: Int, y: Int): Int = x + y
}
}
trait Monoid[A] {
def zero: A
def append(x: A, y: A): A
}
Run Code Online (Sandbox Code Playgroud)
而不是addTwice像:
def addTwice[A](x: A, y: A)(implicit ev: Monoid[A]): A = {
ev.append(ev.append(x,y), y)
}
Run Code Online (Sandbox Code Playgroud)
我想写:
def addTwice[A: Monoid[A]](x: A, y: A): …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
object Application {
case class User(id: Long, username: String)
case class Request(path: String)
case class WrappedRequest(user: User, request: Request)
def updateUserAction(implicit request: WrappedRequest) = {
updateUser("john@mail.com") // <-- I need request.user to be passed implicitly here
}
def updateUser(email: String)(implicit user: User) = {
println(user.username)
}
def main(args: Array[String]) = {
implicit val request = WrappedRequest(User(1L, "john"), Request("/"))
updateUserAction
}
}
Run Code Online (Sandbox Code Playgroud)
从以上,是有可能通过request.user在updateUserAction方法将UpdateUser两个方法隐含?
我有一个简单的代码
private def convertFieldsNames (fieldsNames: Array[String]): Array[String] =
fieldsNames.map(convertFieldName)
private def convertFieldName (fieldName: String): String = s"!!! $fieldName"
val res = convertFieldsNames(Array("123", "456"))
res.map(println)
Run Code Online (Sandbox Code Playgroud)
它工作正常,但是当我添加类型转换功能时,我将在其他功能中使用
implicit def fromStringToEitherStringOrArray (str: String): Either[String, Array[String]] = Left(str)
implicit def fromArrayToEitherStringOrArray (arr: Array[String]): Either[String, Array[String]] = Right(arr)
Run Code Online (Sandbox Code Playgroud)
我一行出现错误
fieldsNames.map(convertFieldName)
type mismatch;
found : String => String
required: Array[String] => ?
Run Code Online (Sandbox Code Playgroud)
我希望这些转换仅在需要转换为Either值时才会生效,所以我无法理解为什么此错误在根本没有Either类型的行中冒出
implicit ×10
scala ×9
typeclass ×3
types ×2
erasure ×1
generics ×1
javascript ×1
oop ×1
overloading ×1
scope ×1