Francis Smart
Francis Smart

Reputation: 4055

Add new method to Julia base operator

I would like to do some calculation before feeding the results to the string operator ^. Currently I am doing the following:

"adsf" ^ Float64(6)
# MethodError: no method matching ^(::String, ::Float64)

The following is my attempt so far:

Base.^(x, y::Float64) = x ^ Int(y)
# syntax: "(x,y::Float64)" is not a valid function argument name

In general I think the function form is correct:

fu(x, y::Float64) = x ^ Int(y)
fu("adsf", Float64(6)) 
# "adsfadsfadsfadsfadsfadsf"

Upvotes: 2

Views: 163

Answers (1)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

You need to specify the operator as a symbol

Base.:^(x, y::Float64) = x ^ Int(y)

Upvotes: 5

Related Questions