相关疑难解决方法(0)

为什么不将异常用作常规控制流?

为了避免我可以用Google搜索的所有标准答案,我将提供一个你可以随意攻击的例子.

C#和Java(和太多的人)有很多类型的一些"溢出"的行为,我不都不像(如type.MaxValue + type.SmallestValue == type.MinValue例如: int.MaxValue + 1 == int.MinValue).

但是,看到我的恶性,我会通过扩展这种行为来增加对这种伤害的一些侮辱,让我们说一个Overridden DateTime类型.(我知道DateTime密封在.NET中,但是为了这个例子,我使用的是一种与C#完全相同的伪语言,除了DateTime没有密封的事实).

被覆盖的Add方法:

/// <summary>
/// Increments this date with a timespan, but loops when
/// the maximum value for datetime is exceeded.
/// </summary>
/// <param name="ts">The timespan to (try to) add</param>
/// <returns>The Date, incremented with the given timespan. 
/// If DateTime.MaxValue is exceeded, the sum wil 'overflow' and 
/// continue from DateTime.MinValue. 
/// </returns>
public DateTime override Add(TimeSpan …
Run Code Online (Sandbox Code Playgroud)

language-agnostic exception

184
推荐指数
12
解决办法
4万
查看次数

使用带有Scala的Eithers"for"语法

据我了解,Scala"for"语法与Haskell的monadic"do"语法非常相似.在Scala中,"for"语法通常用于Lists和Options.我想将它与Eithers 一起使用,但默认导入中不存在必要的方法.

for {
  foo <- Right(1)
  bar <- Left("nope")
} yield (foo + bar)

// expected result: Left("nope")
// instead I get "error: value flatMap is not a member..."
Run Code Online (Sandbox Code Playgroud)

这个功能是通过一些导入提供的吗?

有一个轻微的障碍:

for {
  foo <- Right(1)
  if foo > 3
} yield foo
// expected result: Left(???)
Run Code Online (Sandbox Code Playgroud)

对于列表,它将是List().因为Option,它会None.Scala标准库是否为此提供了解决方案?(或许scalaz?)怎么样?假设我想为Either提供我自己的"monad实例",我怎么能这样做?

monads scala typeclass either for-comprehension

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