小编Jac*_*ang的帖子

使用可扩展析取(并集)类型的错误处理(作为Either的左侧)?

我目前正在集思广益,研究在scala中进行显式错误处理的最佳方法.

理想的最终产品:

  • 编译器强制您已检查所有(明确声明的)错误
  • 尽可能少的样板
  • 没有或几乎没有额外的运行时成本(这意味着没有来自所有函数的所有可能错误的父类型)
  • 允许您在陈述中明确和隐含

基本上,我想在steriods上检查异常.

忽略异常抛出,处理这种情况的典型方法是使用Either类型(或\/来自我正在使用的Scalaz),并将left侧面作为包含所有可能错误的ADT,如下所示:

sealed trait Error_parseAndValidate
case class ParseError(msg: String) extends Error_parseAndValidate
case class ValidateError(msg: String) extends Error_parseAndValidate

def parseAndValidate(str: String): Error_parseAndValidate \/ Int = {
    // can return ParseError or ValidateError
}
Run Code Online (Sandbox Code Playgroud)

但是,如果函数调用嵌套多个级别,这将变得非常繁琐:

考虑具有以下调用堆栈的此DB示例

main- > getResultWithString- >parseAndValidate / fetchResultFromDB

// error type ADT containing expected errors in parseAndValidate
// similarly for other error_* traits
sealed trait Error_parseAndValidate
case class ParseError(msg: String) extends …
Run Code Online (Sandbox Code Playgroud)

error-handling macros scala shapeless

6
推荐指数
0
解决办法
444
查看次数

模式匹配中的错误"此值的类型必须在此上下文中已知"

请考虑以下示例

fn main () {
    f("hello", true);
}

fn f(str: &str, sen: bool) {
    let s: &str = match sen {
        false => str,
        true => str.chars().map(|x| x.to_lowercase()).collect().as_slice()
    };
    println!("{}", s);
}
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误

error: the type of this value must be known in this conntext                                                                                                                                                              
true => str.chars().map(|x| x.to_lowercase()).collect().as_slice()
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

我有点困惑,编译器是否知道类型str&str来自函数定义?我在这里错过了什么?

rust

5
推荐指数
1
解决办法
1716
查看次数

用-hy-GHC堆分析-什么是*(星号)?

当我使用-hy标志来分析程序的堆使用情况时

./prog +RTS -hy
Run Code Online (Sandbox Code Playgroud)

我经常*在结果中看到构造函数,以及其他构造函数,例如[]Word8

*在这种情况下是什么类型?有关系kinds吗?

optimization profiling haskell

4
推荐指数
1
解决办法
212
查看次数

Scalatest Matcher - 检查一组值中是否存在单个值

我正在生成一个值,并且我知道它可能的值。我想写这个

val myInt = someFunction()
myInt shouldBe oneOf (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

然而,从 Scalatest 3 M15 开始,这似乎对我不起作用。我的解决方法是

List(myValue) should contain atMostOneOf (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

这对于阅读和理解来说更加令人困惑。

有办法在这里做我想做的事吗?这似乎是一个常见的场景。

scala scalatest

3
推荐指数
1
解决办法
819
查看次数

管道 - 将多个来源/生产者合二为一

我正在使用文件读取sourceFile,但我还需要在处理操作中引入随机性.我认为最好的方法是拥有一个类型的生产者

Producer m (StdGen, ByteString)
Run Code Online (Sandbox Code Playgroud)

其中StdGen用于生成随机数.

我打算让生产者执行sourceFile的任务,并且每次向下游发送数据时都会产生一个新的种子.

我的问题是,似乎没有像zipSink水槽这样的源组合器.通过Conduit Overview阅读,它似乎暗示你可以嵌入一个Source内部Conduit,但我没有看到它是如何在示例中完成的.

任何人都可以提供一个示例,将两个或多个IO源融合为一个Producer/ Source

编辑:

一个例子:

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE OverloadedStrings #-}

import System.Random (StdGen(..), split, newStdGen, randomR)
import ClassyPrelude.Conduit as Prelude
import Control.Monad.Trans.Resource (runResourceT, ResourceT(..))
import qualified Data.ByteString as BS

-- generate a infinite source of random number seeds
sourceStdGen :: MonadIO m => Source m StdGen
sourceStdGen = do
    g <- liftIO …
Run Code Online (Sandbox Code Playgroud)

haskell conduit

2
推荐指数
1
解决办法
595
查看次数

通过值从原始类型引用复制的惯用方法是什么?

请考虑以下代码段:

fn example(current_items: Vec<usize>, mut all_items: Vec<i32>) {
    for i in current_items.iter() {
        let mut result = all_items.get_mut(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨i&mut usize不是usize:

error[E0277]: the trait bound `&usize: std::slice::SliceIndex<[()]>` is not satisfied
 --> src/lib.rs:3:36
  |
3 |         let mut result = all_items.get_mut(i);
  |                                    ^^^^^^^ slice indices are of type `usize` or ranges of `usize`
  |
  = help: the trait `std::slice::SliceIndex<[()]>` is not implemented for `&usize`
Run Code Online (Sandbox Code Playgroud)

我已经挖掘了文档但是我认为满足编译器的唯一方法是i.clone().

我肯定错过了一些明显的东西.从值原始类型引用复制的惯用方法是什么?

rust

1
推荐指数
2
解决办法
3091
查看次数