Discussion:
How to declare a multidimensional array?
f***@public.gmane.org
2013-05-01 12:39:55 UTC
Permalink
What's Xtend's syntax equivalent for Java's multi-dimensional array syntax:
e.g.
Type[][] matrixData = new Type[Dim1][Dim2];?

NewArrayOfSize() is only set up for single dimensions.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Sven Efftinge
2013-05-01 18:25:16 UTC
Permalink
It's not supported out-of-the box. You can add support by declaring a Java class like this :

public class ArrayLiterals2 {

@Inline("new int[$1][$2]")
public static int[][] intArray(int outerSize, int innerSize) {
throw new UnsupportedOperationException();
}

}


and import it like this in Xtend :

import static extension my.lib.ArrayLiterals2.*

…

val int[][] arr = intArray(10,20)


@Inline tells the compiler to replace any calls to the annotated method with the to-be-inlined Java expression (the 'value' template).
Post by f***@public.gmane.org
e.g.
Type[][] matrixData = new Type[Dim1][Dim2];?
NewArrayOfSize() is only set up for single dimensions.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
v***@gmail.com
2014-09-27 17:54:45 UTC
Permalink
Hi,

Thanks, for the beginners' sake, could you please also give the syntax how
to get and set arr[x][y]?

Cheers,
Attila
Post by Sven Efftinge
It's not supported out-of-the box. You can add support by declaring a Java
public class ArrayLiterals2 {
@Inline("new int[$1][$2]")
public static int[][] intArray(int outerSize, int innerSize) {
throw new UnsupportedOperationException();
}
}
import static extension my.lib.ArrayLiterals2.*


val int[][] arr = intArray(10,20)
@Inline tells the compiler to replace any calls to the annotated method
with the to-be-inlined Java expression (the 'value' template).
likely be removed once Xtend has support for more powerful method-macros.
e.g.
Type[][] matrixData = new Type[Dim1][Dim2];?
NewArrayOfSize() is only set up for single dimensions.
--
You received this message because you are subscribed to the Google Groups
"Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sven Efftinge
2014-09-27 19:16:30 UTC
Permalink
Hi Attila,

use 'get' as in :

val int[][] arr = #[#[1,2], #[3,4]]
assertEquals(3, arr.get(1).get(0))

Sven
Hi,
Thanks, for the beginners' sake, could you please also give the syntax how to get and set arr[x][y]?
Cheers,
Attila
public class ArrayLiterals2 {
@Inline("new int[$1][$2]")
public static int[][] intArray(int outerSize, int innerSize) {
throw new UnsupportedOperationException();
}
}
import static extension my.lib.ArrayLiterals2.*
...
val int[][] arr = intArray(10,20)
@Inline tells the compiler to replace any calls to the annotated method with the to-be-inlined Java expression (the 'value' template).
Post by f***@public.gmane.org
e.g.
Type[][] matrixData = new Type[Dim1][Dim2];?
NewArrayOfSize() is only set up for single dimensions.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
v***@gmail.com
2014-09-29 21:33:47 UTC
Permalink
Hi Sven,

Thanks for the quick reply. Then

arr[x][y] = n;

in Java would become:

arr.get(x).set(y, n)

in xtend. This may be slightly confusing (especially for others, who did
not write the code).
Furthermore

arr[x][y]++;

in Java would become something like:

arr.get(x).set(y, arr.get(x).get(y) + 1)

This is much worse to read. What is the solution for this?
I imagine it would be possible to write some static setter or increaser
functions (e.g. with @Inline), but it still does not feel right.

Regards,
Attila
Post by Sven Efftinge
Hi Attila,
val int[][] arr = #[#[1,2], #[3,4]]
assertEquals(3, arr.get(1).get(0))
Sven
Hi,
Thanks, for the beginners' sake, could you please also give the syntax how
to get and set arr[x][y]?
Cheers,
Attila
Post by Sven Efftinge
It's not supported out-of-the box. You can add support by declaring a
public class ArrayLiterals2 {
@Inline("new int[$1][$2]")
public static int[][] intArray(int outerSize, int innerSize) {
throw new UnsupportedOperationException();
}
}
import static extension my.lib.ArrayLiterals2.*


val int[][] arr = intArray(10,20)
@Inline tells the compiler to replace any calls to the annotated method
with the to-be-inlined Java expression (the 'value' template).
likely be removed once Xtend has support for more powerful method-macros.
e.g.
Type[][] matrixData = new Type[Dim1][Dim2];?
NewArrayOfSize() is only set up for single dimensions.
--
You received this message because you are subscribed to the Google Groups
"Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups
"Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stefan Oehme
2014-09-30 07:05:32 UTC
Permalink
You are right, this is inconvenient and we are looking for ways to improve
this. The workaround depends on what you are trying to do. If it's just a
few places, you could write some extension methods like array.increment(i,
j). If you have a single class that is very array-heavy, you could just
write that one class in Java.
Post by v***@gmail.com
Hi Sven,
Thanks for the quick reply. Then
arr[x][y] = n;
arr.get(x).set(y, n)
in xtend. This may be slightly confusing (especially for others, who did
not write the code).
Furthermore
arr[x][y]++;
arr.get(x).set(y, arr.get(x).get(y) + 1)
This is much worse to read. What is the solution for this?
I imagine it would be possible to write some static setter or increaser
Regards,
Attila
Post by Sven Efftinge
Hi Attila,
val int[][] arr = #[#[1,2], #[3,4]]
assertEquals(3, arr.get(1).get(0))
Sven
Hi,
Thanks, for the beginners' sake, could you please also give the syntax
how to get and set arr[x][y]?
Cheers,
Attila
Post by Sven Efftinge
It's not supported out-of-the box. You can add support by declaring a
public class ArrayLiterals2 {
@Inline("new int[$1][$2]")
public static int[][] intArray(int outerSize, int innerSize) {
throw new UnsupportedOperationException();
}
}
import static extension my.lib.ArrayLiterals2.*


val int[][] arr = intArray(10,20)
@Inline tells the compiler to replace any calls to the annotated method
with the to-be-inlined Java expression (the 'value' template).
likely be removed once Xtend has support for more powerful method-macros.
e.g.
Type[][] matrixData = new Type[Dim1][Dim2];?
NewArrayOfSize() is only set up for single dimensions.
--
You received this message because you are subscribed to the Google
Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups
"Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
f***@public.gmane.org
2013-05-01 21:39:06 UTC
Permalink
Thanks very much Sven!
Post by f***@public.gmane.org
e.g.
Type[][] matrixData = new Type[Dim1][Dim2];?
NewArrayOfSize() is only set up for single dimensions.
--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Continue reading on narkive:
Loading...