Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Exception: Stack Overflow in Enum #2941

Closed
denisnadey opened this issue Mar 23, 2023 · 4 comments
Closed

Unhandled Exception: Stack Overflow in Enum #2941

denisnadey opened this issue Mar 23, 2023 · 4 comments
Labels
request Requests to resolve a particular developer problem

Comments

@denisnadey
Copy link

This is a Dart code that defines an enumeration type Planet. Each value in the enumeration corresponds to a planet and has an associated title. The mercury(), venus(), and earth() values do not have a title specified, so their name is used as the default title. The uranus(title: 'Uranus') value has a specified title of 'Uranus'.

The const Planet() constructor takes an optional title parameter and initializes the title field. The name getter returns the title if it's not empty, otherwise, it returns the default name of the planet.

The main() function prints the name of the last value in the Planet.values list using the name getter. In this case, the last value is neptune, so the output will be 'neptune'.

Overall, this code defines an enumeration of planets and demonstrates how to use it to retrieve the name of the last planet based on its associated title.

import 'dart:core';

enum Planet {
  mercury(),
  venus(),
  earth(),
  uranus(title: 'Uranus'),
  neptune();

  const Planet({
    this.title = "",
  });

  final String title;

  String get name => this.title.isEmpty ? this.name : this.title;
}

void main() {
  print(Planet.values.last.name);
}

logs:
➜ sandbb dart main.dart
Unhandled exception:
Stack Overflow
#0 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:3)
#1 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#2 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#3 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#4 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#5 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#6 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#7 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#8 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#9 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#10 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#11 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#12 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#13 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#14 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#15 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#16 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#17 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#18 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#19 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#20 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#21 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)
#22 Planet.name (file:///Users/denisnadey/Desktop/sandbb/main.dart:16:48)

@denisnadey denisnadey added the request Requests to resolve a particular developer problem label Mar 23, 2023
@eernstg
Copy link
Member

eernstg commented Mar 23, 2023

The invocation of the getter name in the declaration of name is an infinite loop. My guess is that you intended to invoke the extension getter name instead. You can do this by resolving the extension getter invocation explicitly: use EnumName(this).name rather than name.

enum Planet {
  mercury(),
  venus(),
  earth(),
  uranus(title: 'Uranus'),
  neptune();

  const Planet({
    this.title = "",
  });

  final String title;

  String get name => title.isEmpty ? EnumName(this).name : title;
}

void main() {
  print(Planet.values.last.name);
}

@eernstg
Copy link
Member

eernstg commented Mar 23, 2023

I'll close the issue because it is working as intended.

@eernstg eernstg closed this as completed Mar 23, 2023
@denisnadey
Copy link
Author

wow, omg. I am sorry for this issue

@eernstg
Copy link
Member

eernstg commented Mar 23, 2023

Oh, no problem, do keep asking/commenting when you see something surprising!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants