String.FromArray vs String.ToArray

Just curious, is there any reason that ToArray can be used like this:

dim myString as string = "One,Two,Three"
dim split() as string = myString.ToArray(",")

But the reverse can’t be done with FromArray?

dim split() as string = Array("One", "Two", "Three")
dim joined as string = split.FromArray(",")

Instead your have to do:

dim split() as string = Array("One","Two","Three")
dim joined as string = String.FromArray(split, ",")

It seems strange that ToArray is a method on a string, but FromArray is not a method on a string array.

Maybe I’m missing something obvious.

2 Likes

“split.FromArray(”,“)” is effectively saying Array.FromArray(), which has no way of indicating what the output type would be.

String.FromArray() tells you exactly what you get back. After all you could have other type of output, for example Dictionary.FromArray() could be produced ( I know it doesn’t exist ).

Its Xojo :rofl:

i want this and in case its a object it have to use a ToString Method Interface

dim data as Array = ("One", "Two", "Three", yourobject)
dim joined as string = data.Join(",") or data.ToString(",") or data.FromArray(",")
dim data2 As Array = joined.Split(",") or  joined.ToArray(",")

you can not have all

FromArray is an extension to the string type, not the array type. What you are describing would better be named Array.ToString.

2 Likes

FromArray only ever returns a string and is a method that only takes string arrays.
That’s why it confuses me. I suppose it could return a variant otherwise, but in reality the concept of joining an array really only applies to strings.


Public Function Join(extends stringArray() As String, delimiter As String = ",") As String
  Return String.FromArray(stringArray, delimiter)
End Function

Basically a missing feature.

4 Likes

I know that’s what exists today, but others could exist in the future. Even your own classes. Having Classname.FromArray() tests you about the return type.

I stumbled much too often about the change, that „join“ no longer exists in API 2. Its available in many other languages.

2 Likes

Yep, I suppose that was my point without actually saying it.

I think that it should be included in API2 for consistency since it’s definitely possible.

1 Like

The compiler tests you about a return type with an extends method.

I think that this should be included for consistency with ToArray

Yes, but for code readability point of view, Array.FromArray doesn’t tell you anything. Where as String.FromArray tells you exactly what is happening you are generating a string from an array. It is part of the String class. Array.Join is all well and good but it really only applies to a String array, what would should it do if it was an Integer array, for example.

It should throw a compiler error if it was an integer array.

That’s just a non-issue, naturally solved.

Var str1() As String // This type contains the method Join(), should autocomplete
Var int1() As Integer // This type does not contain the method Join(), should not autocomplete

x = str1.Join() // ok
y = int1.Join() // Compiler error Integer array int1.Join() method does not exist

1 Like

I could easily be wrong, but I think the issue with this is that the Xojo compiler has an Array type that represents all arrays, no matter what is stored inside them. The Array implementation probably has a internal property that identifies the type of the contents but that has nothing to do with the Array object’s class as far as the type system is concerned, and thus it doesn’t support the kind of content type-specific functionality you are describing.

That said - I sorely wish it did. I’ve had plenty of situations over the years where I wish that arrays could be subclassed based on their content type.

1 Like

That would be a design flaw, so I guess you are wrong.

Var str1() As String
str1.Add 123 // Should fire a compiler error because Xojo knows this is an array of strings


My example above where I do extend an Array of Strings does exactly that
str1.Join() // works, exists
int1.Join() // causes an invalid method compiler error

1 Like

A Join extension method on an array of strings absolutely works. When API2 was in beta, I proposed this exact thing. String.FromArray(someArray, "separator") was, for some unknown reason, selected over someArray.Join("separator"). Even calling it ToString would have been ok. But I was ignored.

While we’re on the subject, there is zero reason why the separator should be optional. Optional parameters are great when there is a logical default. Joining or splitting an array has no obvious default.

https://tracker.xojo.com/xojoinc/xojo/-/issues/75092

4 Likes

So strange that they wouldn’t want consistency in the language.

Thanks for your effort.

1 Like