朱莉娅的字面类型

发布于 2025-02-12 11:45:36 字数 895 浏览 0 评论 0原文

在朱莉娅中创建字面类型的最佳模式是什么?

在打字稿中,您可以用字符串文字制作类型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

殊姿 2025-02-19 11:45:36

据我所知,当前的做法本质上是您开发的。要混凝土,它是使用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 with Symbol argument. So your function signature would be e.g. process(::Val{:start}) and you would call it with process(Val(:start)) (of course you could use a utility function or macro to generate Val(:start) from a string as in your code if you wanted)

偏爱自由 2025-02-19 11:45:36

我将采取另一种方法来解决您的问题,不要依靠字符串或符号来确定要使用的功能,而是写具有清晰名称的功能

例如,您的功能命名为Process(),并采用start命令。我正在处理什么?我在功能中使用该命令做点事吗?

相反,如果我写的话:

function start_service(# pass required arguments)
   ...
end 

听起来更好,我知道我的开始是什么,并且必要的论点(如果有的话)是有道理的。

稍后,我决定我需要将数据写入文件,现在我必须更新命令,也许是write

function process(::CommandType"write",path::AbstractString,data)
    # What is being written? Error log? Data?
end

写作更干净:

write_data_to(path::AbstractString)

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 a start command. What I'm I processing? Do I do something with that command in the function?

Instead what if I wrote:

function start_service(# pass required arguments)
   ...
end 

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?

function process(::CommandType"write",path::AbstractString,data)
    # What is being written? Error log? Data?
end

It would be cleaner to write:

write_data_to(path::AbstractString)
阳光下慵懒的猫 2025-02-19 11:45:36

我设法在不疯狂的情况下设法获得的最接近的东西,也不会污染吊装符号的范围是:

module Extraction
@enum Type log_stats_by_author commits_by_author
end

function extract_git_stats(extraction_type::Extraction.Type)
...
end

extract_git_stats(Extraction.commits_by_author)

这也可以在朱莉娅的Vscode扩展中与Intellisense一起使用,使用符号/val的解决方案将无法提供任何自动完成:

“在此处输入图像说明”

The closest thing i managed to get without going crazy and without polluting the scope of hoisted symbols is:

module Extraction
@enum Type log_stats_by_author commits_by_author
end

function extract_git_stats(extraction_type::Extraction.Type)
...
end

extract_git_stats(Extraction.commits_by_author)

This also works with intellisense in Julia's vscode extension, solutions using Symbol/Val won't provide any autocompletion:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文