Russell Whitaker wrote:
It would be nice if the compiler could take something like function foo: string; begin foo := 'OK'; end; and have it automatically set capacity = length of the return.
One planned feature are dynamic strings which allocate space for their size automatically. (Not only for function results then, also for variables.) Of course, internally they will have to use pointers and allocate memory on the heap, so they may be a little slower (but perhaps faster in other cases, when pointer copies can be used instead of string copies, but this will have to be checked carefully).
Other than that, I don't see a way to realize this since function results internally must have a constant size (to be returned on the stack or in registers).
While I'm wishing, how about: Astring = string() value "<some string>"; It compiles,
... without the parentheses and with `:' instead of `=' ...
but assumes capacity = 255 rather than the length of <some string>.
`String' used as a type means `String(255)' for BP compatibility (with a warning, normally).
What you suggest is a way to determine the capacity from the initial value. More generally, this could also be applied to arrays (where I'd find it useful sometimes). Implementing it might not be too hard, but I don't yet have a good idea for the syntax. Empty parentheses look strange to me. Besides, for arrays (not for strings) somehow the lower bound must be specified. Something like `...' perhaps:
var s: String (...) = 'Hi'; a: array [1 ...] of Integer = (42, 17, 99);
Hmm ...?
Frank