Using convert as Matt Bauman suggests is exactly what I had in mind.
The behaviors that are supposed to be inherited are explicitly stated in
the collection of functions which are defined with
f(a::Any) = f(convert(T,a));
function f(t::T)
#real definition
end
Any type "A" can "inherit" f just be defining a method
convert(::Type{T},a::A).
Multiple dispatch along with conversion rules like this seems much more
flexible than forcing a unified type hierarchy.
If the package defining f doesn't support this idiom, then another package
can always import f and extend it with the more general definition.
On Tuesday, March 15, 2016 at 2:20:37 PM UTC-4, Stefan Karpinski wrote:
>
> Implicit conversion of function arguments only makes sense if there's a
> well-defined notion of a particular type that is expected when calling a
> function – that makes sense in languages where functions are monomorphic.
> In Julia, that's very far from the case – and the combination of
> inheritance, multiple dispatch and implicit conversion would be total
> chaos. For typed locations like object fields and array slots (or typed
> local variables), we do implicit conversion since there is a well-defined
> type to convert to.
>
> On Tue, Mar 15, 2016 at 11:36 AM, Carlo Lucibello <[email protected]
> <javascript:>> wrote:
>
>> Unfortunately In my use case I would have to add a method for almost
>> every method exported by another module:
>>
>> see https://github.com/JuliaGraphs/Networks.jl/issues/4
>>
>> The easiest way to do would be to use abstraction and inheritance, but in
>> the other module (LightGraphs) I don't have an abstract type to inherit from
>>
>> There is some deeply rooted reason for not having implicit (but
>> explicitly declared) conversion?
>>
>>
>> Il giorno martedì 15 marzo 2016 15:37:33 UTC+1, Matt Bauman ha scritto:
>>>
>>> To generalize this idiom a bit, I think I'd write it as:
>>>
>>> Base.convert(::Type{Float64}, a::MyNum) = convert(Float64, a.x)
>>> f(a) = f(convert(Float64, a))
>>> f(a::Float64) = 2. # real algorithm
>>>
>>> Of course, this will also allow things like `f(1)`.
>>>
>>>
>>> On Tuesday, March 15, 2016 at 10:18:30 AM UTC-4, Stefan Karpinski wrote:
>>>>
>>>> No, there is never any implicit conversion of arguments in Julia.
>>>> However, all you need to do is add a method to f to get it to do what you
>>>> want:
>>>>
>>>> f(a::MyNum) = convert(Float64, a.x)
>>>>
>>>> On Tue, Mar 15, 2016 at 6:54 AM, Carlo Lucibello <[email protected]>
>>>> wrote:
>>>>
>>>>>
>>>>>
>>>>> Il giorno martedì 15 marzo 2016 11:51:31 UTC+1, Carlo Lucibello ha
>>>>> scritto:
>>>>>>
>>>>>> Is it possible to have user defined implicit conversion in Julia?
>>>>>>
>>>>>> This is an example of how I imagine it should work:
>>>>>>
>>>>>> type MyNum
>>>>>> x
>>>>>> end
>>>>>>
>>>>>> implicit_convert(::Type(Float64), a::MyNum) = convert(Float64, a.x)
>>>>>>
>>>>>> f(x::Float64) = 2.
>>>>>> f(MyNum(1.)) # error, sadly
>>>>>>
>>>>>> In C++ this behaviour can be obtained overloading
>>>>>>
>>>>> * overloading operator double()
>>>>>
>>>>>> PS I don’t want to use inheritance
>>>>>>
>>>>>>
>>>>>
>>>>
>