正确地将Maybe a转换为Elm的正确方法,无所不能 [英] Right way to forcibly convert Maybe a to a in Elm, failing clearly for Nothings

查看:130
本文介绍了正确地将Maybe a转换为Elm的正确方法,无所不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我真正想做的是,我有一个数组,我想从中选择一个随机元素.显而易见的事情是从一个随机数生成器获取一个介于0和长度减1之间的整数,该整数我已经在使用它了,然后应用软件包函数执行相同的操作.)来自Haskell,我得到的类型意义是它可以保护我免受索引超出范围的情况的影响,但是我可以控制索引,并且不希望这样做发生,所以我只想假设我得到了Just内容,并在某种程度上被强制转换为a.在Haskell中,这将是fromJust,或者,如果我感到冗长,则fromMaybe (error "some message"). 我应该如何在榆木中做到这一点?

Okay, what I really wanted to do is, I have an Array and I want to choose a random element from it. The obvious thing to do is get an integer from a random number generator between 0 and the length minus 1, which I have working already, and then applying Array.get, but that returns a Maybe a. (It appears there's also a package function that does the same thing.) Coming from Haskell, I get the type significance that it's protecting me from the case where my index was out of range, but I have control over the index and don't expect that to happen, so I'd just like to assume I got a Just something and somewhat forcibly convert to a. In Haskell this would be fromJust or, if I was feeling verbose, fromMaybe (error "some message"). How should I do this in Elm?

我在邮件列表上发现了一个讨论讨论这个问题,但是已经有一段时间了,我没有在讨论建议的标准库中看到我想要的功能.

I found a discussion on the mailing list that seems to be discussing this, but it's been a while and I don't see the function I want in the standard library where the discussion suggests it would be.

这是我到目前为止发现的一些非常不令人满意的潜在解决方案:

Here are some pretty unsatisfying potential solutions I found so far:

  • 只需使用 withDefault .我确实有默认值a可用,但是我不喜欢这样,因为它给我的代码带来了完全错误的含义,并且可能会使调试变得更加困难.
  • 对端口进行一些摆弄,以与Javascript进行交互,并在Nothing那里得到一个抛出的异常.我还没有仔细研究这是如何工作的,但是显然这是可能的.但这似乎混淆了太多的依赖关系,而这些依赖关系本来就是简单的纯Elm.
  • Just use withDefault. I do have a default value of a available, but I don't like this as it gives the completely wrong meaning to my code and will probably make debugging harder down the road.
  • Do some fiddling with ports to interface with Javascript and get an exception thrown there if it's Nothing. I haven't carefully investigated how this works yet, but apparently it's possible. But this just seems to mix up too many dependencies for what would otherwise be simple pure Elm.

推荐答案

(回答我自己的问题)

我找到了两个更令人满意的解决方案:

I found two more-satisfying solutions:

  • 滚动我自己的部分定义的函数,该函数被引用了链接讨论中的其他地方.但是这种方式的代码感觉不完整(我希望编译器有一天会警告我有关模式不完整的信息),并且错误消息仍然不清楚.
  • 模式匹配并使用 Debug.crash (如果没有).这似乎类似于Haskell的error,这是我目前倾向于的解决方案.

  • Roll my own partially defined function, which was referenced elsewhere in the linked discussion. But the code kind of feels incomplete this way (I'd hope the compiler would warn me about incomplete pattern matches some day) and the error message is still unclear.
  • Pattern-match and use Debug.crash if it's a Nothing. This appears similar to Haskell's error and is the solution I'm leaning towards right now.

import Debug

fromJust : Maybe a -> a
fromJust x = case x of
    Just y -> y
    Nothing -> Debug.crash "error: fromJust Nothing"

(不过,模块名称和描述也让我有些犹豫,因为它似乎不适合我的目的;我想指出真正的程序员错误,而不仅仅是调试.)

(Still, the module name and description also make me hesitate because it doesn't seem like the "right" method intended for my purposes; I want to indicate true programmer error instead of mere debugging.)

这篇关于正确地将Maybe a转换为Elm的正确方法,无所不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆