如何在Julia中的字符串中插入可能为'nothing'的变量?

如何在Julia中的字符串中插入可能为'nothing'的变量?,julia,Julia,我试图通过在字符串中插入变量的值,在Julia中生成一个动态字符串。一切正常,直到今天,该值返回nothing,给我留下了一个错误 如何在字符串中包含nothing?如果n==无,至少不必经历一些的麻烦;n=“None”对于我要插入到字符串中的每个变量 function charge_summary(charges_df) if size(charges_df)[1] > 0 n_charges = size(charges_df)[1] total

我试图通过在字符串中插入变量的值,在Julia中生成一个动态字符串。一切正常,直到今天,该值返回
nothing
,给我留下了一个错误

如何在字符串中包含
nothing
?如果n==无,至少不必经历一些
的麻烦;n=“None”
对于我要插入到字符串中的每个变量

function charge_summary(charges_df)
    if size(charges_df)[1] > 0
        n_charges = size(charges_df)[1]
        total_charges = round(abs(sum(charges_df[:amount])), digits=2)
        avg_charges = round(abs(mean(charges_df[:amount])), digits=2)
        most_frequent_vender = first(sort(by(charges_df, :transaction_description, nrow), :x1, rev=true))[:transaction_description]
        sms_text = """You have $n_charges new transactions, totaling \$$total_charges.
        Your average expenditure is \$$avg_charges.
        Your most frequented vender is $most_frequent_vender.
        """
        return sms_text
    else
        return nothing
    end
end

sms_msg = charge_summary(charges_df)


返回:

ArgumentError: `nothing` should not be printed; use `show`, `repr`, or custom output instead.
string at io.jl:156 [inlined]
charge_summary(::DataFrame) at get-summary.jl:18
top-level scope at none:0
include_string(::Module, ::String, ::String, ::Int64) at eval.jl:30
(::getfield(Atom, Symbol("##105#109")){String,Int64,String})() at eval.jl:91
withpath(::getfield(Atom, Symbol("##105#109")){String,Int64,String}, ::String) at utils.jl:30
withpath at eval.jl:46 [inlined]
#104 at eval.jl:90 [inlined]
hideprompt(::getfield(Atom, Symbol("##104#108")){String,Int64,String}) at repl.jl:76
macro expansion at eval.jl:89 [inlined]
(::getfield(Atom, Symbol("##103#107")))(::Dict{String,Any}) at eval.jl:84
handlemsg(::Dict{String,Any}, ::Dict{String,Any}) at comm.jl:168
(::getfield(Atom, Symbol("##14#17")){Array{Any,1}})() at task.jl:259

不幸的是,您必须显式地处理
nothing
。例如:

Your most frequented vender is $(something(most_frequent_vender, "None")).
原因是不清楚您希望如何将
nothing
转换为字符串,因此必须提供此值(在您的情况下,您希望
“None”

较短的版本为:

Your most frequented vender is $(repr(most_frequent_vender)).
但是
nothing
被打印为
“nothing”
定义
Base.string(x::nothing)
方法:

➜  ~ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.0.3 (2018-12-20)
 _/ |\__'_|_|_|\__'_|  |  android-termux/900b8607fb* (fork: 1550 commits, 315 days)
|__/                   |

julia> Base.string(x::Nothing) = repr(x)  # or just return the string "None", that's up to you.

julia> "$(nothing)"
"nothing"

julia>
Julia 1.3更新

请注意,打印值与其他语言中的值不同,也许他们应该选择一个默认值?否则,此功能的错误消息具有误导性。错误消息中的应为不能,而不是不应。您仍然可以如图所示绕过它。有一个默认值,
“nothing”
。这是一个故意的选择,以防止错误/问题,否则会发生默认。确切地说,这就是为什么引入了
某种东西来处理此类案件。这种类型的盗版不是很受欢迎吗?扩展
Base
是不受欢迎的?我认为类型盗版指的是在另一个不相关的包中扩展某些包类型。无论如何,它回答了一个问题:如何在字符串中包含
nothing