# Initializing an object with \`nothing\`

**URL:** https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423
**Category:** New to Julia
**Created:** [June 23, 2017, 10:13am UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423 "2017-06-23T10:13:19Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 23, 2017, 10:13am UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/1 "2017-06-23T10:13:19Z")

</div>

I am curious whether initializing a variable of type T using the following code is any good for type stability. The type is usually alot more complex than the example used below with significant allocations and a dependence on the conditions, so I don’t want to initialize a full instance before the if-statement with dummy input, too much waste. On the other hand, if declared inside the if statement, it will not be visible outside, and that’s a no go. I also couldn’t find a way to initialize a global-but-not-too-global variable inside an if statement, so the following seems like the next most reasonable approach.

```julia
a = nothing
if b > 1
     a = T(1)
else
     a = T(2)
end

```

---

<div class="post-metadata">

### Author: ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)
#### Post date: [June 23, 2017, 10:49am UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/2 "2017-06-23T10:49:56Z")

</div>

> if declared inside the if statement, it will not be visible outside

I don’t think this is true - `if` blocks don’t introduce new scope. Do you have a minimum working example of a case where you can’t access something allocated inside an `if` block?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 23, 2017, 10:54am UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/3 "2017-06-23T10:54:19Z")

</div>

Also, it’ll hurt type stability and the syntax for declaring local variable is `local`.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 23, 2017, 11:04am UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/4 "2017-06-23T11:04:38Z")

</div>

I had a few cases before where declaring a variable with nothing seemed to have solved an error, so I thought this was true. Thanks for correcting me. I will remove this declaration.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [June 25, 2017, 9:57am UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/5 "2017-06-25T09:57:44Z")

</div>

Variables declared inside a for-loop on the other hand are not visible outside the for-loop. What’s the preferred way of making them visible outside?

```julia
local x
for it in iter
  x = something 
end

```

Perhaps?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 25, 2017, 10:02am UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/6 "2017-06-25T10:02:15Z")

</div>

[Yes](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/3).

---

<div class="post-metadata">

### Author: ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)
#### Post date: [July 29, 2017, 2:40pm UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/7 "2017-07-29T14:40:05Z")

</div>

is this still correct for an array.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 29, 2017, 6:32pm UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/8 "2017-07-29T18:32:45Z")

</div>

What is still correct for an array?

---

<div class="post-metadata">

### Author: ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)
#### Post date: [August 2, 2017, 7:44pm UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/9 "2017-08-02T19:44:03Z")

</div>

if i define an array with local without initializing is it allowed. thanks

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [August 2, 2017, 8:20pm UTC](https://discourse.julialang.org/t/initializing-an-object-with-nothing/4423/10 "2017-08-02T20:20:43Z")

</div>

You can **declare** a variable, including an array, without initializing the **variable**. **Defining** the array **IS** initializing it.
