www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Poor Carlos does not manage to find where to eat

reply user1234 <user1234 12.de> writes:
```d
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.
Sep 20
parent reply user1234 <user1234 12.de> writes:
On Saturday, 21 September 2024 at 04:29:15 UTC, user1234 wrote:
 ```d
 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.
Sep 20
parent IchorDev <zxinsworld gmail.com> writes:
On Saturday, 21 September 2024 at 05:00:51 UTC, user1234 wrote:
 On Saturday, 21 September 2024 at 04:29:15 UTC, user1234 wrote:
 ```d
 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.
Sep 22