Methods 朱莉娅:为自定义类型定义方法

Methods 朱莉娅:为自定义类型定义方法,methods,julia,custom-type,Methods,Julia,Custom Type,我很抱歉,如果以前有人回答过这个问题,我没有在我的搜索结果中找到一个直接的答案 我正在经历“艰难地学习朱莉娅”,我真的找不到我的代码和书中的例子有什么区别。无论何时运行它,都会出现以下错误: TypeError: in Type{...} expression, expected UnionAll, got a value of type typeof(+) 代码如下: struct LSD pounds::Int shillings::Int pence::Int

我很抱歉,如果以前有人回答过这个问题,我没有在我的搜索结果中找到一个直接的答案

我正在经历“艰难地学习朱莉娅”,我真的找不到我的代码和书中的例子有什么区别。无论何时运行它,都会出现以下错误:

TypeError: in Type{...} expression, expected UnionAll, got a value of type typeof(+)
代码如下:

struct LSD
    pounds::Int
    shillings::Int
    pence::Int

    function LSD(l,s,d)
        if l<0 || s<0 || d<0
            error("No negative numbers please, we're british")
        end
        if d>12 error("That's too many pence") end
        if s>20 error("That's too many shillings") end
        new(l,s,d)
    end
end

import Base.+
function +{LSD}(a::LSD, b::LSD)
    pence_s = a.pence + b.pence
    shillings_s = a.shillings + b.shillings
    pounds_s = a.pounds + b.pounds
    pences_subtotal = pence_s + shillings_s*12 + pounds_s*240
    (pounds, balance) = divrem(pences_subtotal,240)
    (shillings, pence) = divrem(balance,12)
    LSD(pounds, shillings, pence)
end
struct LSD
磅::整数
先令:整数
便士:整数
功能LSD(l、s、d)

如果l这似乎使用了非常古老的Julia语法(我认为是0.6版)。我想你只需要
函数+(a::LSD,b::LSD)

谢谢你,奥斯卡,你说得对。对不起,我还不能投票。我想这是采用像朱莉娅这样的年轻语言的一个警告。尽管如此,我还是对它充满热情!欢迎来到社区!。Julia 1.0于2018年8月发布,因此我建议尝试使用更新的版本。1.0或更高版本的任何东西在当前版本中都应该工作得很好。我不认为这是正确的,即使是在0.6之前。它使用
LSD
作为类型参数,而不是类型,因此这永远不会是正确的。