我想在协作actor的系统中隐式地传播请求上下文.
为了简化和呈现这种情况,我的系统有多个actor,传递给这些actor的消息需要包含这个RequestContext对象.
ActorA接收MessageA类型的消息,ActorB接收MessageB类型的消息
当ActorA需要向ActorB发送消息时,作为MessageA处理的一部分,它执行业务逻辑,然后根据逻辑结果以及MessageA中可用的RequestContext构造MessageB,然后将其发送给ActorB
def handle(ma:MessageA) {
val intermediateResult = businessLogic(ma)
actorB ! MessageB(intermediateResult, ma.requestContext)
}
Run Code Online (Sandbox Code Playgroud)
我们有一大堆要处理的消息,并且明确地传递requestContext是很麻烦的.
我正在尝试使用Scala的implicits功能创建方法,以避免将传入消息中嵌入的RequestContext显式注入传出消息.
消息是案例类(它们需要).我已经阅读了有关implicits规则的内容,但是将对象的属性带入当前的隐式范围似乎很牵强.
我相信这应该是一个共同的要求.有什么建议 ?
谢谢.
以下是我真正问题的简化版本:
class Z[T]
object E extends Enumeration {
implicit val z = new Z[Value]
val X, Y = Value
}
implicit def f[T : Z] = (getter: T) => 0
implicit def o[T](v: Option[T])(implicit toInt: T => Int) = 0
def e: Option[E.Value] = null
val b: Int = e
Run Code Online (Sandbox Code Playgroud)
这有效,b隐式转换为o(e)(f(Ez)).但随着以下小变化:
implicit def f[T : Z] = (setter: T => Unit) => 0
implicit def o[T](v: Option[T])(implicit toInt: (T => Unit) => Int) = 0
Run Code Online (Sandbox Code Playgroud)
它找不到合适的隐含值Ez,尽管与原始代码没有本质区别,而手动显式转换为o(e)(f(Ez))仍然有效.
我知道隐式参数的实现还没有完成,还有很多未解决的问题.如果这是其中之一,我想向Scala贡献者报告.所以我的问题是,a)这真的是一个错误吗?b)如果是这样,我在哪里以及如何提交错误以便将来修复?
UPDATE
特拉维斯的回答就像一个魅力!顺便说一下,上面的代码解决了我原来的问题:
implicit …Run Code Online (Sandbox Code Playgroud) 据我所知,根据此图表,从VARBINARY到DATE的隐式转换应该没问题.但是,当我尝试以下代码时,SQL Server会响应"不允许从数据类型varbinary(max)到日期的隐式转换.使用CONVERT函数来运行此查询."
CREATE TABLE [dbo].[CONVERSION_TEST](
[mydate] [date] NULL
) ON [PRIMARY]
GO
declare @bin varbinary(MAX) = NULL
insert into conversion_test values (@bin)
Run Code Online (Sandbox Code Playgroud)
我在SQL Server 2012(版本11.0.3128)上运行它.
我有一些C代码,它完全是这样的:
short int fun16(void){
short int a = 2;
short int b = 2;
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用GCC编译它时,我收到警告:
warning: conversion to 'short int' from 'int' may alter its value [-Wconversion]
return a+b;
^
Run Code Online (Sandbox Code Playgroud)
虽然没有明显的转换.两个操作数都很短,甚至返回值都很短.那么,有什么收获?
我们考虑一下这段代码:
class A
object A{
implicit def A2Int(implicit a:A)=1
implicit def A2String(a:A)="Hello"
}
object Run extends App{
implicit val a: A =new A
import A.A2Int
// without this import this code does not compile, why ?
// why is no import needed for A2String then ?
def iWantInt(implicit i:Int)=println(i)
def iWantString(implicit s:String)=println(s)
iWantInt
iWantString(a)
}
Run Code Online (Sandbox Code Playgroud)
它运行和打印:
1
Hello
Run Code Online (Sandbox Code Playgroud)
现在,如果我们注释掉这条线
import A.A2Int
Run Code Online (Sandbox Code Playgroud)
然后我们得到一个编译错误:

随着该系列的注释,为什么Scala无法找到A.A2String它是否能找到A.A2Int?
如何解决这个问题?
谢谢阅读.
我正在尝试以前在不同的上下文中以不同的形式看到的东西:扩展scala的查询扩展 filterById(id: Id)
这就是我尝试过的:
trait TableWithId { self: Profile =>
import profile.simple._
trait HasId[Id] { self: Table[_] =>
def id: Column[Id]
}
implicit class HasIdQueryExt[Id: BaseColumnType, U]
(query: Query[Table[U] with HasId[Id], U]) {
def filterById(id: Id)(implicit s: Session) = query.filter(_.id === id)
def insertReturnId(m: U)(implicit s: Session): Id = query.returning(query.map(_.id)) += m
}
}
Run Code Online (Sandbox Code Playgroud)
这很好,没有真正的魔法.但是因为Table类型没有类型约束,所以我应用filterById的任何查询都会失去它的特异性(现在是通用的Table with HasId[Id]),而我无法再访问它的列(当然除外_.id).
我不知道如何键入这种隐式转换,以防止这种情况发生.可能吗?以下"naieve"解决方案不起作用,因为Scala现在为Id类型推断Nothing:
implicit class HasIdQueryExt[Id: BaseColumnType, U, T <: Table[U] with HasId[Id]]
(query: Query[T, U]) { ... }
Run Code Online (Sandbox Code Playgroud)
我发现突然将Id类型推断为Nothing有点奇怪.如何提示编译器在哪里查找该Id类型?
我使用clang ++来编译程序,我需要在clang ++中编译它而没有错误.我没有得到其他编译器的错误.
代码中的错误行是
memset(grid_, 0, sizeof(int) * x_quadrants * y_quadrants);
Run Code Online (Sandbox Code Playgroud)
整个功能是这样的:
Ocean::Ocean(int num_boats, int x_quadrants, int y_quadrants)
{
grid_ = new int[x_quadrants * y_quadrants];
memset(grid_, 0, sizeof(int) * x_quadrants * y_quadrants);
x_quadrants_ = x_quadrants;
y_quadrants_ = y_quadrants;
boats_ = new Boat[num_boats];
num_boats_ = num_boats;
stats_.hits = 0;
stats_.misses = 0;
stats_.duplicates = 0;
stats_.sunk = 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用memset所以当使用不同的驱动程序进行测试时,我不会得到垃圾值输出.没有必要为clang提供命令行,因为我不允许更改它.
我看到了std :: lower_bound()和std :: upper_bound()语法中的不一致(好吧,类型转换,真的),并且想知道是否有人可以解释一下吗?根据评论,第2行尽管与第1行明显相似,但不会编译; 你需要使用第3行显示的表格(至少在gcc 4.7.3/ubuntu 64位上 - 这就是我必须要玩的)
#include <set>
#include <algorithm>
using namespace std;
class MyInt {
private:
int val;
public:
MyInt(int _val): val(_val) {}
bool operator<(const MyInt& other) const {return val < other.val;}
};
int main() {
set<MyInt> s;
s.insert(1); // demonstrate implicit conversion works
s.insert(MyInt(2));
s.insert(3); // one last one for the road
set<MyInt>::iterator itL = lower_bound(s.begin(), s.end(), 2); //LINE 1
// the line below will NOT compile
set<MyInt>::iterator itU = upper_bound(s.begin(), s.end(), 2); //LINE …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释如何
printf("%d", -2<2u?1:-1);
Run Code Online (Sandbox Code Playgroud)
打印出'-1'.我假设有某种隐式转换正在进行,但我似乎无法掌握它.
我在理解C++使用隐式转换的条件时遇到了一些麻烦.假设我有一个班级:
class SomeClass
{
private:
int val;
public:
SomeClass(int i) :val{ i } {}
operator string() { return to_string(val); }
};
Run Code Online (Sandbox Code Playgroud)
为什么在将此类与运算符一起使用时需要转换为字符串?为什么不隐式执行转换呢?
码:
int main(void)
{
SomeClass sc = 4;
cout << (string)sc << endl; //prints 4, compiles fine
cout << sc << endl;//does not compile, no operator "<<" matches these operands
string str1 = sc;//compiles fine, performs cast
string str2 = "Base String" + sc;//does not compile, no operator "+" matches these operands
}
Run Code Online (Sandbox Code Playgroud)
这个问题更具学术性而非实际性,因为无论如何使用演员表更具可读性.
scala ×4
c++ ×3
c ×2
actor ×1
akka ×1
gcc ×1
generics ×1
implicit ×1
implicits ×1
lower-bound ×1
memset ×1
operators ×1
slick ×1
sql-server ×1
stl ×1
unsigned ×1
upperbound ×1
visual-c++ ×1