I’ve been finding myself writing “classes” using the using ... for ... global
syntax, like so:
struct User {
uint id;
string username;
}
library UserLib {
function displayName (User memory self) public {
// ...
}
}
using UserLib for User global;
This becomes very similar to rust-style classes:
struct User {
id : u64,
username : String
}
impl User {
fn display_name(&self) -> String {
return format!("@{} ({})", self.username, self.id).to_string();
}
}
The syntactic sugar will be a new impl
directive, which basically transpiles into library
+ using ... for
. The following will be identical to the first snippet:
struct User {
uint id;
string username;
}
impl User {
function displayName (User memory self) public {
// ...
}
}
One issue I can think of would be that it becomes impossible to import the library on its own.
A possible solution might be to expose some reflection api, e.g.: type(User).impl
or something like that.
I also didn’t consider whether this would work for user-defined types.
Has something like this been considered already? I searched but couldn’t find anything.
WDYT?