Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » function call operator?
function call operator? [message #1751227] Thu, 05 January 2017 20:23 Go to next message
Eric Salemi is currently offline Eric SalemiFriend
Messages: 34
Registered: September 2016
Location: Belgium
Member
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 21:00 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 10799
Registered: July 2009
Senior Member
(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


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: function call operator? [message #1751232 is a reply to message #1751229] Thu, 05 January 2017 22:43 Go to previous messageGo to next message
Eric Salemi is currently offline Eric SalemiFriend
Messages: 34
Registered: September 2016
Location: Belgium
Member
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 09:20 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 737
Registered: July 2009
Location: Hamburg
Senior Member
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.


---
Get professional support from the Xtext committers at www.typefox.io
Previous Topic:[Highlighting] override text style
Next Topic:Keyword Highlighting In A Standalone Application?
Goto Forum:
  


Current Time: Thu Jun 29 07:36:48 GMT 2017

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

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