Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
入門Go言語仕様 / Go Specification Untyped Constants
Search
DQNEO
April 15, 2021
Programming
0
1.1k
入門Go言語仕様 / Go Specification Untyped Constants
DQNEO
April 15, 2021
Tweet
Share
More Decks by DQNEO
See All by DQNEO
英和辞書付きGo言語仕様書 / Word Wise Go Spec
dqneo
1
410
Go言語低レイヤー入門 Hello world が 画面に表示されるまで / Introduction to low level programming in Go
dqneo
0
800
入門Go言語仕様 Underlying Type / Go Language Underlying Type
dqneo
9
4.3k
How to write a self hosted Go compiler from scratch (Gophercon 2020)
dqneo
3
1.4k
もっと気軽にOSSに Pull Requestを出そう!/ Let's make a PR to OSS more easily
dqneo
6
7.8k
Goコンパイラをゼロから作ってセルフホスト達成するまで / How I wrote a self hosted Go compiler from scratch
dqneo
14
14k
コンパイラをつくってみよう / How to make a compiler
dqneo
9
10k
コンパイラ作りの魅力を語る / Making compilers is fun
dqneo
10
8.2k
Goのmapとheapを自作してみた / How to create your own map and heap in Go
dqneo
0
2.9k
Other Decks in Programming
See All in Programming
Jetpack for KMP
fornewid
1
290
Async Await: Mastering Python's Time-Bending Tricks - EuroPython2024
yanbo
1
290
コード生成を伴うLLMエージェント - 2024.07.18 Tokyo AI
smiyawaki0820
11
4.1k
Terraformテスト入門
msato
0
520
CSC307 Lecture 05
javiergs
PRO
0
210
Ruby メモリ管理 プログラミング
megmogmog1965
0
130
HMSコンペ 11th Solution (team : kansai-kaggler)
t88
1
680
Xcode 16のPreviewModifierと@Previewableを活用した効率的なプレビュー方法の考察
ojun9
2
160
最近追加した型の紹介とその振り返り
aki19035vc
0
170
「2024年版 Kotlin サーバーサイドプログラミング実践開発」の補講 〜O/Rマッパー編〜
n_takehata
2
260
12年前の『型システム入門』翻訳の思い出話
mame
11
1.2k
ピグパーティにおけるMongoDB CommunityバージョンからAtlasへの移行事例
10969hotaka
0
130
Featured
See All Featured
Building Effective Engineering Teams - LeadDev
addyosmani
47
2.2k
The Illustrated Children's Guide to Kubernetes
chrisshort
39
47k
The Cult of Friendly URLs
andyhume
75
5.9k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.9k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
12
3.8k
Code Reviewing Like a Champion
maltzj
517
39k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
502
140k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
36
9.1k
How To Stay Up To Date on Web Technology
chriscoyier
784
250k
WebSockets: Embracing the real-time Web
robhawkes
59
7.2k
Statistics for Hackers
jakevdp
792
220k
Scaling GitHub
holman
458
140k
Transcript
入門Go言語仕様輪読会 Untyped Constants @DQNEO 2021-04-15
自己紹介 • @DQNEO (ドキュネオ) • Goコンパイラ babygo の作者 ◦ https://github.com/DQNEO/babygo
• 公式Goコンパイラ、言語仕様書コントリビュート歴有り
問題: 次の値の型は何でしょうか? 123 "hello" true
答え: なし 123 "hello" true ← 型なし ← 型なし ←
型なし
問題: 次の2文は何が違うでしょうか? const x = 123 var x = 123
注: const / var 違いの他に
答え: 型が違う const x = 123 var x = 123
var x int = 123 両辺とも型なし 両辺とも int =
型なし?
Go言語の定数は • 型あり • 型なし のどちらか
これらは全部型なし定数 • 123 • 1.1 • "hello" • ‘a’ •
true • 1.0+3.0i
型なし定数を作ることも可能 const THREE = 3 const HELLO = "hello" 左辺で型を省き、右辺で型なし定数を使う
型なし定数のおもしろ性質(1) _人人人人人人_ > 型がない <  ̄Y^Y^Y^Y^Y^Y^ ̄
型がないのでユーザ定義型に代入可能 type MyBool bool var b MyBool = true もし
true が bool 型だったら、代入できないはず (代入可能性の解説はこちら ) https://play.golang.org/p/63VHrn7XQVY
const HELLO = "hello" type MyString string var s MyString
= HELLO もし HELLO が string型だったら、代入できない 型がないのでユーザ定義型に代入可能 (代入可能性の解説はこちら )
型がないのでいろんな型に代入可能 var y float32 = 97 var x int64 =
'a' var z byte = 97.0 (※ ただし “representable” な場合に限る) どれも 右辺は 「左辺の型の97」扱い
型なし定数のおもしろ性質(2) 「型を隠し持っている」
型なし定数にも種類がある リテラル 種類 1 integer constant 1.0 floating-point constant 'a'
rune constant "hello" string constant true boolean constant
種類に応じて default type がある リテラル 種類 default type 1 integer
constant int 1.0 floating-point constant float64 'a' rune constant rune(=int32) "hello" string constant string true boolean constant bool
var x = 1.0 型が必要です。 あなたの型を教えてください float64 です 聞かれたときだけ答える default
type とは、 「型を要求されたとき」に使われる型
型を要求される文脈の例 • var x = 123 • y := 1.0
• var ifc interface{} = 1.0 • fmt.Printf("%v", 1.0)
• var x uint8 = 1.0 私は uint8型です。 私に従ってください わかりました
default typeは、必ずしも使われるわけでは ない uint8として振る舞う default typeは使われない
型なし定数のおもしろ性質(3) 「すごい数もOK」
超巨大数を定義できる const HUGE = 92233720368547758070 ↑ int64の最大値より大きい
定義できるが表示できない const HUGE = 92233720368547758070 fmt.Println(HUGE) _人人人人人人_ > コンパイルエラー <  ̄Y^Y^Y^Y^Y^Y^ ̄ constant
92233720368547758070 overflows int64
int型変数に代入できない const HUGE = 92233720368547758070 var x = HUGE constant
92233720368547758070 overflows int64 _人人人人人人_ > コンパイルエラー <  ̄Y^Y^Y^Y^Y^Y^ ̄
Go言語氏 「default type があるとは言ったが、 default type に収まるとは言ってない」
float型変数には代入できる const HUGE = 92233720368547758070 var x float64 = HUGE
fmt.Printf("%v\n", x) => 9.223372036854776e+19 精度は落ちる (当然)
巨大数同士の定数計算ができる const HUGE = 92233720368547758070 const X_HUGE = 922337203685477580700 var
x uint8 = X_HUGE / HUGE 計算結果がuint8 に収まるのでOK => 10
超細かい小数を定義できる const ROOT2 = 1.41421356237309504880168872420969807856 967187537694807317667974 float64 の精度を超えている
二乗すると 2 になる const ROOT2 = 1.41421356237309504880168872420969807856 967187537694807317667974 fmt.Println(ROOT2 *
ROOT2) // => 2 _人人人人人人_ > 精度高すぎ <  ̄Y^Y^Y^Y^Y^Y^ ̄
変数を経由すると精度が落ちる const ROOT2 = 1.41421356237309504880168872420969807856 967187537694807317667974 f := ROOT2 //
ここで float64に詰め替えられる fmt.Println(f * f) // => 2.0000000000000004 https://play.golang.org/p/JMtw6IZjDtP
Goの言語思想 “they are just regular numbers” 型なし定数の数値は「ただの数」である const HUGE =
92233720368547758070 const ROOT2 = 1.4142135623730950488016887242096980785696718753769 4807317667974 https://blog.golang.org/constants
Goの言語思想 “they live in a kind of ideal space of
values” 型なし定数は、型の制約のない自由な理想空間 で生きている
Goの言語思想 1 1.000 1e3-99.0*10-9 '\x01' ‘a’ (= ‘0x97 ’ 97)
'\u0001' 'b' - 'a' 1.0+3i-3.0i これらはどれも 型なし定数 1 と同等に扱える
おまけ この辺は例外的な仕様 • string(97) // => "a" • string(97.0) //
コンパイルエラー
ご清聴 ありがとうございました