".." in "use" statement

image

Why is this not in the language yet? Why can't I just rename some items and then import the rest as-is, without importing the unrenamed versions too (unlike the * operator)?

I hope, if the demand is strong enough, it will end up as an accepted RFC.

Update:

Since everyone seemed to see a reference to dispatch syntax here, I'd like to clarify that the .. was actually taken from the struct update syntax: foo = Foo { field2: new_value, ..foo }. (By the way, even in pattern matching like let Foo { field2, .. } = foo, .. clearly means "the rest").

As for the discouraged * operator, I completely agree that it's a bad way to import anything from a library. However, in my example, this line was located in a dedicated code block dealing with variants of the NixValue enum, which was also inside a function body. Therefore, any potential issues it could cause were highly localized.

Ultimately, the only valid use case I see for .. is as a local shortcut for readability, not as a general import mechanism.

I expect we haven't added it because we already tend to discourage * imports, so we haven't done much in the area of those. Like those, this would have semver hazards. (Less so when importing an enum's variants; more so when importing a module.)

As another similar approach in another language, Haskell would use import qualified X hiding (y, z). We could have use NixValue::{* hiding String, String as NixString}.

That said, that requires duplicating String; use NixValue::{String as NixString, ..} has the virtue of not duplicating. It's an interesting idea.

1 Like

.. usually means "ignore the rest" though, or perhaps "act like _ for the rest". (_ already has a special meaning in this context when used on traits -- methods become dispatchable but the trait name isn't imported.)

Perhaps ..* would be readily interpretable as "treat the rest as *". (Not that I’m in favor of this feature overall.)

Moreso "I don't care" than "ignore". Default field values uses .. to set a field, not ignore it.

1 Like

True upon reflection. Also, name @ .. patterns.

(The "dispatchable but not nameable" aspect of _ trait imports was the first thing I thought of when seeing the snippet for whatever reason.)

(post deleted by author)

Implicit trait imports are a bad idea in the first place. And as I mentioned in the update, this feature is mostly for shortcuts, so all the traits you need should already be in scope before a use like this.

I don't really think the example holds up for local readability even, now whenever I see an identifier in this local scope I have to guess whether it's referring to a variant of the enum, or something in an outer scope, or is a new declaration (in pattern position). If writing NixValue is really such a burden in one function I would much prefer something like use NixValue as NV; NV::String etc.

IMO that would introduce surprising complexity (difference in behavior).