#Ziglings Exercise 6 String Concatenation

1 messages · Page 1 of 1 (latest)

sleek turtle
#

How come

    const major = "Major";
    const tom = "Tom";
    const major_tom = major ++ " " ++ tom;

works but

    const major = "Major";
    const tom = "Tom";
    const major_tom = major ++ ' ' ++ tom;

doesn't work? A space is a single character so I thought to use a character type instead of a double quotes

Even this

    const major_tom = major ++ [_]u8{' '} ++ tom;

doesn't work. Why?

Why don't the last 2 solutions work? I (think I) understand why the first solution works and passes.

topaz steppe
#

you can concatenate arrays or pointers to arrays, but you can't mix the two

#

as for the middle example, it doesn't work because array concatenation doesn't work on scalars

#

you need both sides to be an array or pointer to array (or comptime known slice)

sleek turtle
#

i havent gotten to the part of pointers or scalars. i know the concept of pointers from C. I thought strings were just an array of characters which are numbers ig (u8). and turns out i was reading something wrong because [_]u8{' '} does work. The only one that doesn't work is major ++ ' ' ++ tom which I understand why it doesn't work because its not in an array format i guess. I checked the exercise specifically.

topaz steppe
#

which implicitly coerces to a slice

#

but it's still a pointer either way

#

by "scalar" I just meant a lone u8

#

instead of a u8 that is inside of an array

sleek turtle
#

Pretty sure I understand it now: "foo" ++ ' ' ++ "bar" won't work because ' ' isn't an array or string, its just a number and the ++ (concatenation) operator only works on arrays (incl. strings bc strings are just arrays of characters, which characters are just numbers)