Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for version as expression #20788

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bangbangsheshotmedown
Copy link
Contributor

D is healing

enum value = version(linux) ? 42 : 0;

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @bangbangsheshotmedown! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#20788"

@bangbangsheshotmedown
Copy link
Contributor Author

bangbangsheshotmedown commented Jan 27, 2025

2025-01-27T10:13:52.1539227Z src\dmd\parse.d(8629): Error: no property `versionids` for `this.mod` of type `dmd.astbase.ASTBase.Module`
2025-01-27T10:13:52.1570805Z 
2025-01-27T10:13:52.1572527Z                 if (mod.versionids && findCondition(*mod.versionids, condition.ident))
2025-01-27T10:13:52.1575043Z 
2025-01-27T10:13:52.1605930Z                        ^

i don't understand, the code compiles on my machine

And i also realize that this will not work since it's not done parsing, perhaps someone can help

@dkorpel
Copy link
Contributor

dkorpel commented Jan 27, 2025

The parser is standalone, not dependent on dmd's semantic code. It is templated and can be instantiated both with DMD's AST (with extra semantic info), or 'astbase', a parser only version of the AST. Your code tries to access semantic info versionids which works when compiling dmd, but not in the test for using DMD as a library with astbase.

The duplicated astbase.d module is rather ugly and work is being done to remove it by refactoring dmd's ast to be separate from semantic routines (https://github.com/orgs/dlang/projects/41), but even so, the implementation of this feature should probably not try to resolve the version during parsing.

The proper implementation solution is to create a new Expression node similar to the VersionCondition node and resolve it during semantic. However, I would not spend much effort on this feature as it's likely going to be rejected. The addition of version algebra has been discussed to death without being fruitful.

@bangbangsheshotmedown
Copy link
Contributor Author

Thanks for the detailled explanation, i'll rework this PR

However, I would not spend much effort on this feature as it's likely going to be rejected. The addition of version algebra has been discussed to death without being fruitful.

Well, i disagree with that

Nobody will convince me that this is good:

version(linux)
{
    enum value = 42;
}
else
{
    enum value = 0;
}

@Geod24
Copy link
Member

Geod24 commented Jan 27, 2025

Has this been approved ? @WalterBright has been consistently against it.

@12345swordy
Copy link
Contributor

What problem are you trying to solve?

@bangbangsheshotmedown
Copy link
Contributor Author

What problem are you trying to solve?

The example in the change log entry is self explanatory, makes simple cases easier to process

8 lines of code vs just 1

Right now i solve it with:

struct Version
{
    static bool opDispatch(string identifier)()
    {
        mixin("
            version(", identifier, ")
                return true;
            else
                return false;
        ");
    }
}

This however feels like over-engineering for something that should be simple, and i am not a fan of having to import a module, i never remember wich file has this code and let's be honest, an import for this is cringe

@bangbangsheshotmedown
Copy link
Contributor Author

bangbangsheshotmedown commented Jan 27, 2025

That's the only thing i miss from C, having to include my utility file only once

Perhaps D could have something like C#'s global using, so i could have my utility module always available without having to import it everywhere

This would solve my issue

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#the-global-modifier

@Lars-Kristiansen
Copy link

That does not work. The change is sold as an expression but if so you have to support

void v(T)(T t);
void main()
{
    v!int(version(linux) ? 42 : 0);
}

such a feature requires much more work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants