String to Text Implicit Conversion???

I thought you could do implicit conversion between String and Text. But it doesn’t always work.

I created a new function:

Function MyFunction(var1 as text, var2 as text, var3 as text) as Boolean

If I then do the following:


Dim A as string = "Some String"
Dim B as String = "Some other string"
Dim C  as String = "A third string"

If MyFunction(A,B,C) Then
// Some code
End If

I get a compile error that states “Expected (Text, Text, Text), but these arguments are (String, String, String).”

If String implicitly coverts to Text, why is this an error? Do I have to create Text variables first? That’s going to make implementing text based functions a lot more difficult.

Or is this a bug?

Jon

There is no implicit conversion between String and Text.

Well, that’s news to me. I thought I just read something where there was…

Frankly, there should be. It would make converting to using Text a LOT easier…

Did you try using the explicit conversion (String.ToText)?

If MyFunction(A.ToText, B.ToText, C.ToText) Then End If

[quote=153465:@Paul Lefebvre]Did you try using the explicit conversion (String.ToText)?

If MyFunction(A.ToText, B.ToText, C.ToText) Then End If[/quote]

Yeah, I discovered that now. I guess that’s sufficient. :slight_smile:

What about simply using strings as parameters, since Text to string is implicit ?

Oh, so Text to String is implicit but String to Text is not. Well, that’s confusing! You would think implicit conversions would be reciprocal!

No, I wanted to do something completely in the new framework plus it could be something that I need to use for iOS or maybe not. But the String.ToText method works fine enough…

One of the reasons Text has been introduced is to get rid of the troubles with implicit conversion (same as with the Auto data type as replacement for Variants). So it makes sense that Text → String is converted implicitly, but String → Text needs an explicit conversion with ToText.

Exactly :).

I agree but it’s way confusing having to convert a TextField like

TextField1.Text.toText

I think in cases like this it should be implicit.

If we’ve been diligent with managing our encodings for strings and want to make the transition to the new framework an easier process, is there any way (with extends/assigns or some sort of global trickery) to allow implicit conversions from String to Text within an individual project?

Text to string is already implicit. You do not need anything special to set a String from a Text value.

ToText is necessary only to set a Text from a string value.

Sorry typo: I’ve edited the original to ask if there is any way to allow in a project implicit conversions from String to Text.

Look at Operator_Convert in the docs. Something like this (not tested since I am not on a computer right now):

Function Operator_Convert(Extends s As String) As Text Return s.ToText End

Thanks Eli, that’s the idea of what I’m looking for, but that doesn’t seem to work or change anything within the project.

[quote=193417:@Eli Ott]Look at Operator_Convert in the docs. Something like this (not tested since I am not on a computer right now):

Function Operator_Convert(Extends s As String) As Text Return s.ToText End[/quote]

Extension methods are not consulted when looking for the special operator methods.

Thanks Joe. Any other options for accomplishing this?

There’s no way to add an implicit conversion from String to Text in Xojo code.

I’m not sure I well understand what TEXT is / String.
But I have questions, will the Xojo event which actually use or return String will use Text in the future ?
For example :

  • event KeyDown(Key as String) as Boolean
    in a socket :
  • PageReceived(url as string, httpStatus as integer, headers as internetHeaders, content as string) Handles PageReceived
    I don’t have exemple where the return valu is String but I suppose there is.

Will have I to replace all my String variables in Text ? Is it better to do so (String deprecated in the future) ?

Thank you.