Open TekExplorer opened 3 days ago
I'm not really a fan of this feature, mostly because i don't really see a problem it solves, that a normal function doesn't. Features are not added just because they are interesting. Another problem i see, is that it feels very "meta", sort of like macro, but instead of generating boilerplate code, it changes how the entire language behaves. It's also quite difficult to read, and possibly ambiguous in some cases.
That being said, here are my suggestions:
1) Make it a class - there is no need to add a completely new construct to the language, instead just do the same thing macros
did, and make it a special class
// small syntax change
generator class StringBuilder{
final buffer = StringBuffer();
operator yield(String str) => buffer.write(str);
operator yield*(Iterable<String> strs) => buffer.writeAll(str);
String operator return() => buffer.toString();
}
2) It's possible to make a general function class
that would work as a framework on which to build a custom generator
// yield and yield* come from GeneratorFunction
function class StringBuilder<T> implements GeneratorFunction {
final buffer = StringBuffer();
@override
operator yield(String str) => buffer.write(str);
@override
operator yield*(Iterable<String> strs) => buffer.writeAll(str);
@override
String operator return(T retValue) => buffer.toString();
}
And the reason i would do this is because of it giving it a greater use case, for example having a go-like defer
, or to wrap the result.
function class DeferredStatement<T> implements GeneratorFunction {
List<Function> deferredFuncs = [];
@override
operator yield(void Function() func) =>deferredFuncs.add(func);
@override
T operator return(T retValue){
for(final func in deferredFuncs){
func();
}
return retValue;
}
}
String doSomething() DeferredStatement {
// i KNOW that in Dart the files are automatically closed, but it's just demonstration
File f = File("text.txt");
yield () => f.close();
return f.readAsStringSync(); //after return runs the yielded function
}
It's also possible to wrap the result in somehting like this
// CatcherFunction -> interface for catching exceptions that occured in function
function class ResultWrapper<T> implements CatcherFunction {
@override
Result<T> catch(T err) => Failure(err);
@override
Result<T> operator return(T retValue) => Success(retValue);
}
String toThrowOrNotToThrow() ResultWrapper {
String out = funcCanThrow(); // if throws -> Failure
return out; // success
} // actual return type is Result<String> bcs of ResultWrapper
Possible problems of my proposal, is that it obfuscates the return type of the function, which is not ideal. A possible solution is to have the returns
hint that was in your proposed syntax.
That being said, it still is very meta
, so i'm not really sure if it's good to have this before we have macros. Also, for such a big change in the language (both yours, and my proposed syntaxes), extensive discussion is needed to make it work. As such, @lrhn, could you please provide feedback.
My standpoint on either of the proposals is that it would be good to have, but the benefit of having this is not really proportional to the difficulty of implementing it correctly
This sounds like monads, which means it's probably something that won't work well with the Dart type system if you try to abstract over it. That requires a higher-order type system which Dart doesn't have.
If you don't try to generalize, then it's more like normal functional programming.
String stringBuilder(
void Function({required void Function(String) yield, required void Function(Iterable<String>) yieldAll}) body) {
var buffer = StringBuffer();
body(yield: buffer.write, yieldAll: buffer.writeAll);
return buffer.toString();
}
String writer(Input input) => stringBuilder(({yield, yieldAll}) {
while (imput.something) {
yield(input.value);
}
yieldAll(input.rest);
});
You can even abstract over writers:
typedef Writer<R, T> = R Function(
void Function({required void Function(T) yield, required void Function(Iterable<T>) yieldAll}));
but not over "body modifiers" in general, which means that it's likely not a good general language feature.
I think it could be really interesting if we could create our own generators like
async
,async*
, andsync*
I noticed that really, a lot of the features present in each kind already exists in normal code, ala
Completer
,StreamController
, and custom iterablesI looked at
yield
andyield*
and equated it toStreamController.add
andStreamController.addAll
I recognized thatasync*
returned anIterable<Future<T>>
, whichStream
implements, not dissimilar tosync*
I looked atawait
, and recognized how it flattened the callback hell of.then
s by just giving us the value and early returning theCompleter.future
I looked at these and realized they all return before a single line of code ever runs.
And I looked at #2567 https://github.com/flutter/flutter/issues/25280 and https://github.com/flutter/flutter/issues/51752 and remembered how @rrousselGit mentioned that "hooks" would be a language feature akin to doing
final x = use thing;
So the feature that comes to mind is, what if we could make our own functional generators? extend the dart language "directly"?
What if we could do something like:
and of course, that logic can be anything.
a naive example:
simple, but it shows how simple it could be, yet gives a ton of flexibility in terms of power and complexity isn't declarative code better?
I think this can be a super powerful feature, allowing custom(ish) syntax to exist in packages (or sdks, hint hint) which could possibly simplify a lot.
It would make algebraic effects unnecessary since you'd have to provide them in the returned object before you can use any data.
the presence of
yield
would also provide "free" lazy loading for certain cases, like it does with stream and iterableexisting generators like
async
,async*
andsync*
could even be implemented in this feature, which would have the benefits of being able to look at its implementation (for better understanding and discovery) and also make it possible to add documentation to it.Sometimes I try to hover over
await
oryield(*)
to see what they would say and... nothing.we can even reuse some existing syntax -
await
,yield
etc would be definableoperator
sThere's a lot of potential, and this would be a pretty major feature, so I'd like to hear some thoughts!