build-a-programming-language / lesson-27.md
Lesson 27 · Evaluation

String concatenation

Strings become useful when you can join them. Today you evaluate + on two strings, producing a new string object with their contents concatenated.

The goal

Evaluate the + operator on two string operands into a concatenated string object.

Start here - the target
TO DO
Scenario: Concatenating strings with plus
Giventhe source "foo" + "bar"
Whenthe evaluator evaluates the expression
Thenthe result is a string object whose value is foobar
Andevaluating "" + "x" yields the string x
Background

You parsed string literals back in the parsing chapter, but the evaluator has not had a String object until now. Define it - a wrapper over the text with the usual Type/Inspect - and teach the infix evaluator one new case: when both operands are strings and the operator is +, produce a new string with their contents joined.

Only + is defined on strings; -, *, and comparisons will be type errors handled in the next lesson but one. Keep the boundary in mind - "" + "x" is "x", an empty string concatenates cleanly - and resist adding more string operations today. Concatenation alone makes strings first-class enough to build messages and, later, to see printed output from your programs.

Make it work
type String struct { Value string }
func (s *String) Type() string { return "STRING" }
// in the infix evaluator, when both operands are strings and op is "+":
// return &String{ left.Value + right.Value }
// other operators on strings will be a type error later
CheckpointDONE
The + operator concatenates two strings into a new string object. Commit.