我有一个过滤器,linkifyStuff,我想在其中使用另一个过滤器处理一些变量.我无法弄清楚从另一个过滤器调用一个过滤器的语法.
我知道滤波器链接 - 这不是我想要做的.我想将过滤器应用于linkifyStuff过滤器中的局部变量,而不是其输入或输出.
我希望下面会有类似的工作,但$ filter('filtername')显然不是正确的语法.
module.filter('sanitizeStuff', function() {
// ...
})
module.filter('prettifyStuff', function() {
// ...
})
module.filter('linkifyStuff', function($filter) {
return function(text) {
// ...
// ...
return $filter('sanitizeStuff')(foo) + ' whatever ' + $filter('prettifyStuff')(bar)
}
})
Run Code Online (Sandbox Code Playgroud)
我可以为sanitizeStuff和sanitizeStuff编写一个简单的js函数,并从这些过滤器调用该函数,但这似乎是错误的.如何以角度方式做任何建议?
谢谢.
我正在尝试copy()一个具有类型参数的 Scala 案例类。在呼叫站点,该值的类型为Foo[_]。
这按预期编译:
case class Foo[A](id: String, name: String, v1: Bar[A])
case class Bar[A](v: A)
val foo: Foo[_] = Foo[Int]("foo1", "Foo 1", Bar[Int](1))
foo.copy(id = "foo1.1")
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加另一个 type 成员Bar[A],它将不再编译:
case class Foo[A](id: String, name: String, v1: Bar[A], v2: Bar[A])
case class Bar[A](v: A)
val foo: Foo[_] = Foo[Int]("foo1", "Foo 1", Bar[Int](1), Bar[Int](2))
foo.copy(id = "foo1.1") // compile error, see below
Run Code Online (Sandbox Code Playgroud)
type mismatch;
found : Playground.Bar[_$1]
required: Playground.Bar[Any]
Note: _$1 <: Any, but class …Run Code Online (Sandbox Code Playgroud) 我有一个foo包含类的包FStream.package对象foo定义了一些提供扩展器方法的隐式值类FStream.我想将这些值类从包对象中移出并放入它们自己的单独文件中,但我也希望它们在我使用时始终可用FStream(或者最好是当我使用foo包中的任何东西时.是否可以实现这一点?我尝试将隐式值类放入其他对象中,但我无法从对象扩展.尝试将它们放在类或特征中,但隐式值类只能在其他对象中定义.
富/ FStream.scala
package foo
class FStream {
def makeFoo(): Unit = ???
}
Run Code Online (Sandbox Code Playgroud)
富/ package.scala
package foo
package object foo {
// I want to move these definitions into separate files:
implicit class SuperFoo(val stream: FStream) extends AnyVal {
def makeSuperFoo(): Unit = ???
}
implicit class HyperFoo(val stream: FStream) extends AnyVal {
def makeHyperFoo(): Unit = ???
}
}
Run Code Online (Sandbox Code Playgroud)
酒吧/ usage.scala
package bar
import foo._ // …Run Code Online (Sandbox Code Playgroud) 我正在尝试但未能让这样的东西在 Scala 3 中工作:
type TupleK[K[*], V[*], A] = (K[A], V[A])
final class MapK[K[*], V[*]] private (val rawMap: Map[K[?], V[?]]) {
def foreach(f: TupleK[K, V, ?] => Unit): Unit = {
rawMap.foreach(f.asInstanceOf[Tuple2[K[?], V[?]] => Any])
}
}
object MapK {
def apply[K[*], V[*]](entries: TupleK[K, V, ?]*): MapK[K, V] = {
new MapK[K, V](Map(entries: _*))
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用:
class Key[A]()
type Id[A] = A
val intKey = Key[Int]
val strKey = Key[String]
MapK[Key, Id](intKey -> 1, strKey -> "a")
Run Code Online (Sandbox Code Playgroud)
在 Scala …