Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use (string, int) for names instead of just a string #232

Merged
merged 5 commits into from
Sep 14, 2021
Merged

Conversation

kmyk
Copy link
Collaborator

@kmyk kmyk commented Sep 14, 2021

説明

たとえば以下のようなコードがあったとする。

def f(n: int) -> int:
     n = n + 1
     n = n + 1
     n = n + 1
     return n * n

これは変数名が衝突していて面倒なので、内部的には

def f(n: int) -> int:
     n$2 = n + 1
     n$3 = n$2 + 1
     n$4 = n$3 + 1
     return n$4 * n$4

みたいに変数名を付け替えています。
このときに変数名全体をひとつの文字列として "n$3" みたいにして保持しています。
すると変数名をさらに付け替えるときに "n$3" から "n" を切り出して別の数字を付けて "n$5" にして…となにかと面倒です。そこで ("n", 3) というタプルで持つようにすると実装がきれいになります。

参考: https://hackage.haskell.org/package/template-haskell-2.17.0.0/docs/Language-Haskell-TH-Syntax.html#t:Name

@kmyk kmyk force-pushed the name branch 2 times, most recently from 75169b7 to 2b5ea85 Compare September 14, 2021 10:54
@kmyk kmyk merged commit b64e7ff into master Sep 14, 2021
@kmyk kmyk deleted the name branch September 14, 2021 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant