Skip to main content



      Home
Home » Modeling » TMF (Xtext) » function call operator?
function call operator? [message #1751227] Thu, 05 January 2017 15:23 Go to next message
Eclipse UserFriend
Today I scoured the XTend source code looking for an operator that would do something as simple as taking an input argument, binds it to the scope of a lambda expression and returns the output of the lambda. Maybe I searched wrong but I could not find anything like that.

In the end I implemented it as a method:

def <T, R> R call(T input, Function1<T, R> lambda) {
        lambda.apply(input)
    }


And it can be used like this:

1.call[i|i+1].call[j|j-1].assertEquals(1)


I just thought it could be useful to have an operator for this.

Questions:

1) Can I use custom operators in XTend?
2) Could we add this operation to the ObjectExtensions class?
Re: function call operator? [message #1751229 is a reply to message #1751227] Thu, 05 January 2017 16:00 Go to previous messageGo to next message
Eclipse UserFriend
(1) no you can not introduce custom operators, you can use/overload the existing ones only (see org.eclipse.xtext.xbase.scoping.featurecalls.OperatorMapping class)
in your case you could use e.g.
class Demo {
	def x() {
		1>>>[i|i+1]
	}
	
	def <T, R> R operator_tripleGreaterThan(T input, Function1<T, R> lambda) {
        lambda.apply(input)
    }
}


(2) feel free to file a ticket at https://github.com/eclipse/xtext-extras and/or https://github.com/eclipse/xtext-lib. the base problem is to find a "free" operator for that
Re: function call operator? [message #1751232 is a reply to message #1751229] Thu, 05 January 2017 17:43 Go to previous messageGo to next message
Eclipse UserFriend
When using an operator I apparently have to enclose the whole expression in parenthesis if I want to apply another operator.

For example this does not work:

def void test() {
    1 >>> [return 10] + 1
}


But this does:

def void test() {
    (1 >>> [return 10]) + 1
}


When the syntax used is a method call, the next operator can be written right after. I'm not sure I understand the reason why. Could it be related to operator precedence?

Re: function call operator? [message #1751248 is a reply to message #1751232] Fri, 06 January 2017 04:20 Go to previous message
Eclipse UserFriend
Yes, operator precedence is fixed.
As we're using Antlr,the precedence is defined by the call hierarchy in the Xtend grammar. You cannot change that on library level.
Previous Topic:Combining Xtext with a MySQL database
Next Topic:Keyword Highlighting In A Standalone Application?
Goto Forum:
  


Current Time: Thu May 15 09:54:43 EDT 2025

Powered by FUDForum. Page generated in 0.03067 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top