tezvyn:

What are Dart extension methods? Implement toIntOrDefault on String.

AI-drafted, machine-checkedSource: dart.devadvanced

Tests Dart extension syntax and static resolution. A strong answer defines an extension on String, uses int.tryParse with a fallback, and notes extensions do not mutate the original type. Red flag: using try-catch over tryParse or claiming dynamic dispatch.

WHAT THIS TESTS: This question probes three distinct areas of Dart fluency. First, do you know the syntax and semantics of extension methods, which are static sugar resolved at compile time rather than true class modifications. Second, do you understand Dart's null-safe parsing utilities, specifically int.tryParse versus the throwing int.parse. Third, can you articulate the static resolution rule, which means extensions are not available on dynamic variables and can conflict when multiple extensions define the same name on overlapping types.

A GOOD ANSWER COVERS: A good answer hits four things in order. First, define the extension with a clear name, for example extension ParseHelpers on String. Second, implement toIntOrDefault by accepting an int defaultValue parameter and returning int.tryParse(this) ?? defaultValue, which avoids exceptions entirely. Third, note that extension methods are resolved statically based on the declared type, so they do not work on dynamic variables and they do not mutate the underlying class. Fourth, mention API conflict resolution, such as using an explicit extension prefix like ParseHelpers(myString).toIntOrDefault() if another extension collides.

COMMON WRONG ANSWERS: The biggest red flag is writing a try-catch block around int.parse instead of using int.tryParse, which is both slower and less idiomatic. Another red flag is omitting the default parameter and hard-coding zero, which reduces reusability. Some candidates incorrectly state that extensions modify the original class at runtime or that they work through inheritance, neither of which is true. Finally, forgetting null safety by returning a nullable int or ignoring the possibility of a null string shows shallow familiarity with modern Dart.

LIKELY FOLLOW-UPS: An interviewer might ask how extension resolution works with dynamic types, expecting you to say that dynamic loses all extension methods. They might ask how to handle name conflicts between two extensions on the same type, where the answer is explicit import hiding or prefix syntax. They could also ask about unnamed extensions, generic extensions, or whether you can add static methods through extensions, which you cannot.

ONE CONCRETE EXAMPLE: You could write the following code in an interview. Define extension StringParsing on String { int toIntOrDefault(int defaultValue) => int.tryParse(this) ?? defaultValue; }. Then show usage with final value = '42'.toIntOrDefault(0); which yields 42, and final fallback = 'abc'.toIntOrDefault(-1); which yields -1. If the variable were declared as dynamic text = '42'; then text.toIntOrDefault(0) would fail to compile because dynamic bypasses static extension resolution.

Read the original → dart.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.