(You can also use this to test whether the size of a type is equal to some other constant, e.g. var _ [42]struct{} = ...)
-
-
Show this thread
-
Oh, and you can ensure that one constant is larger than another: https://play.golang.org/p/3nCyCuM_D_b
Show this thread -
HA! I felt that there must be some way to check arbitrary boolean conditions at compile-time, and it finally dawned on me: duplicate map initializers! https://play.golang.org/p/n8NJyx4yDAR It ain't pretty, but you can use this to assert any condition involving values known at compile-time!
Show this thread -
(In other words, both of the previous tricks I posted can be expressed in terms of this one -- it's a generalized form that can check any boolean expression.)
Show this thread -
Compile-time assertion that a constant is a power of 2: var _ [0]struct{} = [c & (c - 1)]struct{}{}
Show this thread
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.