我需要一些帮助.我正在创建一个像这样的SelectItem类:
public class SelectItem<T> where T : class
{
public bool IsChecked { get; set; }
public T Item { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望以下代码有效
SelectItem<String> obj = new SelectItem<String> { Item = "Value" };
obj.IsChecked = true;
String objValue = obj;
Run Code Online (Sandbox Code Playgroud)
而不是必须这样做:
String objValue = obj.Item;
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
当我的程序包含两个文件时:
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)
任何人都能解释这种奇怪的行为吗?
此代码通过一组路径中唯一的基本名称存根的哈希键来编译集合.
%stubs = map { $f=basename $_; $f =~ /^([A-Za-z]+[0-9]+)\./ ; $1=>() } @pathlist;
Run Code Online (Sandbox Code Playgroud)
为什么我需要$f这里的参考?我以为我会好的:
%stubs = map { basename; /^([A-Za-z]+[0-9]+)\./; $1=>() } @pathlist;
Run Code Online (Sandbox Code Playgroud)
但我得不到匹配.我不允许在地图块中修改$ _吗?
对于那些想知道代码在做什么的人:
对于每个$ path(@pathlist),它获取基本名称,匹配第一个字母数字序列,然后返回第一个括号匹配作为空列表值的键.例:
/some/dir/foo123.adfjijoijb
/some/dir/foo123.oibhobihe
/some/dir/bar789.popjpoj
Run Code Online (Sandbox Code Playgroud)
回报
foo123 => ()
bar789 => ()
Run Code Online (Sandbox Code Playgroud)
之后我使用地图的键作为一组值进行处理.
我使用scala中的play框架创建了代码,如下所示:
object Application extends Controller {
def hoge = Action( implicit request =>
val username = MyCookie.getName.get
Ok("hello " + username)
}
}
object MyCookie {
def getName( implicit request: RequestHeader ) = {
request.cookies.get("name").map(_.value)
}
}
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) 如果我定义一个简单的stringToInt函数并将其存储为val,那么一切都按预期工作,例如
scala> def stringToInt1: (String => Int) = _.toInt
stringToInt1: String => Int
scala> stringToInt1("1")
res0: Int = 1
Run Code Online (Sandbox Code Playgroud)
但是,如果我然后隐藏它,它会导致堆栈溢出:
scala> implicit def stringToInt2: (String => Int) = _.toInt
stringToInt2: String => Int
scala> stringToInt2("1")
java.lang.StackOverflowError
at .stringToInt2(<console>:7)
at $anonfun$stringToInt2$1.apply(<console>:7)
at $anonfun$stringToInt2$1.apply(<console>:7)
...
Run Code Online (Sandbox Code Playgroud)
起初我怀疑这是因为下划线没有解决我的预期,但情况并非如此,因为这种隐式val的样式适用于以下简单函数:
scala> implicit def plusTwo: (Int => Int) = _ + 2
plusTwo: Int => Int
scala> plusTwo(2)
res2: Int = 4
Run Code Online (Sandbox Code Playgroud)
如果我显式定义参数,没有堆栈溢出:
scala> implicit def stringToInt3(s: String) = s.toInt
stringToInt3: (s: String)Int
scala> stringToInt3("1")
res3: Int …Run Code Online (Sandbox Code Playgroud) 给出一些方法
def f[A,B](p: A)(implicit a: X[A,B], b: Y[B])
Run Code Online (Sandbox Code Playgroud)
隐式参数列表中的abefore 的顺序b对类型推断是否重要?
我认为只有不同参数列表中的参数放置很重要,例如类型信息仅从左到右通过参数列表流动.
我问,因为我注意到在单隐式列表中更改隐式参数的顺序使得我的程序编译.
以下代码使用:
这是一个简单的sbt构建文件,以帮助编译示例:
scalaVersion := "2.11.5"
libraryDependencies += "com.chuusai" %% "shapeless" % "2.1.0"
scalaSource in Compile := baseDirectory.value
Run Code Online (Sandbox Code Playgroud)
在这个例子上.此代码编译:
import shapeless._
import shapeless.ops.hlist.Comapped
class Foo {
trait NN
trait Node[X] extends NN
object Computation {
def foo[LN <: HList, N <: HList, TupN <: Product, FunDT]
(dependencies: TupN)
(computation: FunDT)
(implicit tupToHlist: Generic.Aux[TupN, LN], unwrap: Comapped.Aux[LN, Node, N]) = ???
// (implicit …Run Code Online (Sandbox Code Playgroud) 鉴于以下路线
val route1: PathMatcher[Unit] = PathMatcher("app")
val route2: PathMatcher1[String] = PathMatchers.Segment
val route3: PathMatcher[Unit] = PathMatcher("lastSegment")
Run Code Online (Sandbox Code Playgroud)
我可以轻松定义
val resultingRoute: PathMatcher[Tuple1[String]] = route1 / route2 / route3
Run Code Online (Sandbox Code Playgroud)
获得预期的类型(PathMatcher [Tuple [String]]).
但是以编程方式创建路线
val routeDef = List(route1, route2, route3)
val resultingRoute = routeDef.reduce((a,b) => a / b)
Run Code Online (Sandbox Code Playgroud)
不会编译,给我
找不到参数连接的隐含值:akka.http.scaladsl.server.util.TupleOps.Join [_1,_1]
此外,推断出的resultRoute类型是
PathMatcher[_ >: Unit with Tuple1[String] with join.Out]
Run Code Online (Sandbox Code Playgroud)
我真的很感激有任何提示给我一些迹象,说明我在这里做错了什么,或者如何解决这个问题.
为了完整性,这是我的导入:
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{PathMatcher, _}
Run Code Online (Sandbox Code Playgroud)
非常感谢!
(我对Scala还是陌生的,希望这不是一个愚蠢的问题。)
据我所知,为函数声明参数implicit有两种(相关但完全不同)的用法:
当编译器可以找到唯一合适的值(在调用范围内)传递时,它使在调用给定函数时显式地传递相应的参数成为可选操作。
它使参数本身成为传递给具有隐式参数的其他函数的合适值(当从给定函数中调用它们时)。
在代码中:
def someFunction(implicit someParameter: SomeClass) = { // Note `implicit`
...
// Note no argument supplied in following call;
// possible thanks to the combination of
// `implicit` in `someOtherFunction` (1) and
// `implicit` in line 1 above (2)
someOtherFunction
...
}
def someOtherFunction(implicit someOtherParameter: SomeClass) = {
...
}
implicit val someValue = new SomeClass(...)
// Note no argument supplied in following call; …Run Code Online (Sandbox Code Playgroud)