dart-lang / dart-lang/language

Dart's difficulty to analyze code flows within a method that needs to return a specific object.

Open
#2,121 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Include a permanent link to the error if possible.

https://help.github.com/en/articles/getting-permanent-links-to-files
```
**In the attached code, Dart has enough information to correctly define which type of object (animal/Cat or animal/Dog) is being passed to the function, but even so the compiler believes it needs a 'return' outside the if/else structure if to avoid terminating without returning any value...**

main() {
Cachorro cachorro1 = Cachorro("Rex", 3);
cachorro1.comer();
cachorro1.dormir();
cachorro1.latir();

print(cachorro1.toString());

Gato gato1 = Gato("Tom", 5);
gato1.vidas--;
gato1.comer();
gato1.dormir();
gato1.miar();

print(gato1.toString());

List animais = [];

//animais.add(gato1);
animais.add(cachorro1);
animais.add(gato1);
animais.add(cachorro1);
animais.add(gato1);
animais.add(cachorro1);

print("-----------------------------");
print("-----------------------------");
Animal? animal1 = funcao(animais.first);
print("-----------------------------");
print("-----------------------------");
if (animal1 is Cachorro) {
animal1.latir();
print("O cachorro já latiu...");
} else if (animal1 is Gato) {
animal1.miar();
print("O gato já miou...");
}
}

// ????????????????????????????????????????????
// Returns Cat/Dog or null...
// Dart does not allow deleting from this method "?" and "return null;" without signaling warning.
// ????????????????????????????????????????????
Animal? funcao(Animal toto) {
print("Trace:...Iniciando a Função");
//Animal animal1 = toto;
if (toto is Cachorro) {
print("O cachorro vai latir...");
toto.latir();
print("Trace:...Recebeu um Cachorro -> Retorna o Cachorro!");
return Cachorro(toto.nome, toto.idade);
} else if (toto is Gato) {
print("O gato vai miar...");
toto.miar();
print("Trace:...Recebeu um Gato -> Retorna o Gato!");
return Gato(toto.nome, toto.idade);
}
return null;
}

abstract class Animal {
Animal(this.nome, this.idade);

String nome;
int idade;

void comer() {
print("Comeu");
}

void dormir() {
print("Dormiu");
}

@override
String toString() {
return "Animal -> Nome: $nome Idade: $idade";
}
}

class Cachorro extends Animal {
Cachorro(String nome, int idade) : super(nome, idade) {
print("Criou o cachorro $nome!");
}

void latir() {
print("Au au");
}

@override
void dormir() {
super.dormir();
print("Roncando muito!!!");
}

@override
String toString() {
return "Cachorro -> Nome: $nome Idade: $idade";
}
}

class Gato extends Animal {
Gato(String nome, int idade) : super(nome, idade) {
print("Criou o gato $nome!");
}

int vidas = 7;
void miar() {
print("Miauuu");
}

@override
String toString() {
return "Gato -> Nome: $nome Idade: $idade";
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the attached example and the funcao(Animal toto) entry point, especially its if/else type checks and final return. Run the example through the Dart analyzer to reproduce the warning, then compare the observed control-flow behavior with the language rules; done means the intended behavior and any required language change are clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.