Alternative Possibility - Implicit with

H. S. Teoh hsteoh at qfbox.info
Sun Nov 20 01:26:25 UTC 2022


On Sun, Nov 20, 2022 at 01:07:14AM +0000, zjh via Digitalmars-d wrote:
[...]
> Can I open `one or more` 'enum' namespaces for a function, because
> sometimes, enum values are continuous,and you do not need `'switch'`
> statements. At this time, I still want to omit `'enum'` names' to use
> the `values`.
> 
> Similarly, can we also open the 'namespace' of `one or more` classes
> or structs for a function.
>
> In this way, just like a `'free functions'`, you can directly use the
> 'static' function in the 'class/struct'.
> 
> How about this?

This is exactly what `with` is designed for:

	enum MyLongEnum { LongValue, AnotherLongValue, YetAnotherLongValue }
	auto myFunc(Args...)(Args args) {
		with (MyLongEnum) {
			auto e = LongValue;
			... // etc.
		}
	}

Currently, `with` supports only one identifier; this could be enhanced
to allow multiple identifiers so that you don't need to have 3-4 nested
blocks just to have 3-4 implicit identifiers:

	enum MyLongEnum { ... }
	class MyClass { ... }
	struct MyStruct { ... }
	with (MyLongEnum, MyClass, MyStruct) {
		... // use members of all 3 without needing to spell them out every time
	}


T

-- 
Long, long ago, the ancient Chinese invented a device that lets them see through walls. It was called the "window".


More information about the Digitalmars-d mailing list