Using typeof in Xtend - xtend

It might be the stupidest question on Xtend. But here it goes
I'm trying to convert the following to Xtend
if (obj instanceof Integer) {
message.reply((Integer)obj);
}
I know that I would need to use the typeof function.
Can I use typeofas a direct replacement to instanceof?

typeof is an old syntax to retrieve the class object. If you're using a newer version of Xtend this is no longer required. Apart from that the instanceof syntax is just like in Java but the cast statement is different so you would write:
if (obj instanceof Integer) {
message.reply(obj as Integer)
}
Since Xtend 2.5 auto-casting is supported so you can even write:
if (obj instanceof Integer) {
message.reply(obj) // obj is automatically casted to Integer
}

Related

Error returned when trying to run a basic function in dart

I'm new to dart (coming from a python background), and I'm struggling to make a function work. I've created the following function:
void main() {
int number = 22;
Function evenChecker(int number) {
if ((number%2) == 0) {
print('$number is even');
} else {
print('$number is odd');
}
}
}
but is get the following error:
Error: A non-null value must be returned since the return type 'Function' doesn't allow null.
bin/hello_dart_project.dart:4
- 'Function' is from 'dart:core'.
Additionally, if anyone has any suggestions about a good dart learning resource, I'd highly appreciate the advice.
^
Unlike Python or JavaScript, there is no keyword to define functions in dart. Instead, we declare functions by starting with their return type:
void evenChecker(...) {...}
^^^^
// This is the return type. "void" means "returns the value null" or "doesn't return anything"

Spring Jpa Projection interface occur error with Boolean type

Q. Why JPA Projection can't convert Mysql bit(1) to Java Boolean?
Spring Jpa Projection occur error Projection type must be an interface! when the Mysql bit(1) type maps to the Java Boolean type.
Jpa converts a Boolean column in Entity class to bit(1) column in Mysql Table.
If I change getIsBasic's type in PlanInfoProjection interface Integer to Boolean, It doesn't work. Why does it occur error?
JPA Repository
#Query(nativeQuery=true, value="select true as isBasic from dual")
ProductOrderDto.PlanInfoProjection findPlanInfoById(Long id);
Projection interface
public class ProductOrderDto {
#Getter
public static class PlanInfo {
private Boolean isBasic;
public PlanInfo(PlanInfoProjection projection) {
// this.isBasic = projection.getIsBasic(); //<-- I want to use like this.
if (projection.getIsBasic() == null) {
this.isBasic = null;
} else {
this.isBasic = projection.getIsBasic() == 0 ? false : true; // <-- I have to convert
}
}
}
public interface PlanInfoProjection {
Integer getIsBasic(); // It works, but I have to convert Integer to Boolean to use.
//Boolean getIsBasic(); // doesn't work, but why???
//Boolean isBasic(); // also doesn't work
//boolean isBasic(); // also doesn't work
}
}
It seems like this doesn't work out of the box. What works for me (although I'm using DB2 so my datatype is different but this shouldn't be a problem) is to annotate it and use SpEL like this:
#Value("#{target.isBasic == 1}")
boolean getIsBasic();
This just takes your int value (0 for false, 1 for true) and creturns a boolean value. Should also work with Boolean but I didn't test it.
Another option is to use #Value("#{T(Boolean).valueOf(target.isBasic)}") but this only works for String values, so you would have to store 'true' or 'false' in your database. With T() you can import Static classes into Spring Expression Language, and then just call the valueOf method which returns a boolean (either Boolean or boolean)

Underscores in MvvmCross data binding

I'm using the fluent syntax and lambdas for data binding in MvvmCross. An example of this is:
var bindings = this.CreateBindingSet<MyTableCell, MyTableCellViewModel>();
bindings.Bind(titleLabel).To(vm => vm.MY_TITLE);
bindings.Apply();
Whenever I try this with an underscore in a view model property I get an exception:
Cirrious.CrossCore.Exceptions.MvxException: Unexpected character _ at
position 3 in targetProperty text MY_TITLE
I believe the error message is a result of MvvmCross parsing the data binding, yet this seems to only make sense for people using string-based data binding, not the lambda expression syntax.
Unfortunately, I cannot change the view models so I'm looking for a workaround to allow underscores in the view models. Any ideas?
I'd guess this is a general problem in the MvvmCross parser - probably in
private void ParsePropertyName()
{
var propertyText = new StringBuilder();
while (!IsComplete && char.IsLetterOrDigit(CurrentChar))
{
propertyText.Append(CurrentChar);
MoveNext();
}
var text = propertyText.ToString();
CurrentTokens.Add(new MvxPropertyNamePropertyToken(text));
}
In https://github.com/MvvmCross/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding/Parse/PropertyPath/MvxSourcePropertyPathParser.cs#L80
Which probably needs to be fixed to something like:
while (!IsComplete &&
(char.IsLetterOrDigit(CurrentChar) || CurrentChar == '_')
There are workarounds you could do, but the easiest solution is probably to fix this and rebuild, rather than to try workarounds.
But if you do want to try workarounds....
Assuming this is static (non-changing) text and this is just a one-off for now, then one workaround might be to add a property to your cell called Hack and to then bind like:
bindings.Bind(this).For(v => v.Hack).To(vm => vm);
//...
private MyTableCellViewModel _hack;
public MyTableCellViewModel Hack
{
get { return _hack; }
set { _hack = value; if (_hack != null) titleLabel.Text = _hack.MY_VALUE; }
}
Another alternative (with the same assumptions) might be to use a value converter -
bindings.Bind(titleLabel).To(vm => vm.MY_TITLE).WithConversion(new WorkaroundConverter(), null);
// ...
public class WorkaroundConverter : MvxValueConverter<MyTableCellViewModel, string>
{
protected override string Convert(MyTableCellViewModel vm, /*...*/)
{
if (vm == null) return null;
return vm.MY_TITLE;
}
}

Interface function returning untyped value (*) while implementation return specific type?

Out of curiosity I was wondering if it was possible to have an interface definition returning untyped values, while its implementations return typed value ?
For example, having the following interface :
interfaceExample
{
function someExampleFunction():*
}
implemented the following way :
classA implements interfaceExample
{
public function someExampleFunction():Int{ ... }
}
classB implements interfaceExample
{
public function someExampleFunction():String{ ... }
}
( I've googled with no result, maybe I haven't been able to ask the right way )
I guess the answer is 'no it's not possible in any way, Interface aren't designed like that' -- so what could be a friendly way to achieve such a thing ?
You're right, this won't compile. This is because the compiler doesn't support overloading of the signature of methods from parent classes or interfaces.
If you think about it, it doesn't really make sense to override the return type when implementing an interface because the code consuming your interface API should not depend on the implementation (int or String return value).
The solution to use really depends on what you are trying to do in the end. In general, the return type should be the common denominator between the different types you could produce. This could also be another interface that describes what to expect from the return value.
While I'm not heavily familiar with interface definitions, I do know that I write a lot of functions that work with typed and untyped arguments and returns.
In the event that what you're describing is not possible, and depending on what you're doing, I would recommend writing a simple function that can convert an untyped variable into a typed variable. They're fairly simple. Then, you could call this function to handle whatever data comes in via the interface definition.
A rough example (untested):
function convertDataTo(type:String, input:*):*
{
var rI:int;
var rN:Number;
var rS:String;
var rB:Boolean;
switch(type)
{
case "int":
if(isNaN(int(input))
{
rI = 0;
}
else
{
rI = int(input);
}
break;
case "Number":
if(isNaN(Number(input))
{
rN = 0;
}
else
{
rN = Number(input);
}
break;
case "Boolean":
if(input == "true")
{
rB = true;
}
else if(input == "false")
{
rB = false;
}
else
{
rB = Boolean(input);
}
return rB;
break;
case "String":
rS = input.toString();
break;
default:
trace("Invalid data type!");
return 0;
break;
}
}
You can also rewrite this function to convert from String type to any other type, and have your interface definition always return strings. On the other end, you can have a similar function that converts all variables to strings that can be interpreted by this function.
By the way, I've had to use that kind of string interpretation in writing my own scripting language for use inside one of my larger projects.
Of course, I don't pretend this answers your question directly, but I wanted to throw out this contingency in case what you're looking for doesn't work.

How do I see common Items between 2 array of Objects

How do I see common Items between 2 array of Objects. My intersect is not returning anything. The object is created from a Linq to SQL class.
In Java atleat, unless you override the .equals() operator, it will test for object equality (essentially using ==). That might be why the intersection is emtpy.
Did you override the Equals method?
You will need to override the object's Equals method. You can find some guidelines at Microsoft's web site.
I've provided a sample below:
public override bool Equals(System.Object obj)
{
if (obj != null && obj is MyClass)
{
MyClass obj2 = (MyClass)obj;
return (obj2.ID == this.ID);
}
}
If you do not override this method, any kind of sort/intersect/comparison will compare the objects based on their reference; so, if two objects refer to the same spot in memory, they are considered "equal."
You will have to sort them, of course.