# Add a custom cast?

**URL:** <https://discourse.processing.org/t/add-a-custom-cast/22995>\
**Category:** Development\
**Created:** [August 2, 2020, 3:46pm UTC](https://discourse.processing.org/t/add-a-custom-cast/22995 "2020-08-02T15:46:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [August 2, 2020, 3:46pm UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/1 "2020-08-02T15:46:49Z")

</div>

Hi I’m making a math library and it includes a matrix class.  
I wish to make it possible to cast it from a float[][]. Something like this:

```auto
float[][] a={{1,2},{3,4}};
Matrix m=(Matrix) a;

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 2, 2020, 5:15pm UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/2 "2020-08-02T17:15:57Z")

</div>

Can’t you just create a `static` method (or alt. an overloaded constuctor) which converts a `float[][]` to a Matrix, like `static final Matrix.fromFloatArray2d(final float[][] arr2d) {}`? 💡

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [August 2, 2020, 6:09pm UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/3 "2020-08-02T18:09:02Z")

</div>

I put that into my Matrix class and it says that there is no return type. Could you please explain how this method works.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 2, 2020, 7:35pm UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/4 "2020-08-02T19:35:10Z")

</div>

When developing a library and you want others to take a look you should make it available online on some repo: 🕶

> [@Library creation help](https://discourse.processing.org/t/library-creation-help/22923/6):
>
> 1st step you should take in order to make a lib is creating a repo for it: octopus You can post the original “.java” file(s) you’re attempting to convert to JS there. coffee Then create a new “.js” file w/ you attempt conversion, so others can chime in too. bellhop_bell

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [August 2, 2020, 7:52pm UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/5 "2020-08-02T19:52:33Z")

</div>

I’ll do so but I have to add a few comments. (remove actully I have lots of usless Code wich is just commented)

Edit 2: I’m done [The Library](https://github.com/NumericPrime/Simple-Math-Library-Work-in-Progress-/tree/master/Simple%20Math/src/simpleMath)

So how can I add a cast from a float[][] to my Matrix class?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 3, 2020, 10:33am UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/6 "2020-08-03T10:33:26Z")

</div>

It would be a function outside the class or inside the class like

```auto
Matrix getMatrix(float [][] grid) {
//do magic
return resultMatrix;

}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 3, 2020, 10:46am UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/7 "2020-08-03T10:46:44Z")

</div>

As has been said above

You can also make a constructor and use it in my function. Which is not a cast but converter

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [August 3, 2020, 5:59pm UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/8 "2020-08-03T17:59:27Z")

</div>

I’ve already added a overloaded constructor. But to create a Matrix object I have to do something like this:

```auto
Matrix m=new Matrix(arr2d);

```

but a cast would be much easier.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 3, 2020, 7:25pm UTC](https://discourse.processing.org/t/add-a-custom-cast/22995/9 "2020-08-03T19:25:53Z")

</div>

> [@NumericPrime](#):
>
> But a cast would be much easier.

Unfortunately Java only allows `(cast)` among compatible datatypes.  
That is, they have a parent/child datatype relationship.

> [@NumericPrime](#):
>
> I’ve already added an overloaded constructor.

Well, now you can also add an alternative `static` method for that same conversion functionality:

```auto
final float[][] f = { { 1, 2 }, { 3, 4 } };
Matrix m;

void setup() {
  m = new Matrix(f);
  println(m);

  m = Matrix.fromFloatArray2d(f);
  println(m);

  exit();
}

static // Comment this out when inside a ".java" file!!!
public class Matrix {
  public float[][] matrix;
  public int rows, cols;

  public Matrix(final float[][] m) {
    rows = m.length;
    cols = m[0].length;
    matrix = m.clone();
    for (int i = 0; i < rows; matrix[i] = m[i++].clone());
  }

  static public final Matrix fromFloatArray2d(final float[][] m) {
    return new Matrix(m);
  }

  @Override public String toString() {
    final StringBuilder s = new StringBuilder("rows x cols (");
    s.append(rows).append(" x ").append(cols).append("):\n");

    for (int i = 0; i < rows; ++i)
      s.append(java.util.Arrays.toString(matrix[i])).append('\n'); 

    return s.toString();
  }
}

```
