Beef icon indicating copy to clipboard operation
Beef copied to clipboard

[Enhancement] Conditional attribute from c#

Open xposure opened this issue 5 years ago • 3 comments

Beef has the concept of SkipCall which requires you to wrap it in #if which can muck up the code. Where .NET has the Conditional attribute that takes an argument and will perform a skipcall if its defined.


[Conditional("DEBUG")]
public static void Asset(bool cond){
  if(!cond) Runtime.FatalError();
}

xposure avatar Dec 11 '20 15:12 xposure

A little update to this issue, with the Comptime feature it's now possible to hack together the Conditional attribute, currently the issues with my current approach are:

  1. It's not possible to iterate over preprocessors, so you have to add the preprocessor in the attribute yourself.
  2. If it's necessary to use a custom preprocessor, the attribute declaration will need to be in the project that is using the attribute or you will need to add the preprocessor also in the project that has the attribute declared.
  3. Currently it's not possible to skip the call, so the arguments will be evaluated.
  4. You will have a warning of unreachable code when the attribute "skips the method".

Here is the code:

	[AttributeUsage(.Method)]
	struct ConditionalAttribute : Attribute, IComptimeMethodApply
	{
		public StringView mPreprocessor;
		public this(StringView preprocessor)
		{
			mPreprocessor = preprocessor;
		}

		[Comptime] 
		public void ApplyToMethod(ComptimeMethodInfo method)
		{
			//Compiler.Assert(method.ReturnType.[Friend]mTypeCode == .None);

#if DEBUG
			if (mPreprocessor == "DEBUG")
				return;
#elif RELEASE
			if (mPreprocessor == "RELEASE")
				return;
#endif

			String emit = scope .();
			emit.Append("return;");
			Compiler.EmitMethodEntry(method, emit);
		}
	}

Igoorx avatar Jan 19 '21 23:01 Igoorx

I don't know about Beef, but in c# if you call something thats conditional the entire statement is removed, for example For(i++) in release mode i++ would never happen if Foo was set for DEBUG only, It seems that is the only piece missing with your example?

xposure avatar Jan 20 '21 14:01 xposure

Two general features could enable ConditionalAttribute directly:

  1. Allow ApplyToMethod to directly set attributes on a method such as SkipCall.
  2. Allow checking preprocessor defines in System.Compiler

bfiete avatar Jan 20 '21 15:01 bfiete