0

Why directory comparison does not work the expected way in Dart?

import 'dart:io';

void main() {
  Directory d = Directory('/kek');
  Directory e = Directory('/kek');
  print(d==e);  // false
  print(d.hashCode);  // 123456
  print(e.hashCode);  // 654321
}
aleskva
  • 1,644
  • 2
  • 21
  • 40

1 Answers1

2

As I read the documentation for the Directory object, the hashCode and operator== methods are simply inherited from Object, and thus have no special implementation that would cause two different Directory objects to compare equal if they point to the same place.

This would be difficult to implement. Should the hashCode canonicalize relative paths and paths containing "." and ".."? Should it follow symlinks? What about files with multiple hard links?

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • So the only way to compare is using a relative/absolute path, but developer must be sure he uses only one type? – aleskva Jul 09 '19 at 17:31
  • I'm used to this working correctly in Python, so I was just surprised there is no implementation in Dart. – aleskva Jul 09 '19 at 17:34
  • I'm saying that if you need the concept of "same directory", maybe you should re-think your design. – Lee Daniel Crocker Jul 09 '19 at 17:34
  • You might be right. I'm trying to rewrite List of Directories into a nested Maps and then translate it into nested Flutter ListViews. But I think there is no Map.addRecursively and Map.forEachRecursively in Dart as it is in Python so I will need to find a different approach. – aleskva Jul 09 '19 at 17:42