DEV Community

hiko1129
hiko1129

Posted on • Originally published at note.hiko1129.com on

[Ruby][Sorbet] Using T.unsafe to escape type constraints

Translate ja to en

typed: trueでもtyped: strictであってもT.unsafeを使うことで特定部分のみ型に基づかない状態で記述できます。

自分がよくやる(やるなよって話であるがTODOつけて一旦通すという意味でよくやる)のは下記のパターンです。

T.unsafe(self).hoge # selfにhogeメソッドは型定義上存在していないが実際には存在するとき
Enter fullscreen mode Exit fullscreen mode

RBIファイルに追記するまで、あるいは何かしらのconfigを書いてメソッドを生やす、あるいは型生成gemのバグ修正待ち、あるいはそもそも事前定義されている型が間違っているときには、上記のようにして一時対応したりしています。

参考

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️