Implement xtend's extension methods? Follow
Hey,
i like xtend's extension methods very much, is it possible to implement this in MPS baseLanguage? and any hints?
http://eclipse.org/xtend/documentation/202_xtend_classes_members.html#extension-methods
i like xtend's extension methods very much, is it possible to implement this in MPS baseLanguage? and any hints?
http://eclipse.org/xtend/documentation/202_xtend_classes_members.html#extension-methods
Please sign in to leave a comment.
actually i'm trying to implement this local extension method using MPS(just for execise), but i find that i have not this skill to do that(typesystem issue). by the way, can you reply the previous ExtractStatementListExpression question?
At first, it looks like you're asking a general question, but then if you have some specific issue I could end up telling things that are obvious for you if I tried to say how I'd do this in general.
Have you done (or understand how to do) the AST, scoping and generator part of extension methods and only have problems with typesystem?
i will discribe my attempt till now very detailed,
and i just focus on the local field extension provider.
first i create a Extension node attribute:
concept Extension extends NodeAttriute
attributed concepts: FieldDeclaration
then i create a ExtensionFieldMethodCall concept:
concept ExtensionFieldMethodCall extends InstanceMethodCallOperation
and the constrains for link {instanceMethodDeclaration}:
find all the FiledDeclaration with Extension attribute, and all the declared method which first param type :==: DotExpression.operator
and the generator for DotExpression
condition: node.operation.isInstanceOf(ExtensionFieldMethodCall) ==>
extensionField.methodCall(operator, actuallParams)
till now all works for me,
first i got a "wrong number of parameters" error,
and of course , the first param is not here, so i copy and modify the checking rule, and it works.
(i must set checking rule's overrides attribute to true!)
then i got the param type checking error. i just copy and modify the InstanceMethodCall's typeof_rule, but the code is too long and too complicated and i can not done that(i just skip and comment it).
so my situation now is i can mark a field as extension, and i can call the extension method, and also i can generate right java code.
but i can not check the param type, can not chain the ExtensionFieldMethodCall.
please see the attactd source code, sorry i still not to know how to upload to github.
com.hpay.language.javaExtensions.rar (192KB)
Everything looks very good. Except I wouldn't inherit from InstanceMethodCallOp, rather have it separate instead. Yes, structurally they're the same: both only have a reference to a method decl. But semantically different. This way you wouldn't have to fight every place in the existing code that works with InstMethodCalls. You found a couple immediately (checking rules, typing rules) but there are more.
So, ExtensionMethodCall extends BaseConcept implements IOperation. And probably not IMethodCall, because it does the argument count check.
Now, you would have to implement your own checking/typing rules. Unfortunately, as far as I know there is no easy way to reuse the typing rule for InstMethCall per se. So, copying it and gradually editing to your needs is probably the way to go.
Of course, it isn't good from software engineering point of view, as the two instances of that code may diverge, but... But then again, there might be some peculiarities with extension method calls that need to be addressed specifically, in their own typing rule.
I think type checking method calls is easily the most complicated aspect of java type system, due to generics. That's why the code there is what it is. So, you have to carefully go through it and change according places.
For example,
should be replaced with something like
Of course, you would also need to change iterations over argument lists. Try it and we'll see what problems come next.
and i must copy IMethodCall's editor component:
IMethodCall_typeArguments -> IExtensionMethodCall_typeArguments
IMethodCall_actualArguments -> IExtensionMethodCall_actualArguments
then i need add typesystem rules to IExtensionMethodCall,because i'm still can not handle generics, so i just write a simple rule:
and it works, but not support generics(it will take time to familiar with that).
by the way ,use IExtensionMethodCall interface concept, i can easily add extension local method support and extension library method support very quickly.
so, i think i have made a real progress,
thanks very much!
i got this error when i call a extension method in try block, i try to resolve this but found that i cann't.
the code in Statement_Behavior's collectUncaughtMethodThrowables method:
it collect the throwsItem from IMethodCall and DefaultClassifierMethodCallOperation, and i can not override this function to add IExtensionMethod's throwsItem.
i think baseLanguage should add interface IThrowable, which have a method "getThrowsItem", then change the collectUncaughtMethodThrowables method to :
or any other option?