请考虑以下代码:
public class TextType {
public TextType(String text) {
underlyingString = text;
}
public static implicit operator String(TextType text) {
return text.underlyingString;
}
private String underlyingString;
}
TextType text = new TextType("Something");
String str = text; // This is OK.
Run Code Online (Sandbox Code Playgroud)
但是如果可能的话,我希望能够做到以下几点.
TextType textFromStringConstant = "SomeOtherText";
Run Code Online (Sandbox Code Playgroud)
我无法使用TextType隐式运算符重载扩展String类,但有没有办法将文字字符串分配给另一个类(由方法或其他东西处理)?
String是一个引用类型,因此当他们开发C#时,他们显然必须使用某种方式来获取类的字符串文字.我只是希望它不是硬编码的语言.
一些前端专家声称<input>使用<label>(隐式标签)包装是一种更好的做法(与明确的做法相比,即使它们具有for属性).
无论出于什么原因,我想看看如何以优雅的方式在技术上完成Django.
有一个带有隐式参数的模板类声明:
List.h
template <typename Item, const bool attribute = true>
class List: public OList <item, attribute>
{
public:
List() : OList<Item, attribute> () {}
....
};
Run Code Online (Sandbox Code Playgroud)
我尝试在不同的头文件中使用fllowing forward声明:
Analysis.h
template <typename T, const bool attribute = true>
class List;
Run Code Online (Sandbox Code Playgroud)
但是G ++显示了这个错误:
List.h:28: error: redefinition of default argument for `bool attribute'
Analysis.h:43: error: original definition appeared here
Run Code Online (Sandbox Code Playgroud)
如果我使用没有隐式参数的前向声明
template <typename T, const bool attribute>
class List;
Run Code Online (Sandbox Code Playgroud)
编译器不接受这种结构
Analysis.h
void function (List <Object> *list)
{
}
Run Code Online (Sandbox Code Playgroud)
并显示以下错误(即不接受隐含值):
Analysis.h:55: error: wrong number of …Run Code Online (Sandbox Code Playgroud) 我想知道这里的原因是什么(implicit ev: Null <:< A1):
sealed abstract class Option[+A] extends Product with Serializable {
def orNull[A1 >: A](implicit ev: Null <:< A1): A1 = this getOrElse null
...
}
Run Code Online (Sandbox Code Playgroud)
岂不
def orNull[A]: A = this getOrElse null
Run Code Online (Sandbox Code Playgroud)
考虑到它似乎甚至不适用于像这样的值类型
Option(1).orNull
Run Code Online (Sandbox Code Playgroud)
但
Option(1).getOrElse(null)
Run Code Online (Sandbox Code Playgroud)
呢?
Option的源代码
我正在尝试编写代码来表示Scala中的多项式.我需要这个代码是类型多态,所以我使用implicits来处理不同的类型.我有:
case class Mono[T](degree: Int, coeff: T) {
def Degree: Int = return degree
def Coeff: T = return coeff
}
class Poly[T](private val terms: List[Mono[T]]) {
trait Semiring[T] {
def add(x:T, y:T): T
def mul(x:T, y:T): T
def exponent(x: T, n:Int): T
val unitA: T
}
implicit object IntSemiring extends Semiring[Int] {
def add(x: Int, y: Int): Int = x+y
def mul(x: Int, y: Int): Int = x*y
def exponent(x: Int, n:Int): Int = if(n==0) 1 else x*exponent(x, n-1) …Run Code Online (Sandbox Code Playgroud) 我刚刚遇到函数和对象之间的奇怪差异(scala 2.10):
implicit def conv(c: Int => String) : (PrintStream => Int => Unit) = p => v => p.println(c(v))
def f(h: PrintStream => Int => Unit) : Unit = h(System.out)(1)
def a(x: Int) = x.toString
val b = (x: Int) => x.toString
// def main(args: Array[String]) = f(a) // fail
// def main(args: Array[String]) = f((x: Int) => x.toString) // fail
def main(args: Array[String]) = f(b) // ok
Run Code Online (Sandbox Code Playgroud)
为什么defs/lambda文字和lambda vals之间有区别?
更新:显然,二进制函数不会出现问题:只有转换函数至少有两个参数时,才能将函数隐式转换为二阶函数
我检查过这个,确实以下代码有效:
implicit def conv(c: (Int,Unit) …Run Code Online (Sandbox Code Playgroud) 我得到一个奇怪的编译器错误,关于实际存在的隐式但由于某种原因无法找到.所以我构建了一个小的测试用例,可以重现神秘的行为.
trait Hide {
type T
}
object HideString extends Hide {
override type T = String
}
object HideBool extends Hide {
override type T = Boolean
}
Run Code Online (Sandbox Code Playgroud)
简单类型用作隐式转换的明确目标.
def id[H <: Hide, C](x : C)(implicit ev : C => H#T) : H#T = ev(x)
def drop[H <: Hide, C](x : C)(implicit ev : C => H#T) : Int = {
println(ev(x))
1
}
def idSeq[H <: Hide, C](x : Seq[C])(implicit ev : Seq[C] => Seq[H#T]) : Seq[H#T] = …Run Code Online (Sandbox Code Playgroud) 我有一个方法调用一些其他方法,使用隐式类型Foo.我想在Foo中隐含f.我是这样做的:
def myMethod(a: Int, f: Foo) = {
implicit val implicitF = f;
somethingThatNeedsFoo(a)
}
Run Code Online (Sandbox Code Playgroud)
我不想f在myMethod的签名中标记隐式,因为Foos并不隐含在使用myMethod的上下文中.我的问题:与使用临时变量相比,是否有更惯用(或简洁)的方法来实现这种效果?
在Scala 2.12中导入global执行上下文然后在作用域中定义了另一个隐式执行上下文导致模糊隐式,而在2.11中它可以正常工作.
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
class A(implicit ec: ExecutionContext) {
x = implicitly[ExecutionContext]
}
Run Code Online (Sandbox Code Playgroud)
编译器给出错误:
error: ambiguous implicit values:
both lazy value global in object Implicits of type => scala.concurrent.ExecutionContext
and value ec in class A of type scala.concurrent.ExecutionContext
match expected type scala.concurrent.ExecutionContext
val x = implicitly[ExecutionContext]
^
Run Code Online (Sandbox Code Playgroud)
这是什么原因以及如何在代码中解决它?
我正在尝试编写通用的加权平均函数.我想放宽对值和重量相同类型的要求.即,我想支持说:(value:Float,weight:Int)和(value:Int,weight:Float)参数的序列而不仅仅是:(value:Int,weight:Int)
为此,我首先需要实现一个带有两个通用数值并返回其产品的函数.
def times[A: Numeric, B: Numeric](x: B, y: A): (A, B) : ??? = {...}
Run Code Online (Sandbox Code Playgroud)
编写签名并考虑返回类型,让我意识到我需要为Numerics定义某种层次结构来确定返回类型.即x:Float*y:Int=z:Float,x:Float*y:Double=z:Double.
现在,数字类定义操作plus,times等只为同一类型的参数.我想我需要实现一个类型:
class NumericConverter[Numeirc[A],Numeric[B]]{
type BiggerType=???
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以把我的时间函数写成:
def times[A: Numeric, B: Numeric](x: B, y: A): (A, B) :
NumericConverter[Numeirc[A],Numeric[B]].BiggerType= {...}
Run Code Online (Sandbox Code Playgroud)
并将"较小的类型"转换为"较大的类型"并将其转换为times().
我是在正确的轨道上吗?我将如何"实施" BiggerType?
显然我做不了类似的事情:
type myType = if(...) Int else Float
Run Code Online (Sandbox Code Playgroud)
因为它是动态评估的,所以它不起作用.
我知道我可以使用Scalaz等来做这个,但这是一个学术练习,我想了解如何编写一个静态返回基于参数类型的类型的函数.
如果有更简单的方法,请随时告诉我.
更新:
这就是我想出来的.
abstract class NumericsConvert[A: Numeric,B: Numeric]{
def AisBiggerThanB: Boolean …Run Code Online (Sandbox Code Playgroud)