Hi Glen,

Perhaps in my previous email when I wrote:

f(1, 2, 3) rather than f((1, 2 , 3))

I should have written something like f(1, 2.3, 5 + 3.im, 3 + 1im, 
Nullable{Float64}(5.)) to make it explicit that I would like to parametrise 
different types. I think David Gold's suggestion is the closest to what I 
want but doesn't quite solve the problem. If I wanted to lift a set of 
Julia's functionality to new types having an arbitrary set of input types, 
I could:

1. Spend alot of time writing code to parse different subsets of modules. 
This would take an immense amount of work and the libraries would 
inevitably change.
2. If we could model a variadic template function having the semantics as I 
specified previously:

function {T...}my_fun(x...)
  for i in 1:length(x)
    println(typeof(x[i]) <: T[i])
  end
end

The above function for simplicity. Here T... are the types of x... and they 
could be all different or the same

We could lift functionality of all the relevant function in one go with a 
sketch implementation like:

# Import the functions ...
import Base: get, +, -, /, *, ^, ...

# Function symbols to be lifted
funs = [:+, :-, ...]

# Generic get function
# Assume appropriate get functions are written for each type
function get{T}(x::T)
  x
end

# My new type is T1{T}()

S = Union{Symbol, Expr}

# My lifter function
function lift(fun_name::S, new_type::S)
  quote
    function $fun_name{T...}(x...)
    y = Array(Any, length(x))
    for i in 1:length(x)
      y[i] = get(i)
    end
    ret = $fun_name(y...)
    U = typeof(ret)
    return $new_type{U}(ret)
    end
  end
end

# Then lift the functions
for i in funs
  eval(lift(i, :T1))
end

Could then do T1(3.4) + 9 and so on for all the functions regardless of 
number of arguments return type etc

DP





On Monday, October 5, 2015 at 7:20:55 AM UTC+1, Glen O wrote:
>
> Just to be clear, are you looking for something that will parameterise 
> across a variety of types, or are you wanting to restrict to a single type?
>
> If the latter, it can be easily done with
>
> function mycode{T}(x::T...)
>    <code here>
> end
>
> which will make all values in the varargs list the same. To restrict to a 
> specific subset, there's also
>
> function mycode(x::Union(Int,Array)...)
>   <code here>
> end
>
> which will require that the arguments in x be either Ints or Arrays (and 
> can have both).
>
> If you're actually after the former, is there a reason why you can't just 
> ask for typeof(x[i]), directly? Alternatively, would this work?
>
> function mycode{T}(x...;t::T=x)
>     for j=1:length(x)
>       println(typeof(x[j])<:T[j])
>     end
> end
>
> On Monday, 5 October 2015 09:32:57 UTC+10, [email protected] wrote:
>>
>> Thanks David, that gets me part of the way there but is there a way of 
>> specifying the x argument as a varag so that the function can be called as:
>>
>> f(1, 2, 3) rather than f((1, 2 , 3)) this is useful if I am trying to 
>> "lift" a bunch of functions that already exist to play with new types. For 
>> instance I could then call plus on the new types T1(value1) + T2(value2) 
>> retaining the natural semantics of the functions.
>>
>> DP
>>
>>
>> On Monday, October 5, 2015 at 12:03:33 AM UTC+1, David Gold wrote:
>>>
>>> Note that if the type variable T does not appear in the argument 
>>> signature then the method won't be callable.
>>>
>>> If you really want to make use of the variable parameters, your best bet 
>>> I think is to pass a tuple of args:
>>>
>>> function f{T<:Tuple}(x::T)
>>>     for (i, param) in enumerate(T.parameters)
>>>         println(typeof(x[i]) <: param)
>>>     end
>>> end
>>>
>>> This (or some variant, I forget) can also make the types of the 
>>> individual xs available at compile time. 
>>>
>>>
>>>
>>> On Saturday, October 3, 2015 at 7:22:49 PM UTC-7, [email protected] 
>>> wrote:
>>>>
>>>> Is it possible to specify a variable number of parameter types in 
>>>> Julia, for instance something like
>>>>
>>>> function {T...}my_fun(x...)
>>>>   for i in 1:length(x)
>>>>     println(typeof(x[i]) <: T[i])
>>>>   end
>>>> end
>>>>
>>>> should print true ... times
>>>>
>>>> Thanks
>>>>
>>>

Reply via email to