I think I wasn't clear. I was telling after your new method definition like

julia>convert(::Type{A1}, a::A2) = A1(a)  #without trailing semicolon
convert (generic function with 1 method)

Because you had trailing semicolon prompt did not show the result.

I agree that documentation should mention that in order to expand a 
module's function
one needs to import that function first. Otherwise it is just a new 
function for the current module.

Best,

On Wednesday, May 4, 2016 at 11:35:26 AM UTC+3, Arda Aytekin wrote:
>
> Hey Stefan,
>
> Actually, I did not import these functions, nor did I try extending 
> Base.{convert,promote_rule} versions.
>
> Following purely the documentation, one cannot understand easily whether 
> they should extend them, or not.
> Sorry for being dumb --- extending Base. versions solved my issue. But as 
> `ggggg` says, this is not trivial
> at first try.
>
> And, as for your comment, Lutfullah Tomak, I can say I cannot notice that 
> there is only one method due to
> one reason. If you type `?promote_rule` in repl to see how it works, repl 
> gives you the correct function without
> any `Base.` preceding the function's name. Then, if you run 
> `methods(promote_rule)` prior to extending this
> function, you see again a list of `promote_rule`s which are already 
> defined in Julia. Finally, you assume (at
> least I assumed) that it is the very `promote_rule` function you need to 
> extend, not `Base.promote_rule`.
>
> The same applies to `convert`. 
>
> On Tuesday, May 3, 2016 at 7:31:56 PM UTC+2, Stefan Karpinski wrote:
>>
>> Did you import the convert and promote_rule functions?
>>
>> On Tue, May 3, 2016 at 12:21 PM, Arda Aytekin <ardaa...@gmail.com> wrote:
>>
>>> Hey,
>>>
>>> I am sorry if this question is either irrelevant or stupid, but I am 
>>> having problems figuring out the type promotion and conversions.
>>> Could you please explain to me why the below code excerpt is not giving 
>>> me what I want it to return:
>>>
>>> abstract A;
>>>
>>> immutable A1 <: A
>>>     num::Float64
>>> end;
>>>
>>> immutable A2 <: A
>>>     num::Int64
>>> end;
>>>
>>> # conversion
>>> A1(a::A2) = A1(a.num);
>>> convert(::Type{A1}, a::A2) = A1(a);
>>>
>>> # promotion
>>> promote_rule(::Type{A1}, ::Type{A2}) = A1;
>>>
>>> a1 = A1(1.);
>>> a2 = A2(2);
>>>
>>> promote(a1,a2)
>>> julia> (A1(1.0),A2(2))
>>>
>>> Why can I not get `(A1(1.0),A1(2.0))` for example, although the 
>>> `convert(::Type{A1}, a::A2)` method and the `A1(a::A2)` constructor
>>> are working properly. Or, am I missing something?
>>>
>>> I tried to follow the promotion and conversion section of documentation 
>>> <http://julia.readthedocs.org/>; however, I have not been successful so 
>>> far.
>>>
>>> Thanks!
>>>
>>
>>

Reply via email to