-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error using generic type inside a Callable #204
Comments
I've tried to work around the problem, without any success: $ emojicodec parser.π
parser.π:74:23: π¨ error: No such generic type variable "A".
πππ₯‘ stringπ‘ β‘οΈ π¬ππAπ
β¬οΈ
$ emojicodec parser.π
parser.π:74:8: π¨ error: Unexpected token Generic.
πππ₯‘πAβͺπ stringπ‘ β‘οΈ π¬ππAπ
β¬οΈ
$ emojicodec parser.π
parser.π:74:6: π¨ error: Unexpected token Generic.
ππAβͺπππ₯‘ stringπ‘ β‘οΈ π¬ππAπ
β¬οΈ It seems that Callables cannot be generic, or use any generic parameters inside them? I'd be just as happy to just pass a reference to a generic method (as something like a Delegate) instead of using a Callable, but I have not discovered a way to do that either. |
Thanks for raising this interesting issue. I agree that it should work. The way the compiler is implemented, however, is that it does not use the method's type context when analysing closures. Instead a new type context similar to the one inπ is used, which is why you cannot access any generic type variables. I think this could be fixed by passing down the type context when analysing the closure but I'm not sure if that would have any side effects. Closures themselves cannot have generic type parameters as callables cannot be generic themselves. If we wanted to allow that, fixing the above would be more complicated and involve keeping a list of parent functions in the type context.
I'm not exactly sure I understand what you want to do, but have you looked into solving this using an OO approach. Maybe you can define a protocol for your delegate or use a functor? |
Sorry for the ambiguous comment. Words are hard. What I mean is something like what I could do in other languages such as JavaScript: function doSomething(func) {
func('hello');
}
doSomething(console.log); In this example,
This would work, but is limited to a single implementation for that protocol for that class. From the example above, we wouldn't be able to do an equivalent of: doSomething(console.log);
doSomething(console.warn);
doSomething(console.info);
doSomething(console.error); Instead it's the equivalent of doSomething(console); where class Log {
log(val) { console.log(val); }
}
const log = new Log();
doSomething(log);
class Warn {
log(val) { console.warn(val); }
}
const warn = new Warn();
doSomething(warn);
class Info {
log(val) { console.info(val); }
}
const info = new Info();
doSomething(info);
class Error {
log(val) { console.error(val); }
}
const error = new Error();
doSomething(error); In conclusion, it seems that EmojiCode is designed for OOP and not Functional programming. Is that correct? Is there any path to making either work? |
You can always create a closure to bind a method to a specific object. For example, to create a callable that invokes π‘ on
This would be equivalent to the following JavaScript:
Note that Emojicode is certainly not Haskell, but you should be able to write functional programs as in other multi-paradigm languages just fine. |
I'm trying to create a generic method that returns a callable using the generic type, but it throws an error. Here's the minimal code required to trigger the issue:
which results in the following compiler error:
I've reviewed the Callables documentation as well as multiple others parts of the Language Reference and Package Index several times, but I'm not seeing anything I'm missing. How do I accomplish what I'm trying to do (use an outer generic type parameter inside a callable)?
P.S. It doesn't matter whether I use
ππ₯‘
or not, I get the same error.The text was updated successfully, but these errors were encountered: