What is the best declaration type for a string like parameter?
sigod via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jan 28 07:03:38 PST 2016
On Thursday, 28 January 2016 at 13:36:46 UTC, Puming wrote:
> I have a function that reads a line of string and do some
> computation.
>
> I searched the forum and found that people use `const(char)[]`
> or `in char[]` to accept both string and char[] arguments.
>
>
> What's the difference between `const(char)[]` and `in char[]`?
>
> If they are not the same, then which is better? If they are,
> then why both forms exists?
`in char[]` is short for `scope const char[]` or `scope
const(char[])`.
See http://dlang.org/spec/function.html#parameters
It depends on the situation. If possible I would use `in`
modifier. If not then just `const`.
> I found it a bit confusing and not quite readable, so I made an
> alias:
>
> alias str = const(char)[]
>
> and so far it works. But if `in char[]` is better, then I
> cannot alias it:
>
> alias str = in char[]
>
> this does not compile.
Please, don't define such aliases. I'm sure a lot of developers
will find it confusing. As I do.
`const(char)[]` or `in char[]` is perfectly understandable as
soon as you know what it means.
Also, read this: http://dlang.org/spec/const3.html
More information about the Digitalmars-d-learn
mailing list