Skip to content

Conversation

@goldeneggg
Copy link
Owner

@goldeneggg goldeneggg commented Jun 29, 2025

This pull request is an experiment by OpenAI Codex CLI to add new features.

  • setup OpenAI Codex CLI without AGENTS.md
  • run codex
  • send a prompt for adding GetAs generics support function
  • request a review after implementation

Summary

I used the prompt (in Japanese) as follows:

あなたはGo言語およびプログラミング言語の型に関するスペシャリストです。

あなたにはgetter.goに実装されているGetter型に、ジェネリクスを活用した汎用的なメソッド `GetAs` を実装してもらいます。

Getter型には、structの各フィールドにフィールド名指定でアクセスする処理が型単位で提供されています。
このリポジトリはGo1.19に準拠していてジェネリクスを使用できる状況にある為、今後はジェネリクスを活用した汎用的なメソッド `GetAs` を提供することとして、現在型単位で提供されているメソッドをいずれ廃止したいと考えています。

まずは現状のGettetの実装を読み込んで仕様を理解し、同じ仕様をジェネリクス活用して完璧に実現する為の `GetAs` メソッドを実装してください。また、getter_test.goに`GetAs`メソッドに関する網羅性が高いテストコードも追加してください。実装が完了したら、あなたの成果の解説をGETTER_GENERICS.mdに記載してください。

実装にあたって不明点があれば、適宜私に確認をしてください。

This prompt intentionally ignores the restriction that Go generics cannot define type parameters in method definitions.
The purpose of this pull request is to verify how OpenAI Codex behaves in response to such prompts.

In conclusion, OpenAI Codex realized my mistaken request midway through the implementation and proposed two workarounds (one of which is the result of this project).

This pull request contains the final "working" artifact.

Appendix

Total Costs

⚠️ The Codex CLI doesn't provide a command to check the cost or tokens consumed, so I instead opened the OpenAI Platform Settings > Billing > Credit balance in my browser to see how my credits were changing.

  1. Before work = 4.99 USD
  2. After implementation is complete = 2.68 USD
  3. After review is complete = 2.08 USD

Bookmarks

We conducted the same experiment with...

@goldeneggg
Copy link
Owner Author

After completing the implementation, we asked the following question (in Japanese) for reflection.

実装お疲れ様でした。最後に1つ振り返りをやりましょう。今回私は「Goのジェネリクスはメソッドに型パラメータを定義できない」という仕様を理解しておらずあなたを混乱させる依頼をしてしまいました。より良い実装は本来どういう内容にすべきだったでしょうか?
この質問には実装ファイルの修正は一切不要で、振り返りの感想だけを述べてください。

OpenAI Codex’s answer (in Japanese) are as follows:

codex
振り返りありがとうございます。今回のやり取りを通じて、「Go1.19 では非ジェネリック型のメソッドに型パラメータを付けられない」という仕様が抜け落ちていたため、最初にメ
ソッド案をお示しし、あとで急遽“自由関数”案に切り替える羽目になりました。

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

## よりベターな設計案

### 1. 自由関数 (今回採用)

Go1.19 ではメソッドに型パラメータを付与できないので、ジェネリック関数として実装するのが最もシンプルで安全な解です。

    // GetAs retrieves the field named "name" from g and attempts to cast its value to T.
    // The boolean reports whether the cast succeeded.
    func GetAs[T any](g *Getter, name string) (T, bool) { … }

GETTER_GENERICS.md (/Users/fskmt/github/goldeneggg/structil/GETTER_GENERICS.md)

### 2. Getter 型をジェネリックにする

トレードオフを許容できるなら、Getter 自体をジェネリック型にしてしまう方法もあります。

    // Go1.19 でも型パラメータを持つ型にはメソッドでさらに型パラメータを追加可能
    type GetterG[S any] struct { base *Getter }

    func (g *GetterG[S]) GetAs[T any](name string) (T, bool) { … }

    func NewGetterG[S any](s S) (*GetterG[S], error) { … }

こうすればメソッドとして書けますが、API が少し冗長になります。

### 3. 将来的にメソッド型パラメータが解禁されたら移行

Go1.21 以降でようやく「非ジェネリック型のメソッドにも型パラメータが付けられる」ようになれば、現状の自由関数 GetAs を func (g *Getter) GetAs[T any](…)
の形にリファクタできる想定です。
GETTER_GENERICS.md (/Users/fskmt/github/goldeneggg/structil/GETTER_GENERICS.md)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

### 結論

Go1.19 では最初から「自由関数」に落ち着けてしまうのが素直かつメンテナンス性も高い選択肢でした。
(あるいは型のジェネリック化を許容できるチーム/ライブラリ設計なら、Getter 自体をジェネリックにしてメソッド化もあり得る…というトレードオフです)

以上、ご参考になれば幸いです!

@goldeneggg goldeneggg changed the title add GetAs generics support function with OpenAI Codex (model is default) add GetAs generics support function with OpenAI Codex (codex-mini-latest) Jun 29, 2025
@goldeneggg goldeneggg changed the title add GetAs generics support function with OpenAI Codex (codex-mini-latest) add GetAs generics support function with OpenAI Codex CLI(codex-mini-latest) Jun 29, 2025
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.

2 participants