object properties variable

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

object properties variable

Lewis Lehe
Hi I was wondering if there is a way to do this? To access a fieldname with a variable?

type Foo
 bar
:Float64
end

foo
= Foo(5.0)

test
= :bar
foo[test]



Also, I wondered if there is a way to convert an object into a dict? I want to "println" an object in a way that's readable in the output, like a Dict.



Reply | Threaded
Open this post in threaded view
|

Re: object properties variable

John Myles White

getfield(foo, :bar)


You can iterate over the fieldnames(foo) to print things. I would avoid a potentially wasteful conversion to a dictionary since it doesn't buy you anything in terms of expressive power.

--John

On Monday, August 15, 2016 at 8:38:04 PM UTC-7, Lewis Lehe wrote:
Hi I was wondering if there is a way to do this? To access a fieldname with a variable?

type Foo
 bar
:Float64
end

foo
= Foo(5.0)

test
= :bar
foo[test]



Also, I wondered if there is a way to convert an object into a dict? I want to "println" an object in a way that's readable in the output, like a Dict.



Reply | Threaded
Open this post in threaded view
|

Re: object properties variable

Lewis Lehe
thanks, this did it for me.

On Monday, August 15, 2016 at 8:48:31 PM UTC-7, John Myles White wrote:

getfield(foo, :bar)


You can iterate over the fieldnames(foo) to print things. I would avoid a potentially wasteful conversion to a dictionary since it doesn't buy you anything in terms of expressive power.

--John

On Monday, August 15, 2016 at 8:38:04 PM UTC-7, Lewis Lehe wrote:
Hi I was wondering if there is a way to do this? To access a fieldname with a variable?

type Foo
 bar
:Float64
end

foo
= Foo(5.0)

test
= :bar
foo[test]



Also, I wondered if there is a way to convert an object into a dict? I want to "println" an object in a way that's readable in the output, like a Dict.