Skip to content

Dart's odd problems #3637

Closed
Closed
@seha-bot

Description

@seha-bot

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

#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions