# Type stability for scalars

**URL:** <https://discourse.julialang.org/t/type-stability-for-scalars/6606>\
**Category:** New to Julia\
**Tags:** type-stability\
**Created:** [October 22, 2017, 6:18am UTC](https://discourse.julialang.org/t/type-stability-for-scalars/6606 "2017-10-22T06:18:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![turtle](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@turtle](https://discourse.julialang.org/u/turtle)\
**Post date:** [October 22, 2017, 6:18am UTC](https://discourse.julialang.org/t/type-stability-for-scalars/6606/1 "2017-10-22T06:18:22Z")

</div>

For type stability my current code is:

```julia
eltype(x)(0.5)

```

Please advise, is there a simpler solution if x is a scalar?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [October 22, 2017, 6:50am UTC](https://discourse.julialang.org/t/type-stability-for-scalars/6606/2 "2017-10-22T06:50:46Z")

</div>

To get the type of `x` you can use `typeof(x)`.  
To convert `0.5` to the type of `x` you can use `oftype(x, 0.5)`.

---

<div class="post-metadata">

**Author:** ![turtle](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@turtle](https://discourse.julialang.org/u/turtle)\
**Post date:** [October 22, 2017, 7:16am UTC](https://discourse.julialang.org/t/type-stability-for-scalars/6606/3 "2017-10-22T07:16:12Z")

</div>

> [@bkamins](#):
>
> oftype(x, 0.5)

Thank you very much!

---

<div class="post-metadata">

**Author:** ![turtle](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@turtle](https://discourse.julialang.org/u/turtle)\
**Post date:** [October 22, 2017, 8:27am UTC](https://discourse.julialang.org/t/type-stability-for-scalars/6606/4 "2017-10-22T08:27:04Z")

</div>

Just one more question.  
Is there the slightest difference in code efficiency for the three versions?  
(I see no difference after top:, but what happens in define?)

```julia
julia> x = 1f0
1.0f0

julia> @code_llvm typeof(x)(0.5)

; Function Attrs: uwtable
define float @jlsys_Type_56602(i8**, double) #0 !dbg !5 {
top:
  %2 = fptrunc double %1 to float
  ret float %2
}

julia> @code_llvm eltype(x)(0.5)

; Function Attrs: uwtable
define float @jlsys_Type_56602(i8**, double) #0 !dbg !5 {
top:
  %2 = fptrunc double %1 to float
  ret float %2
}

julia> @code_llvm oftype(x,0.5)

; Function Attrs: uwtable
define float @julia_oftype_64020(float, double) #0 !dbg !5 {
top:
  %2 = fptrunc double %1 to float
  ret float %2
}

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [October 22, 2017, 8:48am UTC](https://discourse.julialang.org/t/type-stability-for-scalars/6606/5 "2017-10-22T08:48:34Z")

</div>

I guess there should be no difference in performance. When I use `@code_native` the generated code is identical although it references to a different file.

---

<div class="post-metadata">

**Author:** ![turtle](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@turtle](https://discourse.julialang.org/u/turtle)\
**Post date:** [October 22, 2017, 8:56am UTC](https://discourse.julialang.org/t/type-stability-for-scalars/6606/6 "2017-10-22T08:56:37Z")

</div>

Thank you again.  
It is a peace of mind that I am free to choose the preferred syntax.
