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

Dart's odd problems #1

Closed
seha-bot opened this issue Mar 3, 2024 · 2 comments
Closed

Dart's odd problems #1

seha-bot opened this issue Mar 3, 2024 · 2 comments

Comments

@seha-bot
Copy link

seha-bot commented Mar 3, 2024

First off I would like to apologise because this is not an issue with invariant_collection. I am writing here only because I don't know how to contact you ':-)

Your attention to writing safe and readable code and your contribution to Dart made me reach out to you for assistance.
I am collecting examples of "odd" Dart code for my presentation.

What do I mean by "odd"?

Odd code is code which is either undefined or unclear. Undefined is self-explanatory, but I doubt there is anything undefined in the Dart standard (you tell me :D). Unclear is code which can confuse the reader and induce him to thinking it does thing X, when in reality it does thing Y.

Motivation

I am a member of a discord server focusing on embedded engineering and it features an "odd problems emporium" channel. People post odd code and without using a compiler you need to explain what that code does.

Example:

/* C99 */
int main(void) {
    int i = 1;
    return i / i++;
}

What does this program return? Solution is that this is UB as there is no sequence point.

What have I collected so far

No real type safety

dart-lang/sdk#55021

Default parameters are allowed to be overriden

class Foo {
  int bar([int? baz = 0]) {
    return baz!;
  }
}

final class Bar extends Foo {
  @override
  int bar([int? baz]) {
    return baz!;
  }
}

void main() {
  Foo foo1 = Foo();
  Foo foo2 = Bar();
  print(foo1.bar());
  print(foo2.bar());
}

the existence of covariant

Return values of methods are allowed to be specialized, while for parameters you need covariant.
I will present this as "odd" because most people will be confused after I show them these two examples in succession:

Slide 1:

abstract class Foo {
  Foo bar();
}

final class Bar extends Foo {
  @override
  Bar bar() {
    return this;
  }
}

Slide 2:

abstract class Foo {
  void baz(Foo foo);
}

final class Bar extends Foo {
  @override
  void baz(Bar bar) {}
}

void is of type Null and has a size

void main() {
  List<void> foos = <void>[];
  foos.add(print("0"));
  foos.add(null);
  for (final e in foos.cast<Null>()) {
    print(e);
  }
}

Pattern matching is falsely non-exhaustive

dart-lang/language#3633

Conditional member access operator discards the assignment operations

final class Foo {
  int i = 0;
}

void main() {
  Foo? foo;
  int i = 0;
  foo?.i = ++i;
  print(i);
}

Note

I am not trying to get anyone to dislike Dart and all of these "issues" have a valid reasoning behind their existence.
Thank you for reading this and I hope you can provide some more examples of oddity in Dart. Also, would you like to say something so I can put it as a quote in the presentation?

Cheers

@eernstg
Copy link
Owner

eernstg commented Mar 4, 2024

Hello @seha-bot, I tried to move the issue to the Dart language repository where it seems to be a much better fit, but github doesn't want to do that. Please just copy your text and create the issue there: https://github.com/dart-lang/language/issues/new?assignees=&labels=request&projects=&template=1-PROBLEM_STATEMENT.md

@seha-bot
Copy link
Author

seha-bot commented Mar 4, 2024

I've copied it over to there:
dart-lang/language#3637

@seha-bot seha-bot closed this as completed Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants