The use case is the following: I have parameters to various economic
models in composite (immutable) types. Each model maps to a set of
moments, for which I have data, and I am calling NLopt to find the
parameters for which the moments match the data. For this, I need to be
able to convert back and forth between immutable types and vectors
(which can be passed to NLopt).

I am using the function

repack(schema, value, outputtype)

The same value may be mapped in multiple ways, hence `schema`. For
example, a schema could be that various fields of a composite type are
ignored or constrained.

A straightforward natural mapping when the schema is the composite type
itself is

repack(T::DataType, x::T, ::Type{Vector}) = [getfield(x,f) for f = 
fieldnames(x)]

I want to write a short version

repack{T}(x::T, outputtype) = repack(T, x, outputtype)

that only gets called when T is a DataType. I don't know how to do this.

But in the meantime I realized that DataType is not what I want anyway,
I want to test for composite types, but that has no explicit type.

Best,

Tamas


On Fri, May 01 2015, Tom Breloff <[email protected]> wrote:

> If I understand correctly, you want to pass an instance of Foo into fun2,
> which would then effectively call "fun1(Foo, Foo())".  I don't quite
> understand the use case... can you give an example of when the second
> "fun1" definition would be called?
>
> On Friday, May 1, 2015 at 9:24:59 AM UTC-4, Tamas Papp wrote:
>>
>> Not really --- I wanted to dispatch on something being an _instance_ of
>> a type that is a DataType. Having reread the manual, I don't think that
>> is possible directly, but works with one level of indirection:
>>
>> fun1(T::DataType,x) = "do something with $x"
>> fun1(T, x) = "this is not a datatype"
>> fun2{T}(x::T) = fun1(T,x)
>> type Foo end
>> fun2(Foo())
>>
>> That said, I realized that DataType is not what I need.
>>
>> Best,
>>
>> Tamas
>>
>> On Thu, Apr 30 2015, Tom Breloff <[email protected] <javascript:>> wrote:
>>
>> > Is this what you're looking for?
>> >
>> >
>> > julia> yyy(x::DataType) = true
>> > yyy (generic function with 1 method)
>> >
>> >
>> > julia> yyy(x) = false
>> > yyy (generic function with 2 methods)
>> >
>> >
>> > julia> yyy(Int)
>> > true
>> >
>> >
>> > julia> yyy(5)
>> > false
>> >
>> >
>> >
>> >
>> > On Thursday, April 30, 2015 at 11:04:02 AM UTC-4, Tamas Papp wrote:
>> >>
>> >> This is toy problem that came up in the context of something larger,
>> >> reduced to be simple so that I can ask about it more easily.
>> >>
>> >> Suppose I want to implement the function
>> >>
>> >> is_instanceof_datatype(x) = is(typeof(x),DataType)
>> >>
>> >> using dispatch: a default method that is
>> >>
>> >> is_instanceof_datatype(x) = false
>> >>
>> >> and some other method which only gets called when x is an instance of a
>> >> DataType:
>> >>
>> >> is_instanceof_datatype{ ... }(x::T) = true # how to dispatch
>> >>
>> >> but I don't know how to do the latter, hence the ....
>> >>
>> >> The context is that I want to write a method that, for instances of
>> >> DataTypes, returns the slots in a given order, but for other values it
>> >> does something else, and I don't know how to do this.
>> >>
>> >> Best,
>> >>
>> >> Tamas
>> >>
>>

Reply via email to