Skip to content

Commit 1e718d7

Browse files
claudeondrejmirtes
authored andcommitted
Document critical string vs char type distinction in Zephir
Added comprehensive documentation clarifying the important differences between string and char types in Zephir: **Key Additions:** 1. **Type System Section Enhancements:** - Clarified that char uses single-quote syntax: 'A' - Emphasized that string indexing (str[i]) returns char, not string - Added that string must use double-quote syntax: "hello" - Noted that single quotes are exclusively for char type 2. **New Dedicated Section: "String vs Char: Key Distinctions":** - Syntax difference table (single vs double quotes) - Detailed explanation of array access returning char - Practical examples showing string iteration yields char types - Type conversion examples (char to string and vice versa) - Common pitfall example showing incorrect char/string comparison 3. **Coding Guidelines Update:** - Added reminder that string indexing returns char type - Emphasized quote usage: single for char, double for string 4. **Common Pitfalls Section:** - Added "String vs Char Confusion" as item #3 - Listed specific gotchas: * str[0] is char type, not string * Quote syntax requirements * Loop variable type when iterating strings * Correct comparison syntax This addresses a critical type distinction that can cause confusion for developers new to Zephir, especially those coming from PHP where this distinction doesn't exist.
1 parent dfbd7a3 commit 1e718d7

File tree

1 file changed

+83
-7
lines changed

1 file changed

+83
-7
lines changed

turbo-ext/CLAUDE.md

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,11 @@ Static typing prevents type changes after declaration, enabling compiler optimiz
289289
- String assignment throws compiler exceptions
290290

291291
**Char:**
292-
- Stores single characters using single-quote syntax (`'Z'`)
293-
- Extracted from strings via indexing
292+
- Stores single characters using **single-quote syntax** (`'Z'`)
293+
- **Important**: Accessing a string with array/index access returns a `char` type
294+
- Extracted from strings via indexing: `let ch = str[0];` (ch is char type)
295+
- Must use single quotes for char literals: `'A'`, `'z'`, `'\n'`
296+
- Can be converted to string by concatenation or assignment to string variable
294297

295298
**Integer/Unsigned Integer:**
296299
- Numeric values auto-cast (floats truncate)
@@ -303,9 +306,75 @@ Static typing prevents type changes after declaration, enabling compiler optimiz
303306
- Similar auto-casting rules to integer types
304307

305308
**String:**
306-
- Double-quote required
307-
- Characters auto-convert to strings
309+
- **Must use double-quote syntax** (`"hello"`)
310+
- **Important**: Accessing a string index with `str[i]` returns a `char` (not a string)
311+
- Characters auto-convert to strings when assigned to string variables
308312
- Null becomes empty string
313+
- Cannot use single quotes for strings (single quotes are for char type only)
314+
315+
### String vs Char: Key Distinctions
316+
317+
Understanding the difference between `string` and `char` types is critical in Zephir:
318+
319+
**Syntax Difference:**
320+
- **String**: Uses double quotes `"hello"`
321+
- **Char**: Uses single quotes `'h'`
322+
323+
**Array Access Returns Char:**
324+
When you access a string by index, Zephir returns a `char`, not a `string`:
325+
326+
```zephir
327+
string text = "hello";
328+
char firstChar;
329+
let firstChar = text[0]; // firstChar = 'h' (char type, not string)
330+
```
331+
332+
**Practical Example:**
333+
```zephir
334+
string str = "hello";
335+
char ch;
336+
337+
// Iterating over a string yields char types
338+
for ch in str {
339+
// ch is of type char ('h', 'e', 'l', 'l', 'o')
340+
// Use single-quote syntax for comparison
341+
if ch == 'e' {
342+
echo "Found 'e'";
343+
}
344+
}
345+
346+
// Array access also returns char
347+
let ch = str[0]; // ch = 'h' (char type)
348+
```
349+
350+
**Type Conversion:**
351+
```zephir
352+
char ch = 'A';
353+
string str;
354+
355+
// Char to String: Auto-converts when assigned to string
356+
let str = ch; // str = "A"
357+
358+
// String to Char: Use array access
359+
let str = "Hello";
360+
let ch = str[0]; // ch = 'H'
361+
```
362+
363+
**Common Pitfall:**
364+
```zephir
365+
string text = "test";
366+
367+
// WRONG: This won't work as expected
368+
// text[0] returns char 't', not string "t"
369+
if text[0] == "t" { // Comparing char to string - type mismatch!
370+
// This may not work as expected
371+
}
372+
373+
// CORRECT: Use single quotes for char comparison
374+
if text[0] == 't' { // Comparing char to char - correct!
375+
// This works correctly
376+
}
377+
```
309378

310379
---
311380

@@ -1697,6 +1766,8 @@ To identify new optimization targets:
16971766
- Specify return types on all methods
16981767
- Use type hints for parameters
16991768
- Be aware of automatic type conversions
1769+
- **Remember**: String indexing (`str[i]`) returns `char`, not `string`
1770+
- Use single quotes for `char` literals (`'a'`), double quotes for `string` literals (`"a"`)
17001771

17011772
**Memory Efficiency:**
17021773
- Prefer static types over dynamic for performance
@@ -1759,9 +1830,14 @@ vendor/bin/zephir generate && vendor/bin/zephir compile
17591830

17601831
1. **Undeclared Variables**: All variables must be declared with `var` or a type
17611832
2. **Type Mismatches**: Static types cannot be reassigned to different types
1762-
3. **Missing Semicolons**: While optional, they can help avoid ambiguity
1763-
4. **File/Class Mismatch**: File structure must match namespace and class names
1764-
5. **Immutable Variables**: Use `let` to assign values, not direct assignment
1833+
3. **String vs Char Confusion**:
1834+
- String indexing returns `char`, not `string`: `str[0]` is char type
1835+
- Use single quotes for char: `'a'`, double quotes for string: `"a"`
1836+
- When iterating strings, loop variable is `char` type
1837+
- Comparing `str[0] == "a"` is wrong; use `str[0] == 'a'` instead
1838+
4. **Missing Semicolons**: While optional, they can help avoid ambiguity
1839+
5. **File/Class Mismatch**: File structure must match namespace and class names
1840+
6. **Immutable Variables**: Use `let` to assign values, not direct assignment
17651841

17661842
## Resources
17671843

0 commit comments

Comments
 (0)