On Sunday, 28 June 2015 10:13:42 UTC-3, ks wrote:
>
> Hello Andrew,
>
> Thanks!
>
> the other answer is on the money, but, in this particular case, it seems 
>> to me that you might want to have a function that could take both of those, 
>> with the idea that you never get more than max_num_items, but that if you 
>> find max_iter_id before that, the sequence stops there.
>>
>> in that case, named args would be more suitable.  something like
>>
>> function getitems(; max_num_items=-1, max_iter_id=...)
>>
>
> In this case it would work nicely because all the cases make sense: none, 
> one (any), or both.
>  
>
>>
>> where you might still have a special type for item id, but also have a 
>> special singleton that means "none" (like -1 means unlimited for 
>> max_num_items).
>>
>> then you could specify none (all items), either, or both.
>>
>
> So you'd have a parent and two child types?:
>
>                       ItemId
>            /                             \
>     ExistingItemId          NoneItemId (Singleton)
>
> What would be the difference of using one type ItemId and "nothing" to 
> denote "none"?:
>
> getitems(; max_num_items = -1, max_item_id = nothing )
>
> I also read about Nullable{T} type, perhaps it could be used as well in 
> this case.
>

any of those would work.  you could also do something "dirtier" with a -ve 
sign if you were using a signed int for item IDs, but "real" IDs were 
positive.

for example:

immutable ItemId
    id::Int
end

then ItemId(-1) could be used to mean "no value".

it depends how "serious" you want to be about making things type safe.

andrew

 

> Thanks again,
> Krystian
>  
>
>>
>> andrew
>>
>>
>> On Friday, 26 June 2015 23:30:45 UTC-3, ks wrote:
>>>
>>> Hello everyone,
>>>
>>> I've just started to write a bit of code in Julia and I'm still 
>>> exploring the best ways of doing this and that. I'm having this small 
>>> problem now and wanted to ask for your advice.
>>>
>>> I'd like to have two methods that retrieve some items. The first method 
>>> takes the max number of items that should be retrieved. And the second 
>>> method takes the max item id.
>>>
>>> getitems( maxnumitems )
>>> getitems( maxitemid )
>>>
>>> In both cases the argument has the same type: Int. So how do I take the 
>>> advantage of multiple dispatch mechanism in this situation? And is multiple 
>>> dispatch really the recommended way of handling a situation like this one? 
>>> Here're some alternatives that I thought of:
>>>
>>> 1. Use different function names: getitems, getitems_maxid. Not too 
>>> elegant as you mix purpose and details of function usage in its name.
>>> 2. Use named arguments. This will cause the function implementation to 
>>> grow (a series of if / else), again not too elegant.
>>> 3. Define a new type: ItemId which behaves exactly as Int but can be 
>>> used to 'activate' multiple dispatch (one function would use Int and the 
>>> second one would use ItemId). Generally not the best approach if you have 
>>> methods each having an argument that should be really represented as an Int 
>>> rather than a new type.
>>> 4. ...?
>>>
>>> What would you recommend ?
>>>
>>> Thank you,
>>> ks
>>>
>>

Reply via email to