小编and*_*ask的帖子

显式使用LambdaMetafactory

我正在尝试显式使用LambdaMetafactory.metafactory,我无法理解为什么它仅适用于Runnable功能接口.例如,此代码执行预期的操作(它打印"hello world"):

public class MetafactoryTest {

    public static void main(String[] args) throws Throwable {

        MethodHandles.Lookup caller = MethodHandles.lookup();
        MethodType methodType = MethodType.methodType(void.class);
        MethodType invokedType = MethodType.methodType(Runnable.class);
        CallSite site = LambdaMetafactory.metafactory(caller, 
                                                      "run", 
                                                      invokedType, 
                                                      methodType, 
                                                      caller.findStatic(MetafactoryTest.class, "print", methodType), 
                                                      methodType);
        MethodHandle factory = site.getTarget();
        Runnable r = (Runnable) factory.invoke();
        r.run();
    }

    private static void print() {
        System.out.println("hello world"); 
    }    
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用不同的功能界面时会出现问题,例如Supplier.以下代码不起作用:

public class MetafactoryTest {

    public static void main(String[] args) throws Throwable {

        MethodHandles.Lookup caller = MethodHandles.lookup();
        MethodType methodType = MethodType.methodType(String.class);
        MethodType invokedType …
Run Code Online (Sandbox Code Playgroud)

java lambda metaprogramming java-8

19
推荐指数
1
解决办法
6161
查看次数

Haskell:非IO monad中的异常处理

我开始使用Yesod开发一个小项目,这是我第一次使用Haskell做一些真实的事情.这个处理注册表单的代码工作正常:

postRegisterR :: Handler ()
postRegisterR = do email <- runInputPost $ ireq textField "email"
                   user  <- runInputPost $ ireq textField "user"
                   pwd   <- runInputPost $ ireq textField "pwd"
                   cpwd  <- runInputPost $ ireq textField "cpwd"
                   if pwd == cpwd && isValidEmail email
                      then do
                        tryInsert email user pwd
                        setSession "user" user
                        redirectUltDest SessionR
                      else do
                        redirect HomeR

tryInsert :: Text -> Text -> Text -> Handler ()
tryInsert email user pwd = do pwdbs <- liftIO $ hashedPwd pwd …
Run Code Online (Sandbox Code Playgroud)

monads haskell exception-handling yesod

6
推荐指数
2
解决办法
779
查看次数