# Seven Lines of Julia (examples sought)

**URL:** https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416
**Category:** General Usage
**Created:** [November 19, 2020, 7:37am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416 "2020-11-19T07:37:29Z")
**Posts on this page:** 1
**Showing post:** 157

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [April 1, 2022, 9:55pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/157 "2022-04-01T21:55:09Z")

</div>

In Julia v1.8 we’ll have the possibility of type-annotating global variables, to promise their type won’t change:

```julia
julia> x::Float64 = 3.14
3.14

julia> x = 2.71
2.71

julia> x = "hello world"
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Float64

```

Wouldn’t it be nice if we could automatically get the type annotation without having to explicitly type it ourselves? Here be ~~dragons~~ macros (note that the macro definition is exactly 7 lines):

```julia
julia> macro stable(ex)
           ex.head !== :(=) && throw(ArgumentError("@stable: `$(ex)` is not an assigment expression."))
           quote
               local x = $(esc(ex.args[2]))
               $(esc(ex.args[1]))::typeof(x) = x
           end
       end
@stable (macro with 1 method)

julia> @stable y = "hello world"
"hello world"

julia> y
"hello world"

julia> y = "foo bar"
"foo bar"

julia> y = 1 + 2
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String

```

Thanks @Mason for the tip in Zulip about [`local`](https://docs.julialang.org/en/v1/base/base/#local) and the suggestion for the name of the macro (I initially called it `@auto`, like the C++ [`auto`](https://en.cppreference.com/w/cpp/language/auto) keyword).

---

_[View the full topic](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416)._
