Giter VIP home page Giter VIP logo

Comments (4)

jishnub avatar jishnub commented on May 24, 2024 1

Yes, once you use the Base convert function, you can't add a new function by the same name (as in the example above). You can only add methods to it. And to add methods to the function, you either need to import it, or fully qualify the name.

I think it's a good idea for the docs to be explicit about this, and add methods to Base.convert.

from julia.

inkydragon avatar inkydragon commented on May 24, 2024

Looks like it's working fine on v1.10.0.

julia> struct MyType
         value
       end

julia> convert(::Type{MyType}, x) = MyType(x)
convert (generic function with 1 method)

julia> convert(MyType, 1)
MyType(1)

julia> convert(MyType, pi)
MyType(π)

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39 (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 32 × 13th Gen Intel(R) Core(TM) i9-13900HX
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, goldmont)
  Threads: 1 on 32 virtual cores

And latest doc already imports Base.convert at the beginning.
Maybe the doc update needs to be backported. (#50274)

```julia
import Base: convert
convert(::Type{MyType}, x) = MyType(x)
```

from julia.

Vectornaut avatar Vectornaut commented on May 24, 2024

Whoops—I left out a step when I wrote the reproduction procedure, and never tried it again from a clean REPL! I've updated the procedure.

It turns out that before doing the original reproduction procedure, I tried to do an impossible conversion, like one of these:

convert(Int64, "a")
convert(Int64, 1.5)

This step is required to get the "invalid method definition" error later. Maybe this is expected behavior, although I certainly didn't expect it!

from julia.

Vectornaut avatar Vectornaut commented on May 24, 2024

Yes, once you use the Base convert function, you can't add a new function by the same name (as in the example above). You can only add methods to it. And to add methods to the function, you either need to import it, or fully qualify the name.

Aha! So, when @inkydragon pointed out that the original reproduction procedure "looks like it's working fine," that's actually an example of the silent failure I mentioned in the "Additional Context" section. The code runs without errors, but it doesn't add a method to Base.convert, which is what I thought would happen based on the uncorrected docs. I think @KristofferC's update resolves the confusion. 👍

from julia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.