The shortcut is slightly less convenient if you have an array as a
parameter. Then you have to write
myfunc{T <: Integer}(x::Array{T})
Because Array{Int64} is not a subtype of Array{Integer}.
kl. 08:37:28 UTC+1 onsdag 12. februar 2014 skrev Kevin Squire følgende:
>
> So, perhaps you're aware of this, but just in case: for things like Int64
> and Int32, you can usually just define
>
> myfunc(x::Integer)
>
> and Julia will automatically create a specialized version of the function
> for each int type you call it with.
>
> There are cases where this is not what you want, of course, but overall
> I find this rather nice (and I really only understood this recently).
>
> Cheers, Kevin
>
> On Tuesday, February 11, 2014, Fil Mackay
> <[email protected]<javascript:>>
> wrote:
>
>> Thanks again for the reply.
>>
>> Another example to accepting a symbol is taking an integer, I choose (a
>> somewhat arbitrary) type on my function call - say Int32. I then want to
>> call it with an Int64. I *completely* agree with the "no automatic
>> conversion"-ish-ness of Julia, but I want some syntax sugar to make
>> explicit conversions easier :)
>>
>> myfunc(my64::Int32)
>>
>> # rather than
>>
>> myfunc(convert(Int32, my64))
>>
>> # seems easier, esp. when you have lots of args :)
>>
>> You'd probably need an operator other than "::" - a conversion operator
>> like ":>"?
>>
>>
>> On Wed, Feb 12, 2014 at 5:43 PM, Kevin Squire <[email protected]>wrote:
>>
>>> This is a pretty typical pattern for Julia, as you're finding. My
>>> understanding is that this was a design choice: there are very few
>>> automatic conversions between types in Julia, and if anything looks like it
>>> was automatically converted, it was probably a side effect of multiple
>>> dispatch on an abstract type or because of an extra definition like the one
>>> I gave you.
>>>
>>> If you could be more explicit about the problem you're trying to solve,
>>> you might get a better answer. There are ways people have gotten around
>>> this, but only with certain patterns (e.g., wrapper types).
>>>
>>> Cheers, Kevin
>>>
>>>
>>>
>>> On Tuesday, February 11, 2014, Fil Mackay <[email protected]>
>>> wrote:
>>>
>>>> Thanks for the reply Kevin.
>>>>
>>>> Unfortunately this is a routine pattern that I'm hitting, I was hoping
>>>> for a more general solution than adding a lot of extra overloads..
>>>>
>>>>
>>>> On Wed, Feb 12, 2014 at 10:36 AM, Kevin Squire
>>>> <[email protected]>wrote:
>>>>
>>>>> Hi Fil,
>>>>>
>>>>> The way to do that in Julia is simply to define another version of the
>>>>> function, which does the conversion and passes that on to the "main"
>>>>> version of the function:
>>>>>
>>>>> test(str::String) = test(symbol(str))
>>>>>
>>>>> Cheers,
>>>>> Kevin
>>>>>
>>>>>
>>>>> On Tue, Feb 11, 2014 at 3:30 PM, Fil Mackay <[email protected]
>>>>> > wrote:
>>>>>
>>>>>> Just wondering if there is an operator to easily perform known
>>>>>> conversion. Say I have a function that takes a Symbol, and I want it to
>>>>>> accept a String as well:
>>>>>>
>>>>>> function test(s::Symbol)
>>>>>> end
>>>>>>
>>>>>> What I want to do is say, "yes I know this is not a Symbol - so
>>>>>> please convert() it"
>>>>>>
>>>>>> mystr = "foo"
>>>>>> test(mystr::Symbol)
>>>>>>
>>>>>> This would translate to:
>>>>>>
>>>>>> mystr = "foo"
>>>>>> test(convert(Symbol, mystr))
>>>>>>
>>>>>> Is there any such operator? I would have thought doing this with the
>>>>>> current type assert would make sense?
>>>>>>
>>>>>> Regards, Fil.
>>>>>>
>>>>>>
>>>>>
>>>>
>>