Rfcs: Allow wildcard "_" as const pattern

Created on 11 Dec 2017  路  6Comments  路  Source: rust-lang/rfcs

One of the things holding static_assertions from becoming 1.0 is a restriction on where the wildcard identifier is allowed. I'm requesting that consts may be allowed to use _ just like with let bindings.

T-lang

Most helpful comment

const destructuring (const (A, B): (&str, i32) = ("foo", 0);) might be a very nice addition, and then the _ patterns would come "for free". cc @nikomatsakis

All 6 comments

Nit: _ is a pattern, not an identifier. To me it'd seem odd to support only _ and not more general patterns (but general patterns do not seem useful for constants).

I feel this issue would be better simply by having some method--any method--of generating a hygienic name for an item (a const, fn, mod, etc.) inside a macro just like we are capable of doing (or perhaps, rather, forced to do) for local variables.

For static_assertions though you don鈥檛 actually want a const item, only the side effects of evaluating its initializer, right?

@SimonSapin yes, I'm mainly after the side effects. This is one way I can think of achieving what I want with minimal effect on the language. This is also a backwards-compatible change.

A const item using _ would allow for using the compile-time side effects multiple times within the same context without naming conflicts.

const destructuring (const (A, B): (&str, i32) = ("foo", 0);) might be a very nice addition, and then the _ patterns would come "for free". cc @nikomatsakis

I actually spoke to Niko about this a month ago. He recommended to add something like my const_assert macro to the language instead. The issue is that I need this issue for other macros within this crate as well.

@eddyb I really like the idea of adding constant destructuring to the language! This should also work for statics.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

p-avital picture p-avital  路  3Comments

marinintim picture marinintim  路  3Comments

steveklabnik picture steveklabnik  路  4Comments

burdges picture burdges  路  3Comments

clarfonthey picture clarfonthey  路  3Comments