Re: [julia-users] promotion and conversion

2016-05-04 Thread Lutfullah Tomak
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 
> `g` 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  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 
>>> ; however, I have not been successful so 
>>> far.
>>>
>>> Thanks!
>>>
>>
>>

Re: [julia-users] promotion and conversion

2016-05-04 Thread Arda Aytekin
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 
`g` 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  > 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 
>> ; however, I have not been successful so 
>> far.
>>
>> Thanks!
>>
>
>

Re: [julia-users] promotion and conversion

2016-05-03 Thread ggggg
I ran into the same confusion the first time I tried following that 
section. Maybe adding something like "In most cases, you must ``import 
Base: convert`` or explicitly extend ``Base.convert``." to that section of 
the docs would help. Probably similar language should be added to 
promote_rule as well.


Re: [julia-users] promotion and conversion

2016-05-03 Thread Lutfullah Tomak
Hi Arda,
As Stefan sad, you need import those Base functions or use Base.convert and 
Base.promote_rule to add a new method to them. You may notice in repl that if 
you do not import them after typing those methods you have only 1 methods for 
each of those functions.

Re: [julia-users] promotion and conversion

2016-05-03 Thread Stefan Karpinski
Did you import the convert and promote_rule functions?

On Tue, May 3, 2016 at 12:21 PM, Arda Aytekin  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
> ; however, I have not been successful so
> far.
>
> Thanks!
>


[julia-users] promotion and conversion

2016-05-03 Thread Arda Aytekin
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 
; however, I have not been successful so far.

Thanks!