Comments (4)
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.
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/doc/src/manual/conversion-and-promotion.md
Lines 167 to 170 in a6ce761
from julia.
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.
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)
- `which` with names of modules behaves differently in `Main`
- `Sys.cpu_info()` documentation says time units are CLK_TCK, should be milliseconds
- `zero` on uninitialized arrays now fail in some cases HOT 2
- Change in backslash between adjoined arrays. HOT 3
- ScopedValue is allocating when accessed HOT 4
- New stack overflow in type inference on old package. HOT 1
- Assertion error on `codeinst->max_world` when loading Parquet2 HOT 2
- Unbound arg problem with default constructor of a parametric type with Union fields HOT 1
- `show` of `StepRangeLen` fails due to missing `TwinPrecision` zero HOT 3
- Incorrect optimization on typejoin in 1.11+ HOT 3
- InvertedIndices broke on 1.11 HOT 2
- `UnionAll` types violate observational equivalence for `egal` (`===`) objects HOT 3
- Broken links in manual HOT 1
- Reverse REPL HOT 1
- julia & freebsd HOT 14
- Installation problem due to glibc requirement (CentOS 6.10, glibc 2.12, linux kernel 2.6.32) HOT 6
- wrong tbaa for ptr_phi in codegen (causes Transducers segfault) HOT 4
- effects mistake with refining inconsistent errors to always-throws HOT 4
- Broadcasted in-place assignment of matrix to subarray 3x slower in 1.11-alpha1 over 1.10 and nightly HOT 7
- Add error hint for `getproperty(::Dict, ::Symbol)`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from julia.