当我的程序包含两个文件时:
main.c中
#include <stdio.h>
int main(void) {
printf("%lf\n",f());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
func.c
double f(int a) {
return 1;
}
Run Code Online (Sandbox Code Playgroud)
编译器不显示任何错误.
当我的程序只包含一个文件时:
main.c中
#include <stdio.h>
int main(void) {
printf("%lf\n",f());
return 0;
}
double f(int a) {
return 1;
}
Run Code Online (Sandbox Code Playgroud)
Visual C++ 2008编译器显示以下错误:
Error 2 error C2371: 'f' : redefinition; different basic types d:\temp\projects\function1\function1\1.c 8 function1
Run Code Online (Sandbox Code Playgroud)
任何人都能解释这种奇怪的行为吗?
我们经常需要传递代码上下文信息,例如执行操作的用户.我们将此上下文用于授权检查等各种事务.在这些情况下,隐含值可以证明对减少锅炉板代码非常有用.
假设我们传递了一个简单的执行上下文: case class EC(initiatingUser:User)
我们可以有方便的守卫:
def onlyAdmins(f: => T)(implicit context:EC) = context match{
case EC(u) if(u.roles.contain(Role.ADMIN)) => f
case _ => throw new UnauthorizedException("Only admins can perform this action")
}
val result = onlyAdmins{
//do something adminy
}
Run Code Online (Sandbox Code Playgroud)
我最近发现自己需要在与Akka演员合作时这样做,但他们使用模式匹配,我还没有找到一个很好的方法来使implicits与提取器一起工作.
首先,您需要使用每个命令传递上下文,但这很简单:
case class DeleteCommand(entityId:Long)(implicit executionContext:EC)
//note that you need to overwrite unapply to extract that context
Run Code Online (Sandbox Code Playgroud)
但是接收函数看起来像这样:
class MyActor extends Actor{
def receive = {
case DeleteCommand(entityId, context) => {
implicit val c = context
sender ! onlyAdmins{
//do something …Run Code Online (Sandbox Code Playgroud) 在开发DSL时,限制隐式变量范围的最简洁方法是什么,同时隐藏这样一个隐式变量定义的事实?
举个例子,这是理想的行为......
object External
{
def funNeedingValue(implicit a : String)
{
println(a)
}
}
object Main extends App
{
useValue("Hi") {
// Implicit string "Hi" is only defined in this block
External.funNeedingValue // Prints "Hi"
}
External.funNeedingValue // Compilation error: No implicit String defined
}
Run Code Online (Sandbox Code Playgroud)
以下是关闭,但没有所有所需的属性......
// The following works, but does not hide the fact that there is an implicit
// variable defined.
object Main extends App
{
{
implicit val implicitValue = "Hi"
External.funNeedingValue // Prints "Hi"
} …Run Code Online (Sandbox Code Playgroud) 我在使用复杂对象作为Json.format上另一个对象的属性时遇到错误.
我有两个类:RoleDTO和EmailInvitationDTO.EmailInvitationDTO有一个RoleDTO.所以,我宣布:
case class RoleDTO(id:Option[Long] = None, roleType:Int, userID:Long, fromHousingUnitID:Option[Long] = None, isAdmin:Option[Boolean] = None, fromResidentUserID:Option[Long] = None, documentNumber:Option[String] = None, fromCondoID:Option[Long] = None)
object RoleDTO { val roleFormat = Json.format[RoleDTO] }
case class EmailInvitationDTO(firstName:String, lastName:String, email:String, role:RoleDTO)
object EmailInvitationDTO{ val emailInvitationFormat = Json.format[EmailInvitationDTO] }
Run Code Online (Sandbox Code Playgroud)
我收到错误:没有可用的RoleDTO隐式格式.即使我宣布roleFormat中前行变量emailInvitationFormat:
object EmailInvitationDTO {
val roleFormat = Json.format[RoleDTO]
val emailInvitationFormat = Json.format[EmailInvitationDTO]
}
Run Code Online (Sandbox Code Playgroud)
谁知道缺少什么?谢谢.
有人可以向我解释为什么我在这里遇到“发散的隐式扩展错误”吗?我认为这与类型同义词有关,type MyIdType = String但我不确定为什么。
import org.scalacheck.Arbitrary
import org.scalacheck.Arbitrary._
import org.scalacheck.Gen
def arbitraryCaseClass[A,C](f: A => C)(implicit t: Arbitrary[A]): Arbitrary[C] = Arbitrary(for(v <- arbitrary[A]) yield f(v))
type MyIdType = String
implicit val arbMyIdType: Arbitrary[MyIdType] = Arbitrary(Gen.identifier)
case class Foo(s: MyIdType, t: Int)
implicit def arbA = arbitraryCaseClass((Foo.apply _).tupled)
val foo = arbitrary[Foo].sample
Run Code Online (Sandbox Code Playgroud)
错误:
Error:(13, 40) diverging implicit expansion for type org.scalacheck.Arbitrary[(A$A6.this.MyIdType, Int)]
starting with method arbTuple2 in trait ArbitraryArities
implicit def arbA = arbitraryCaseClass((Foo.apply _).tupled)
Run Code Online (Sandbox Code Playgroud) 我想Iterable用一些自定义代码增强所有s.为此,我写了以下内容:
implicit class RichIterable[A, B <: Iterable[A]](b: B) {
def nonEmptyOpt: Option[B] = if (b.nonEmpty) Some(b) else None
}
Run Code Online (Sandbox Code Playgroud)
现在,当我想用这种方法在List那绝对是的一个子类Iterable,像这样
List(1, 2, 3).nonEmptyOpt
Run Code Online (Sandbox Code Playgroud)
我明白了
value nonEmptyOpt is not a member of List[Int]
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我只是看了一下List.flatMap宣言,对此感到有些惊讶.
final override def flatMap[B, That](f: A => GenTraversableOnce[B])
(implicit bf: CanBuildFrom[List[A], B, That]): That
Run Code Online (Sandbox Code Playgroud)
在哪里object List定义:
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, List[A]] =
ReusableCBF.asInstanceOf[GenericCanBuildFrom[A]]
Run Code Online (Sandbox Code Playgroud)
因此,如果我们调用flatMapa,List我们将获得List并且我没有看到That类型中的任何点,如果它总是被推断为List[B](因为implicit).
所提供的代码片段是一个虚构的简约示例,仅用于说明问题,与实际的业务逻辑类型无关。
在下面的代码中,我们在Entry类型内部有一个嵌套Registry类型。
class Registry[T](name: String) {
case class Entry(id: Long, value: T)
}
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为不同注册表的条目是不同的,不可比拟的类型。
然后,我们可能有一个隐式的Ops类,例如,在测试中使用的类,它将我们的注册表绑定到某些测试存储实现(一个简单的可变映射)
object testOps {
import scala.collection.mutable
type TestStorage[T] = mutable.Map[Long, T]
implicit class RegistryOps[T](val self: Registry[T])(
implicit storage: TestStorage[T]
) {
def getById(id: Long): Option[self.Entry] =
storage.get(id).map(self.Entry(id, _))
def remove(entry: self.Entry): Unit = storage - entry.id
}
}
Run Code Online (Sandbox Code Playgroud)
的问题是:所述Entry包装物被视为无法比拟的类型到原来的注册表对象consructed内行动
object problem {
case class Item(name: String)
val items = new Registry[Item]("elems")
import testOps._
implicit val storage: TestStorage[Item] =
scala.collection.mutable.Map[Long, …Run Code Online (Sandbox Code Playgroud) implicit ×10
scala ×8
akka ×1
c ×1
collections ×1
debugging ×1
declaration ×1
evaluation ×1
function ×1
json ×1
pass-by-name ×1
rider ×1
scala-2.12 ×1
scalacheck ×1
scope ×1