朱莉娅的字面类型
在朱莉娅中创建字面类型的最佳模式是什么?
在打字稿中,您可以用字符串文字制作类型:
type command = "start" | "end" | "pause";
在朱莉娅(Julia)中,这将使您可以使用带有字符串参数的多个dispatch,从而易于扩展案例。
一个例子看起来像是
function process(::"start")
initialize()
do_something()
...
end
function process(::"end")
teardown()
...
end
function process(::"pause")
temporary_pause()
...
end
当前,我正在使用类似于此的解决方法,
struct CommandType{T}
end
CommandType(s::AbstractString) = CommandType{Symbol(s)}()
macro CommandType_str(s)
return CommandType{Symbol(s)}
end
function process(t::CommandType"start")
...
end
function process(t::CommandType"stop")
...
end
function process(t::CommandType"pause")
...
end
process(command::String) = process(CommandType(command))
# Usage
process("start")
我可以使用更简洁的模式吗?是否有可能在不久的将来将字面类型添加到朱莉娅?
What is the best pattern to create literal types in Julia?
In Typescript, you can make types out of string literals:
type command = "start" | "end" | "pause";
In Julia, this would allow you to use multiple-dispatch with string arguments, making it easy to extend cases.
An example of this would look like
function process(::"start")
initialize()
do_something()
...
end
function process(::"end")
teardown()
...
end
function process(::"pause")
temporary_pause()
...
end
Currently, I am using a workaround similar to this
struct CommandType{T}
end
CommandType(s::AbstractString) = CommandType{Symbol(s)}()
macro CommandType_str(s)
return CommandType{Symbol(s)}
end
function process(t::CommandType"start")
...
end
function process(t::CommandType"stop")
...
end
function process(t::CommandType"pause")
...
end
process(command::String) = process(CommandType(command))
# Usage
process("start")
Is there a more succinct pattern I can use? Is it possible that literal types will be added to Julia in the near future?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,当前的做法本质上是您开发的。要混凝土,它是使用
val
与符号
参数。因此,您的函数签名将是例如process(:: val {:start})
,您将使用process(val(val(:start))访问它(当然可以使用实用程序函数或宏来从字符串中生成
val(:start)
(如果需要)As far as I know the current practice is essentially what you have developed. To be concrete it is to use
Val
withSymbol
argument. So your function signature would be e.g.process(::Val{:start})
and you would call it withprocess(Val(:start))
(of course you could use a utility function or macro to generateVal(:start)
from a string as in your code if you wanted)我将采取另一种方法来解决您的问题,不要依靠字符串或符号来确定要使用的功能,而是写具有清晰名称的功能。
例如,您的功能命名为
Process()
,并采用start
命令。我正在处理什么?我在功能中使用该命令做点事吗?相反,如果我写的话:
听起来更好,我知道我的开始是什么,并且必要的论点(如果有的话)是有道理的。
稍后,我决定我需要将数据写入文件,现在我必须更新
命令
,也许是write
?写作更干净:
I would take another approach to your problem, don't rely on string or symbols to determine which function to use, instead write functions that have clear names.
For example, your function is named
process()
and takes astart
command. What I'm I processing? Do I do something with that command in the function?Instead what if I wrote:
Sounds better, I know what I'm starting and the necessary arguments, if any, would make sense.
Later on, I decide I need to write data to a file, I now have to update the
Command
, perhaps,write
?It would be cleaner to write:
我设法在不疯狂的情况下设法获得的最接近的东西,也不会污染吊装符号的范围是:
这也可以在朱莉娅的Vscode扩展中与Intellisense一起使用,使用符号/val的解决方案将无法提供任何自动完成:
The closest thing i managed to get without going crazy and without polluting the scope of hoisted symbols is:
This also works with intellisense in Julia's vscode extension, solutions using Symbol/Val won't provide any autocompletion: