LoginLogin
Might make SBS readonly: thread

A string literal passed into a function's parameter is not treated like a string type variable

Root / SmileBASIC Bug Reports / [.]

MZ952Created:
DEF F(A$)
 A$[0] = "A"
 RETURN A$
END
DIM TEST$ = "Z"
?F(TEST$)
> A
?F("Z")
> Type mismatch in 0:2
It seems that functions passed string literals, despite appearing to then pass that string literal into a variable, can't manipulate it appropriately. Edit: Don't know if this discussion already exists. If it does, I don't recall it.

I would guess that this has something to do with a string's reference type nature.

I've known about this but I don't think I ever mentioned it outside of chat. I guess string literals are read-only, though it's weird... If you do A$=B$, A$ and B$ will be the same string. But TEST$="Z" must actually make a copy, otherwise your second example would throw the same error.

I've known about it too for some time. I circumvented it by
DEF F(_A$)
 DIM A$ = _A$
 'the code uses A$
END
which seems to do the trick without any repercussions later. Not 100% sure, but I'm pretty sure that A$ does not reference _A$, or else it would be referencing the read-only type and erroring all the same. Edit: actually, this would make sense. If you consider _A$ as a placeholder for "Z", then DIM A$ = _A$ is the same as DIM A$ = "Z", and thus it doesn't reference.

Have you tried this? (I don't have my 3DS to test it right now)
DEF F(A$)
 A$ = A$
 'the code uses A$
END

Anything set inside a function that doesn't output stuff revert to the previous value when the function is ended unless it's an array value. I thought this was common knowledge for SB coders...

Have you tried this? (I don't have my 3DS to test it right now)
DEF F(A$)
 A$ = A$
 'the code uses A$
END
Type mismatch. No effect

Have you tried this? (I don't have my 3DS to test it right now)
DEF F(A$)
 A$ = A$
 'the code uses A$
END
Type mismatch. No effect
Huh... I guess A$ = A$ + "" might work, but your version is better than that.

Have you tried this? (I don't have my 3DS to test it right now)
DEF F(A$)
 A$ = A$
 'the code uses A$
END
Type mismatch. No effect
Huh... I guess A$ = A$ + "" might work, but your version is better than that.
Yeah, that one works

Anything set inside a function that doesn't output stuff revert to the previous value when the function is ended unless it's an array value. I thought this was common knowledge for SB coders...
I don't follow. Edit: Post noticed

Anything set inside a function that doesn't output stuff revert to the previous value when the function is ended unless it's an array value. I thought this was common knowledge for SB coders...
I don't follow.
If the function doesn't end with (), then a value set will always be reverted unless if the value is inside an array

If the function doesn't end with (), then a value set will always be reverted unless if the value is inside an array
The function does end with () though. Edit: The dilemma isn't one about reverted values, it's about how SB handles string literals as a function parameter.

If the function doesn't end with (), then a value set will always be reverted unless if the value is inside an array
The function does end with () though. Edit: The dilemma isn't one about reverted values, it's about how SB handles string literals as a function parameter.
Okay.
in mindWhat the heck is a string literal or a function parameter

What the heck is a string literal or a function parameter
DEF F(FunctionParameter)
 RETURN "AStringLiteral"
END

What the heck is a string literal or a function parameter
DEF F(FunctionParameter)
 RETURN "AStringLiteral"
END
HOW DARE YOU READ MY MIND!

Here's another annoying thing
DEF F(A$[])
 PUSH A$, "A"
 RETURN A$
END

DIM TEST$[0]
COPY TEST$, F(TEST$)
?LEN(TEST$)
> 1
?LEN(TEST$[0])
> 0
?TEST$[0]
> {empty string}
Now, look at this
'same DEF function

DIM TEST$[0]
?LEN(F(TEST$))
> 1
?LEN(F(TEST$)[0])
> 1
?F(TEST$)[0]
> A
Functions don't like returning one of their input variables. Like the other issue of this post's namesake, this is solved by
DEF F(_A$[])
 DIM A$[0]
 COPY A$, _A$
 'the code uses A$
END
More specifically, it doesn't like returning one of its input variables into the original input variable, but it does return the correct array if you read from the function directly.