Re: [julia-users] Type-stable global variables?

2016-10-15 Thread Andrei Zh
Thanks, this is exactly what I was looking for. 

On Saturday, October 15, 2016 at 4:08:48 PM UTC+3, Yichao Yu wrote:
>
>
>
> On Sat, Oct 15, 2016 at 8:59 AM, Andrei Zh  > wrote:
>
>> What is the most straightforward way to make a variable in the global 
>> scope that can change it's value, but not its type? So far I use this: 
>>
>> const GLOBAL_VAR = [MyType[]]  # array with single element
>>
>> set_global_var(x::MyType) = GLOBAL_VAR[1] = x
>> get_goval_var() = GLOBAL_VAR[1]
>>
>> This works fine and preserves type stability, but looks quite 
>> unintuitive. Is there more standard container or another way (e.g. type 
>> assertions or something) to handle such cases? 
>>
>
> Use `const GLOBAL_VAR = Ref{MyType}()` and `GLOBAL_VAR[]`. The plan is 
> that this will essentially be how a typed non-const global be implemented 
> in the future.
>  
>
>

Re: [julia-users] Type-stable global variables?

2016-10-15 Thread Yichao Yu
On Sat, Oct 15, 2016 at 8:59 AM, Andrei Zh 
wrote:

> What is the most straightforward way to make a variable in the global
> scope that can change it's value, but not its type? So far I use this:
>
> const GLOBAL_VAR = [MyType[]]  # array with single element
>
> set_global_var(x::MyType) = GLOBAL_VAR[1] = x
> get_goval_var() = GLOBAL_VAR[1]
>
> This works fine and preserves type stability, but looks quite unintuitive.
> Is there more standard container or another way (e.g. type assertions or
> something) to handle such cases?
>

Use `const GLOBAL_VAR = Ref{MyType}()` and `GLOBAL_VAR[]`. The plan is that
this will essentially be how a typed non-const global be implemented in the
future.


[julia-users] Type-stable global variables?

2016-10-15 Thread Andrei Zh
What is the most straightforward way to make a variable in the global scope 
that can change it's value, but not its type? So far I use this: 

const GLOBAL_VAR = [MyType[]]  # array with single element

set_global_var(x::MyType) = GLOBAL_VAR[1] = x
get_goval_var() = GLOBAL_VAR[1]

This works fine and preserves type stability, but looks quite unintuitive. 
Is there more standard container or another way (e.g. type assertions or 
something) to handle such cases?