types - Does Julia have a strict subtype operator? -


question: julia have strict subtype operator?

note: operator <: not strict subtype operator, since number <: number evaluates true. interested in operator evaluate false number <: number true int <: number.

possible use case: consider function defined:

myfunc{t<:union(int, string)}(x::array{t, 1}, y::array{t, 1)}) 

currently, function constrains x , y arrays of same type, type int, string, or union(int, string). strict subtype operator, force input arrays of type int or string, , eliminate (rather odd) union(int, string) scenario.

i don't think there such operator in julia, quite easy write function same check:

strictsubtype{t,u}(::type{t}, ::type{u}) = t <: u && t != u # note: untested! 

however, have question use case. if want like

function my_func{t<:string}(x::vector{t}, y::vector{t})     # handle strings     # note string abstract type, inherited e.g. asciistring , utf8string end  function my_func(x::vector{int}, y::vector{int})     # handle ints     # note int concrete type (actually alias either int32 or int64,     # depending on platform) no generic type parameter necessary end 

then write instead. if have parts of logic can shared, refactor out separate methods, can perhaps relax type parameters (or omit them entirely).

update, in response comment:

if 2 methods should exactly same thing, you're better off using duck typing, , not specifying types of function arguments @ all:

funciton my_func(x, y)     # handle ints, strings , else supports things need (e.g. > , <) end 

julia compile specific methods each type combination call things for, still fast code; if function type stable, fast combination (see julia docs more thorough explanation of how works). if want make sure 2 arguments vectors, , of same type, i'd recommend doing diagonal dispatch (also more thoroughly explained in docs):

function my_func{t}(x::abstractvector{t}, y::abstractvector{t})     # handle stuff end 

note use abstractvector rather vector - allows using other container type behaves vector elements of type t, maximizing usability of function other coders.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -