# Pitch: Introduce #module to get the current module name

**URL:** <https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806>\
**Category:** Pitches\
**Created:** [March 17, 2021, 9:40am UTC](https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806 "2021-03-17T09:40:50Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [March 17, 2021, 8:35pm UTC](https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806/21 "2021-03-17T20:35:24Z")

</div>

> [@davedelong](#):
>
> I'm not sure that `StaticContext` (to give it a name) would need to be passed around like a regular struct, because like `StaticString` , the data would live in read-only sections of the binary itself.

Right, that's what I meant by

> [@johannesweiss](#):
>
> Maybe we could put all the `#context` values that appear in the whole program in a special section of the binary but that also doesn't immediately seem like a great idea.

The difficulty with that is that the user could then not easily change a bit of the `#context` without a heap allocation. That may be acceptable, or it may not be. Also it'd still occupy quite a bunch of space in the binary. But yes, it's a possibility.

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [March 17, 2021, 8:53pm UTC](https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806/22 "2021-03-17T20:53:07Z")

</div>

You're right, I missed that! Thanks for pointing it out 🙇‍♂️

I don't pretend to know a lot about efficient implementations; I'm still quite unfamiliar with Swift's inner workings compared to my knowledge of Objective-C. I'm approaching this from the POV of ergonomics and API design, and my own experience of getting annoyed at having to splat `file: String = #file, line: Uint = #line, function: String = #function` in lots of places and manually forwarding on 3 different parameters.

To come back to the original proposal… I _do_ think it could be very nice to have `#module` as a compiler-provided value and am interested in seeing this proposal move forward because of that. However, I'm wary because we keep coming up with these values (you made a nice list of them) and I wish there was a nicer way to package them all up and make them more ergonomic in code.

---

<div class="post-metadata">

**Author:** ![davidjennes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davidjennes/32/14795_2.png) [@davidjennes](https://forums.swift.org/u/davidjennes)\
**Post date:** [March 18, 2021, 1:53am UTC](https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806/23 "2021-03-18T01:53:36Z")

</div>

Slight nitpick: you counted `file` twice I think, so the totals should be 13 words & 18 words.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [March 20, 2021, 1:44pm UTC](https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806/24 "2021-03-20T13:44:04Z")

</div>

> [@johannesweiss](#):
>
> Maybe we could put all the `#context` values that appear in the whole program in a special section of the binary but that also doesn't immediately seem like a great idea. Not sure, what do people think?

Simply storing all the relevant info for every occurrence of `#context` might be too expensive in binary size, but could we trade binary size for performance of actually accessing properties on the context object by having a lookup table that exploits the nested nature of modules, files, lines and columns? In that case, unless I'm missing something, a single word parameter might suffice for passing the context.

If that turns out too slow, maybe a happy medium could be found (e.g. keep column and line in the context struct, the rest in the lookup table) that would provide the right level of convenience, performance and binary size for most users.

---

<div class="post-metadata">

**Author:** ![hoon-prestolabs](https://avatars.discourse-cdn.com/v4/letter/h/b4bc9f/32.png) [@hoon-prestolabs](https://forums.swift.org/u/hoon-prestolabs)\
**Post date:** [November 13, 2023, 1:34am UTC](https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806/25 "2023-11-13T01:34:38Z")

</div>

For people who are looking for this feature.

We now have `#fileID` which contains module name at first segment.

- [fileID() | Apple Developer Documentation](https://developer.apple.com/documentation/swift/fileid())

---

<div class="post-metadata">

**Author:** ![mredig](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mredig/32/22501_2.png) [@mredig](https://forums.swift.org/u/mredig)\
**Post date:** [March 4, 2024, 6:51pm UTC](https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806/26 "2024-03-04T18:51:01Z")

</div>

+1 to this proposal. Stumbled upon the post while searching how to do exactly this. I also concur with the OP that the `#fileID` hack is just that: useful, but ultimately a hack. While the value would be generated at compile time, it will require string processing at runtime to remove the extraneous information.

That's not so horrible for my immediate need, but I can _totally_ see that, for situations such as logging, this would be undesired overhead.

[Previous page](https://forums.swift.org/t/pitch-introduce-module-to-get-the-current-module-name/45806.md?page=1)
