在C++中是否隐式内联了以下自由函数,类似于如果在类定义中定义了成员函数如何隐式内联?
void func() { ... }
Run Code Online (Sandbox Code Playgroud)
模板函数的行为方式是否相同?
struct Base{
Base(Base &){} // suppress default constructor
};
struct Derived : Base{
};
int main(){
Derived d;
}
Run Code Online (Sandbox Code Playgroud)
显示的代码给出了错误,因为"Base"的默认构造函数(隐式)被抑制.事实上,该标准以12.1美元计价"If there is no user-declared constructor for class X, a default constructor is implicitly declared."
有三件事:
a)标准是否说任何地方如果用户声明构造函数存在于类中,则默认构造函数(隐式)被抑制.它基本上是上面的措辞否定或是否再次暗示:)?
b)为什么会那样?
c)为什么相同的规则不适用于默认的析构函数?
我从未对重载运算符做过任何大量的工作,尤其是隐式和显式转换.
但是,我有几个频繁使用的数字参数,所以我创建一个struct作为数字类型的包装,以强类型这些参数.这是一个示例实现:
public struct Parameter
{
private Byte _value;
public Byte Value { get { return _value; } }
public Parameter(Byte value)
{
_value = value;
}
// other methods (GetHashCode, Equals, ToString, etc)
public static implicit operator Byte(Parameter value)
{
return value._value;
}
public static implicit operator Parameter(Byte value)
{
return new Parameter(value);
}
public static explicit operator Int16(Parameter value)
{
return value._value;
}
public static explicit operator Parameter(Int16 value)
{
return new Parameter((Byte)value);
}
}
Run Code Online (Sandbox Code Playgroud)
当我试验我的测试实现以获得显式和隐式运算符的挂起时,我试图显式地转换Int64为我的 …
Java中有一个问题是我兄弟:
通过隐式转换原始数据类型,您可能会丢失精度并获得不正确的结果.
A,B,False
答案的关键是答:是的
我认为既不会失去精确度也不会得到不正确的结果.我知道显式转换可能会失去精度并得到不正确的结果,但不是隐式转换.
例如:
int i = 9;
short s = 3;
i = s; // implicit conversion, neither loose
//precision nor incorrect results
s = i; // compile error, do we call this implicit conversion?
//if yes, then the answer to question 3 is True,
//but I don't think this is an implicit conversion,
//so I think answer is false.
Run Code Online (Sandbox Code Playgroud)
作为注释中的州:
隐式类型转换:程序员不会尝试转换类型,而是在某些情况下系统会自动转换类型.
有人可以建议吗?
非常感谢.
我有一个非常简单的案例类,它是更大案例类的一部分.
case class PublisherStatus(status: String)
case class Publisher(id: Option[BSONObjectID], name: String, status: PublisherStatus, keywords: List[String], updatedAt: Option[DateTime], outputChannelIP: String, outputChannelName: String)
Run Code Online (Sandbox Code Playgroud)
我已经为它定义了BSON Reader和Writer,如下所示.
implicit object StatusBSONReader extends BSONDocumentReader[PublisherStatus] {
def read(doc: BSONDocument): PublisherStatus =
PublisherStatus(
doc.getAs[String]("status").get
)
}
implicit object StatusBSONWriter extends BSONDocumentWriter[PublisherStatus] {
def write(status: PublisherStatus): BSONDocument =
BSONDocument(
"status" -> status.status)
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试执行以下操作时,我收到编译错误.
def updateState(id: String, s: String) {
import models.PublisherBSONWriter._
implicit val statusWrites = Json.writes[PublisherStatus]
val objectId = new BSONObjectID(id)
val status = PublisherStatus(s)
val modifier = …Run Code Online (Sandbox Code Playgroud) 在Spark SQL中,我们有Row对象,其中包含组成一行的记录列表(想想Seq[Any]).A Row有序数访问器,如.getInt(0)或getString(2).
假设序号0 = ID且序数1 =名称.很难记住顺序是什么,使代码混乱.
比方说,我有以下代码
def doStuff(row: Row) = {
//extract some items from the row into a tuple;
(row.getInt(0), row.getString(1)) //tuple of ID, Name
}
Run Code Online (Sandbox Code Playgroud)
问题是如何在Row对象中为这些字段创建别名?
我在想我可以创建一个隐含Row对象的方法;
def id(implicit row: Row) = row.getInt(0)
def name(implicit row: Row) = row.getString(1)
Run Code Online (Sandbox Code Playgroud)
然后我可以重写上面的内容;
def doStuff(implicit row: Row) = {
//extract some items from the row into a tuple;
(id, name) //tuple of ID, Name
}
Run Code Online (Sandbox Code Playgroud)
有更好/更整洁的方法吗?
通常,我首先在同一个文件中写一个case class然后是同伴object,就在下面.但是当试图导入implicit同伴中的声明时,我被迫切换声明的顺序(显然我不想要).克服这种情况的建议做法是什么?
对于具体情况,以下代码不起作用:
object SomeLib {
def doSomething[T : List](t: T) = "meh"
}
case class FooWorker(x: String) {
import FooWorker._ // bring the implicit in scope for doSomething
def then(event: String) : FooWorker = {
copy(x = SomeLib.doSomething(event)) // requires implicit
}
}
object FooWorker {
implicit val list = List("a", "b", "c")
}
Run Code Online (Sandbox Code Playgroud)
但如果我object FooWorker在case class FooWorker它确实工作之前宣布.我正在使用Scala 2.11.6和SBT进行测试.非常感谢!
对于我的示例,我将使用Options,但在我的实际代码中,我使用的是自定义数据类型.添加导入import cats.std.option._将解决示例中的问题.我有一些看起来像这样的代码:
import cats.{FlatMap, Eval, Applicative}
import cats.data.Kleisli
import cats.syntax.all._
implicit val optionApplicative = new Applicative[Option] {
override def pure[A](x: A): Option[A] = Option(x)
override def ap[A, B](fa: Option[A])(f: Option[(A) => B]): Option[B] = for {
a <- fa
fUnwrapped <- f
} yield fUnwrapped(a)
}
val test : Option[(Int, String)] = (Option(4) |@| Option("name")).map((_, _))
Run Code Online (Sandbox Code Playgroud)
这段代码编译运行得很好.
接下来我曾经Kleisli写过一些返回的函数Option:
val test2 : List[Int] => Option[Int] = {
val f = (xs : List[Int]) …Run Code Online (Sandbox Code Playgroud) 具有如下定义的隐式值和函数
implicit val v = 0
def function(implicit v: Int): Map[String, String] = Map("key" -> "value")
Run Code Online (Sandbox Code Playgroud)
我可以
function.get("key") // res0: Option[String] = Some(value)
function(v)("key") // res0: String = value
Run Code Online (Sandbox Code Playgroud)
但以下不编译
function("key")
Run Code Online (Sandbox Code Playgroud)
那么如何使用括号访问地图并传递隐式参数呢?
我想在包装函数中定义隐式值,并将其提供给内部函数使用,到目前为止,我设法通过从包装器传递隐式变量来做到这一点:
case class B()
trait Helper {
def withImplicit[A]()(block: => A): A = {
implicit val b: B = B()
block
}
}
class Test extends Helper {
def useImplicit()(implicit b: B): Unit = {...}
def test = {
withImplicit() { implicit b: B =>
useImplicit()
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能避免implicit b: B =>,并implicit val b: B = B()提供给内部功能块?
implicit ×10
scala ×6
c++ ×2
apache-spark ×1
c# ×1
c#-4.0 ×1
case-class ×1
constructor ×1
destructor ×1
dictionary ×1
dotty ×1
explicit ×1
function ×1
inline ×1
java ×1
scala-cats ×1
templates ×1