Thread overview
Poor Carlos does not manage to find where to eat
Sep 21
user1234
Sep 21
user1234
Sep 22
IchorDev
September 21
module m;

void placeToEat1(T)(T t)
    if (__traits(hasMember, T, "wantsBeer")
    && (__traits(hasMember, T, "wantsFries"))
    //&& (!__traits(hasMember, T, "wantsShrimps"))  // uncomment to help Carlos
    )
{
}

void placeToEat2(T)(T t)
    if (__traits(hasMember, T, "wantsBeer")
    && (__traits(hasMember, T, "wantsFries"))
    && (__traits(hasMember, T, "wantsShrimps")))
{
}

alias findPlaceToEat = placeToEat1;
alias findPlaceToEat = placeToEat2;

struct TouristType1
{
    enum wantsBeer = true;
    enum wantsFries = true;
}

struct TouristType2
{
    enum wantsBeer = true;
    enum wantsFries = true;
    enum wantsShrimps = true;
}

void main()
{
    TouristType1 Juan;
    Juan.findPlaceToEat();      // OK Juan will not starve to death
    TouristType2 Carlos;
    Carlos.findPlaceToEat();    // NG, Carlos will starve to death even tho
                                // placeToEat2 fits more to his needs
}

Shouldn't this work for Carlos ? placeToEat2 matches more after all.

September 21

On Saturday, 21 September 2024 at 04:29:15 UTC, user1234 wrote:

>
module m;

[...]

Beside the childish example... D could use a system of "constraint matching score" to help Carlos. Three AndAndExps verified should have a better score than two and finally determine the selection.

September 22

On Saturday, 21 September 2024 at 05:00:51 UTC, user1234 wrote:

>

On Saturday, 21 September 2024 at 04:29:15 UTC, user1234 wrote:

>
module m;

[...]

Beside the childish example... D could use a system of "constraint matching score" to help Carlos. Three AndAndExps verified should have a better score than two and finally determine the selection.

Template if constraints are binary, there’s no way to keep a ‘score’. If you want a new syntax for that, you can make a post in DIP ideas. Otherwise, the bools would have to keep track of ‘how many times a chained logical statement evaluated to true’ which is completely nonsensical.