虽然在另一个问题上开头,但我遇到了似乎相关的不同谜语.这是其中之一:
trait Sys[S <: Sys[S]] {
type Peer <: Sys[Peer]
}
trait Fenced {
type Peer <: Sys[Peer]
}
def makeFence[S <: Sys[S]] = new Fenced { type Peer = S#Peer }
Run Code Online (Sandbox Code Playgroud)
错误如下:
error: overriding type Peer in trait Fenced with bounds >: Nothing <: Sys[this.Peer];
type Peer has incompatible type
def makeFence[S <: Sys[S]] = new Fenced { type Peer = S#Peer }
^
Run Code Online (Sandbox Code Playgroud)
为什么?(也试图自我类型添加_:S =>到Sys,没有问题)
虽然Rex的答案使得构造Fenced对象成为可能,但它并没有真正解决我在使用类型投影(S#Peer)时表示类型字符丢失的问题.我想出了另一种造成更严格限制的情景; 我认为这是核心问题:
trait …Run Code Online (Sandbox Code Playgroud) types scala abstract-type type-projection bounded-quantification
以下是我的错误:
trait Foo[A]
class Bar[A](set: Set[Foo[A]] = Set.empty)
Run Code Online (Sandbox Code Playgroud)
这产生了
<console>:8: error: polymorphic expression cannot be instantiated to expected type;
found : [A]scala.collection.immutable.Set[A]
required: Set[Foo[?]]
class Bar[A](set: Set[Foo[A]] = Set.empty)
^
Run Code Online (Sandbox Code Playgroud)
我必须重复类型参数,这非常烦人Set.empty.为什么类型推断失败了这个默认参数?以下作品:
class Bar[A](set: Set[Foo[A]] = { Set.empty: Set[Foo[A]] })
Run Code Online (Sandbox Code Playgroud)
请注意,这与此无关Set:
case class Hallo[A]()
class Bar[A](hallo: Hallo[A] = Hallo.apply) // nope
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这不仅有效:
class Bar[A](hallo: Hallo[A] = Hallo.apply[A])
Run Code Online (Sandbox Code Playgroud)
......还有这个:
class Bar[A](hallo: Hallo[A] = Hallo()) // ???
Run Code Online (Sandbox Code Playgroud) 我可以得到一个Type从TypeTag[A]使用tpe方法.但我还可以从类型中恢复类型标记吗?
import scala.reflect.runtime.{universe => ru}
import ru.{Type, TypeTag}
def forward[A](implicit tt: TypeTag[A]): Type = tt.tpe
def backward(t: Type): TypeTag[_] = ???
Run Code Online (Sandbox Code Playgroud)
原因是我有一个API使用类型标签作为地图的键,但在某些时候我只有类型并删除了标签.
我在我的DSL中遇到问题,重载泛型方法导致编译器希望我添加显式参数类型:
def alpha[T](fun: Int => T): String = fun(33).toString
def beta [T](fun: Int => T): String = fun(66).toString
def beta [T](thunk: => T): String = thunk.toString
alpha { _ + 11 } // ok
beta { _ + 22 } // "error: missing parameter type for expanded function"
beta { _: Int => _ + 22 } // ok... ouch.
Run Code Online (Sandbox Code Playgroud)
我有可能摆脱最后一行的混乱局面吗?
编辑:
为了证明重载不是scalac本身的歧义问题,这里是一个没有类型参数的版本,它可以很好地工作:
def beta(fun: Int => String): String = fun(66).reverse
def beta(thunk: => String): String = thunk.reverse
beta(_.toString) // …Run Code Online (Sandbox Code Playgroud) 在Scala 2.10中,MurmurHash由于某种原因被弃用,说我MurmurHash3现在应该使用.但是API是不同的,并且没有有用的scaladoc用于MurmurHash3- >失败.
例如,当前代码:
trait Foo {
type Bar
def id: Int
def path: Bar
override def hashCode = {
import util.MurmurHash._
var h = startHash(2)
val c = startMagicA
val k = startMagicB
h = extendHash(h, id, c, k)
h = extendHash(h, path.##, nextMagicA(c), nextMagicB(k))
finalizeHash(h)
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何使用MurmurHash3呢?这需要一个快速的操作,最好不分配,所以我不希望建立一个Product,Seq,Array[Byte]或whathever MurmurHash3似乎为我提供.
是否可以使用带有varargs 的磁铁模式:
object Values {
implicit def fromInt (x : Int ) = Values()
implicit def fromInts(xs: Int*) = Values()
}
case class Values()
object Foo {
def bar(values: Values) {}
}
Foo.bar(0)
Foo.bar(1,2,3) // ! "error: too many arguments for method bar: (values: Values)Unit"
Run Code Online (Sandbox Code Playgroud)
?
它让我发疯,字符串插值有一些特殊的规则,不允许从a + b样式直接转换:
// ok
def test(f: java.io.File) = {
val abs = f.getAbsoluteFile
val isF = abs.isFile
"select " + (if (isF) "file" else "folder") +"\"" + abs.getName +"\" of folder"
}
// fail
def test(f: java.io.File) = {
val abs = f.getAbsoluteFile
val isF = abs.isFile
s"select ${if (isF) "file" else "folder"} \"${abs.getName}\" of folder"
}
Run Code Online (Sandbox Code Playgroud)
然后有一个可爱的错误消息:
<console>:38: error: value $ is not a member of String
s"select ${if (isF) "file" else "folder"} \"${abs.getName}\" of folder …Run Code Online (Sandbox Code Playgroud) 使用Scala 2.10/2.11宏天堂注释宏,如何添加或扩展带注释的类的伴随对象?骨架:
import scala.annotation.StaticAnnotation
import scala.reflect.macros._
import language.experimental.macros
class foo extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro fooMacro.impl
}
object fooMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = ???
}
Run Code Online (Sandbox Code Playgroud)
这样,给定
trait Foo[A]
Run Code Online (Sandbox Code Playgroud)
以下输入
@foo class Bar
object Baz {
def baz = 33
}
@foo class Baz
Run Code Online (Sandbox Code Playgroud)
将扩展为:
object Bar {
implicit def hasFoo: Foo[Bar] = ???
}
class Bar
object Baz {
def baz = 33
implicit def hasFoo: Foo[Baz] = ???
}
class Baz
Run Code Online (Sandbox Code Playgroud)
这是第一次天真的尝试,def …
case class Cat(name: String)
object CuterImplicits {
implicit class CatCuteChecker(c: Cat) {
def isCute(c: Cat) = true
}
}
trait CuteChecker[A] {
def isCute(a: A): Boolean
}
object CheckingForCuteness {
def isItCute[A](a: A) = implicitly[CuteChecker[A]].isCute(a)
}
object Main extends App {
CheckingForCuteness.isItCute[Cat](Cat("funny"))
}
Run Code Online (Sandbox Code Playgroud)
怎么修:
错误:(17,37)找不到参数e的隐含值:CuteChecker [A] def isItCute [A](a:A)=隐式[CuteChecker [A]].isCute(a)^
我可以使用Sphinx4附带的en-us东西,没问题:
cfg.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us")
cfg.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict")
cfg.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin")
Run Code Online (Sandbox Code Playgroud)
我可以用它来转录英文声音文件录制.
现在我想用德语录音.在网站上,我找到了声学和语言模型的链接.其中有一个档案'德国Voxforge'.我找到声学模型路径的相应文件.但就我所见,它不包含字典或语言模型.
如何在Sphinx4中获取德语的字典和语言模型路径?
scala ×9
cmusphinx ×1
generics ×1
hash ×1
implicit ×1
macros ×1
murmurhash ×1
overloading ×1
reflection ×1
sphinx4 ×1
types ×1