我试图创建从任何类型(比如Int)到String的隐式转换...
隐式转换为String意味着RichString方法(如反向)不可用.
implicit def intToString(i: Int) = String.valueOf(i)
100.toCharArray // => Array[Char] = Array(1, 0, 0)
100.reverse // => error: value reverse is not a member of Int
100.length // => 3
Run Code Online (Sandbox Code Playgroud)
隐式转换为RichString意味着String方法(如toCharArray)不可用
implicit def intToRichString(i: Int) = new RichString(String.valueOf(i))
100.reverse // => "001"
100.toCharArray // => error: value toCharArray is not a member of Int
100.length // => 3
Run Code Online (Sandbox Code Playgroud)
使用两个隐式转换意味着重复的方法(如长度)是不明确的.
implicit def intToString(i: Int) = String.valueOf(i)
implicit def intToRichString(i: Int) = new RichString(String.valueOf(i))
100.toCharArray // => Array[Char] = Array(1, …Run Code Online (Sandbox Code Playgroud) 控制器:
def test = Action { implicit request =>
import play.api.i18n._
val msg = Messages("error.invalid")
implicit val langInController = lang(request)
Ok(views.html.test(langInController, msg))
}
Run Code Online (Sandbox Code Playgroud)
视图:
@(langInController: play.api.i18n.Lang, msg:String)(implicit request: Request[_])
<div>Lang from controller: @langInController, Message: @msg</div>
<div>Message from view: @play.api.i18n.Messages("error.required")</div>
Run Code Online (Sandbox Code Playgroud)
消息资源,conf/messages.zh-CN:
error.required=?????
Run Code Online (Sandbox Code Playgroud)
试
使用英文Firefox发送请求标头Accept-Language:en-us,en;q=0.5以访问test操作.结果是:
Language from controller: Lang(en,), Message: This field is required
Message in view: ?????
Run Code Online (Sandbox Code Playgroud)使用中国谷歌浏览器发送请求标题Accept-Language:zh-CN,zh;q=0.8以访问它.结果是:
Language: Lang(zh,CN), Message: ?????
Message in view: ?????
Run Code Online (Sandbox Code Playgroud)从测试中我们知道:
Accept-Language环境:
在我的库中,我有三个类型类:
trait Monoid[T] {
val zero : T
def sum(x : T, y : T) : T
}
trait AbelianGroup[T] extends Monoid[T] {
def inverse(x : T) : T
def difference(x : T, y : T) : T
}
//represents types that are represents lists with a fixed number of elements, such as
//the tuple type (Int, Int)
trait Vector[T, U] {
...
}
Run Code Online (Sandbox Code Playgroud)
在以下条件下,这些类型类可以相互转换:
T是一个scala.math.Numeric类型,它也是一个类型AbelianGroup.T是a AbelianGroup,那么它也是一个Monoid(当前AbelianGroup …[error] test.scala:31: ambiguous implicit values:
[error] both method taggedQueryParamDecoder in trait ExternalInstances0 of type [A, T](implicit evidence$2: org.http4s.QueryParamDecoder[A])org.http4s.QueryParamDecoder[scalaz.@@[A,T]]
[error] and method iiQueryParamDecoder in trait ExternalInstances1 of type [B](implicit ii: foo.InvariantInstances[B])org.http4s.QueryParamDecoder[B]
[error] match expected type org.http4s.QueryParamDecoder[scalaz.@@[String,foo.tags.Social]]
[error] implicitly[QueryParamDecoder[String @@ Social]]
[error] ^
Run Code Online (Sandbox Code Playgroud)
我导入instances._; instances延伸ExternalInstances1和ExternalInstances1延伸ExternalInstances0.由于这种继承,我希望其ExternalInstances1成员能够赢得胜利ExternalInstances0,而不是产生歧义.
为什么会发生这种情况,我该如何解决?谢谢.
来源位于http://scastie.org/12233,转载如下:
/***
scalaVersion := "2.11.7"
libraryDependencies += "org.http4s" %% "http4s-core" % "0.10.0"
resolvers ++= Seq(
"tpolecat" at "http://dl.bintray.com/tpolecat/maven",
"Scalaz Bintray Repo" …Run Code Online (Sandbox Code Playgroud) 我需要编写一个对Seq[T]对象执行排序的通用代码。我知道这不会是可能执行排序操作,直到我们知道base class和它的attributes。在查看了这个答案之后,我使用了这段代码,我的要求是处理尽可能多的自定义数据类型。
case class Country(name: String, id : Int)
type CountrySorter = (Country, Country) => Boolean
def byName : CountrySorter = (c1:Country, c2:Country) => c1.name < c2.name
def byId : CountrySorter = (c1:Country, c2:Country) => (c1.id < c2.id)
val sortingMap = Map[String, CountrySorter](
"sortByCountryName" -> byName ,
"soryByCountryId" -> byId
)
Run Code Online (Sandbox Code Playgroud)
函数调用
def sort[T]( input : Seq[T], criteria : String) : Seq[T] = {
input.sortWith(sortingMap(criteria))
}
Run Code Online (Sandbox Code Playgroud)
input.sortWith(sortingMap(criteria))在这里我得到错误,因为sortWith函数只需要 …