# Typed globals: macro to define multiple, without manual type annotations

**URL:** https://discourse.julialang.org/t/typed-globals-macro-to-define-multiple-without-manual-type-annotations/90818
**Category:** General Usage
**Tags:** macros, typed-globals
**Created:** [November 25, 2022, 4:46pm UTC](https://discourse.julialang.org/t/typed-globals-macro-to-define-multiple-without-manual-type-annotations/90818 "2022-11-25T16:46:59Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [December 9, 2022, 1:57am UTC](https://discourse.julialang.org/t/typed-globals-macro-to-define-multiple-without-manual-type-annotations/90818/6 "2022-12-09T01:57:35Z")

</div>

Ah, found what I was originally thinking of: it’s the “@stable” macro by @giordano in the Seven Lines of Julia thread:

> [@Seven Lines of Julia (examples sought)](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/157):
>
> In Julia v1.8 we’ll have the possibility of type-annotating global variables, to promise their type won’t change: 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\> macro stable…

Simeon’s is a bit robuster indeed, with the $tmp.

@simeonschaub I was also wondering, why the `Base.mapany` (and not just `map`)?  
↓  
EDIT: I found why [here](https://timholy.github.io/SnoopCompile.jl/stable/snoopr/#Adding-type-annotations) (in the SnoopCompile docs) (maybe):

> `mapany` avoids trying to narrow the type of `f(v[i])` and just assumes it will be `Any`, thereby avoiding invalidations of many `convert` methods.

So, it’s used to spare the compiler some type inference work and/or to avoid `convert` invalidations, I think

The final thing I wonder about is the @nospecialize:  
all Exprs are the same type, so there is no “too many specializations” problem right?

Like, why not say `rewrite_assignment(ex::Expr)`.  
And why the `Meta.isexpr(ex, …)` instead of just `ex.head == …`?  
↓  
Ah, these are to handle `LineNumberNode`s, I assume.

Still, how is the @nospecialize useful?

---

_[View the full topic](https://discourse.julialang.org/t/typed-globals-macro-to-define-multiple-without-manual-type-annotations/90818)._
