INTRODUCTION
I'm a fan of UFCS, I probably think too much about it. Maybe here's a different direction towards UFCS.
The idea is that a simple language addition could (through some development)
lead to Extension Methods and UFCS.
This language addition might be called automatic casting.
CHAPTER 1: AUTO-CAST
Say that I want to call the
std::string member function find_first_of on a const char*string,
"Hello3". Of course, I can't call
"Hello3".findfirstof("123")
but it's easy if I first convert the C string into a
std::string: string("Hello3").findfirstof("123")
Maybe we could invent some syntax to tell the compiler to
auto-cast a
const char* to a string, where needed:// auto-cast const char to string.
auto operator string(const char s){return string(s);}
We have entered the realm of MMINAE (missing member is not an error). If the
compiler can't resolve a member function, it will then apply the user-defined
auto-casts (in order) until the casted value can resolve the member function, which
is then used.
More complicated auto-casts can be defined. For example, this will
auto-cast an int to a string, by converting the digits to ASCII:
auto operator string(int n){return tostring(n);}
Then this allows code like:
(654321).findfirstof("123")
which will, after some MMINAE scanning, convert this code to:
tostring(654321).findfirstof("123")
CHAPTER 2: EXTENSION METHODS
I'd like to extend
std::string by adding another member function, hasInt(int n).Not by actually going into the
<string> header file and adding a member function, but by creating some code to give that illusion.
First, I define a helper class that provides the
hasInt member function: struct StringHasInt : public string {
bool hasInt(int n){
return this->contains(tostring(n));
}
};
Then define an auto-cast from a `string` to a `StringHasInt`:
auto operator StringHasInt(string s){return staticcast<StringHasInt>(s);}
Thus, when I call
hasInt on a string: string text;
...
text.hasInt(123);
MMINAE scanning will activate, and resolve the missing member by converting to the code:
staticcast<StringHasInt>(text).hasInt(123);
**CHAPTER 3: UFCS**
So, if we want to "do the UFCS" and would like to get from:
bool hasInt(const string s, int n){...etc...
to
text.hasInt(123)
by a simple macro call:
MAKEUFCS(hasInt);
How is this done? The macro magic to convert this to a
helper class followed by an auto-cast is
left as an exercise to the reader!
https://redd.it/1r4z40v
@r_cpp