我有以下通用的Interval类(由用户soc为我制定):
case class Interval[T](from: T, to: T)(implicit num: Numeric[T]) {
import num.mkNumericOps // allows us to write from.toDouble and to.toDouble
def mid: Double = (from.toDouble + to.toDouble) / 2.0
}
Run Code Online (Sandbox Code Playgroud)
典型用例:Interval [Double]或Interval [Int].为了添加二元联合和交集运算符,我(implicit num: Numeric[T])在伴随对象中遵循了类似的模式:
object Interval {
def union[T](interval1: Interval[T], interval2: Interval[T])(implicit num: Numeric[T]) = {
import num.mkOrderingOps // allows interval1.from min
Interval[T](interval1.from min interval2.from, interval1.to max interval2.to)
}
def intersect[T](interval1: Interval[T], interval2: Interval[T])(implicit num: Numeric[T]) = {
import num.mkOrderingOps
Interval[T](interval1.from max interval2.from, …Run Code Online (Sandbox Code Playgroud) 这是我为jqGrid创建Json响应的代码,以及用于定义单元成员的新关键字,我收到以下消息"找不到隐式类型数组的最佳类型".
var resRows = results.Select(record =>
new
{
id = record.Reference,
cell = **new** []
{
record.Reference,
record.TradeDate.ToShortDateString(),
record.Currency1,
record.Currency2,
record.Notional.ToString(),
record.EffectiveDate.ToShortDateString(),
record.Quote.ToString()
}
}).ToArray();
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
C# 允许您定义对委托类型的隐式转换:
class myclass
{
public static implicit operator Func<String, int>(myclass x)
{
return s => 5;
}
public static implicit operator myclass(Func<String, int> f)
{
return new myclass();
}
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是,我们不能使用它来使对象看起来像函数:
var xx = new myclass();
int j = xx("foo"); // error
Action<Func<String, int>> foo = arg => { };
foo(xx); // ok
Run Code Online (Sandbox Code Playgroud)
有没有一种好方法可以使自己的类的对象直接在其基本实例上接受函数样式参数(参数)?有点像索引器,但用括号而不是方括号?
我有一个方法,它采用带有隐式视图边界的类型参数.使用无效数据类型调用方法时,是否可以使用@implicitNotFound批注来提供更好的编译器错误?
该方法的文档是无用的,甚至源代码也无济于事,所有在线使用的例子都处于特征或类级别.
案例类在scala中似乎没有隐式排序.
scala> case class A(i:Int)
defined class A
scala> implicitly[Ordering[A]]
<console>:10: error: No implicit Ordering defined for A.
implicitly[Ordering[A]]
Run Code Online (Sandbox Code Playgroud)
我想知道是否总是为所有案例类定义一个隐式排序,如果不是,至少有一种方法可以为相同成员类型的案例类/案例类的每个arity定义隐式排序.
假设我有代码:
class A(implicit s:String = "foo"){println(s)}
object X {
implicit val s1 = "hello"
}
object Y {
import X._
// do something with X
implicit val s2 = "hi"
val a = new A
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
<console>:14: error: ambiguous implicit values:
both value s2 in object Y of type => String
and value s1 in object X of type => String
match expected type String
val a = new A
Run Code Online (Sandbox Code Playgroud)
有什么办法,我可以告诉斯卡拉利用价值s2的Y?(如果我重新命名s2到s1,它工作正常,但是这不是我想要的). …
如果你像我一样,你偶尔想要为Scala集合或序列编写增强的方法,但是你想要绑定集合类型以及元素类型,而不仅仅是向Seq [T]进行upcast.
scala implicit implicit-conversion higher-kinded-types scala-collections
Scala函数具有以下链接方法:
fn1.andThen(fn2)
fn1.compose(fn2)
Run Code Online (Sandbox Code Playgroud)
但是如何写这种情况:
我有cleanUp()必须始终作为最后一步调用的函数。我还有很多其他功能,例如:
class Helper {
private[this] val umsHelper = new UmsHelper()
private[this] val user = umsHelper.createUser()
def cleanUp = ... // delete user/ and other entities
def prepareModel(model: TestModel) = {
// create model on behalf of the user
}
def commitModel() = {
// commit model on behalf of the user
}
}
Run Code Online (Sandbox Code Playgroud)
而且一些外部代码可以使用如下代码:
val help = new Helper()
help.prepareModel()
help.commitModel()
// last step should be called implicitly cleanUp
Run Code Online (Sandbox Code Playgroud)
如何以一种功能性的方式编写代码,即链接将始终cleanUp隐式调用函数作为最后一步?
注意:我将其视为C …
这是我的代码的简化版本。
我怎样才能避免打电话asInstanceOf(因为这是设计不佳的解决方案的味道)?
sealed trait Location
final case class Single(bucket: String) extends Location
final case class Multi(buckets: Seq[String]) extends Location
@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
class Log[L <: Location](location: L, path: String) { // I prefer composition over inheritance
// I don't want to pass location to this method because it's a property of the object
// It's a separated function because there is another caller
private def getSinglePath()(implicit ev: L <:< Single): String = s"fs://${location.bucket}/$path"
def getPaths(): Seq[String] =
location match { …Run Code Online (Sandbox Code Playgroud) 在 Scala 3 中summon似乎和旧的implicitly. 但是当我们深入研究实际例子时,我们发现情况并非如此。例如
case class A(i: Int, s: String)
val mirror = implicitly[Mirror.Of[A]]
type ValueOfs = Tuple.Map[mirror.MirroredElemLabels, ValueOf]
val valueOfs = summonAll[ValueOfs]
def values(t: Tuple): Tuple = t match
case (h: ValueOf[_]) *: t1 => h.value *: values(t1)
case EmptyTuple => EmptyTuple
Run Code Online (Sandbox Code Playgroud)
产生错误
cannot reduce inline match with
scrutinee: compiletime.erasedValue[App.ValueOfs] : App.ValueOfs
patterns : case _:EmptyTuple
case _:*:[t @ _, ts @ _]
Run Code Online (Sandbox Code Playgroud)
但是替换implicitly[Mirror.Of[A]]为summon[Mirror.Of[A]]编译很好。
在这种情况下和一般情况下summonvs的微妙之处是什么implicitly?
implicit ×10
scala ×8
c# ×2
casting ×2
annotations ×1
case-class ×1
composition ×1
delegates ×1
func ×1
implicits ×1
json ×1
methods ×1
numeric ×1
scala-2.11 ×1
scala-3 ×1
shapeless ×1
typeclass ×1