Sean Mackesey
Sean Mackesey

Reputation: 10939

How do I access a Type's constructors as functions in Julia

This would be useful for object construction by chaining. For example, say I would like to create a DataFrame by piping a Dict to it. As in,

merge(dict1, dict2) |> DataFrame

But DataFrame here returns the type DataFrame rather than the constructor I need. How can I access the constructor? I can see the signatures with methods(DataFrame) but can't access the actual function.

Upvotes: 0

Views: 191

Answers (1)

Isaiah Norton
Isaiah Norton

Reputation: 4366

This doesn't work for any type because the |> (pipe) method does not exist for the signature (Any, DataType).

I haven't tried with DataFrame, but the following trivial example works:

type Foo
  x::Int
end

|>(a::Any, T::DataType) = T(a)

test = 1 |> Foo

Upvotes: 1

Related Questions