Differentiate classes and subclasses

The more I look at this function the more I’m close to a dart Wat moment…

1 Like

Yes, I believe it would be possible to define a library API to let you ask these questions in a way that it didn’t significantly impact performance or code size.

I think it’s mostly that not enough users have needed this functionality. Defining a new core library API is a lot of work and it doesn’t happen unless there is significant demand.

2 Likes

The case is not very common in Dart, unless I’m missing a specific usecase.

Also, this would make the list have a very weird type. Are you sure you’re thinking of how this would be done in a soundly-typed language?

@RandalSchwartz This might not be very common for most Flutter programmers but if you want to write very generic code it is not so exotic .

I can confirm, after running this on DartPad:

class A {}
class B {}

class Ax extends A{}

String describeTypes<T1, T2>(T1 t1, T2 t2) {
  if (t1 is T2) {
    return T1 == T2 ? 'same type' : '$T1 is a subtype of $T2';
  }
  return t2 is T1 ? '$T1 is a supertype of $T2' : 'types are unrelated';
}

void main() {
  final record = (A(), B(), Ax());
  final list = [A(), B(), Ax()];
  final ax = Ax();

  // record
  print(describeTypes(ax, record.$1)); // Ax is a subtype of A
  print(describeTypes(ax, record.$2)); // types are unrelated
  print(describeTypes(ax, record.$3)); // same type

  // list
  print(describeTypes(ax, list[0])); // Ax is a subtype of Object
  print(describeTypes(ax, list[1])); // Ax is a subtype of Object
  print(describeTypes(ax, list[2])); // Ax is a subtype of Object
}

It looks like the checks are based on static type information, so it can’t parse the list effectively.

2 Likes

This is basic of OOP. Dart in the context of flutter is mostly about reading states computed by server side.

When used like any languages to represent real world process any FP or OOP will provide a way to compare Inheritance or Trait.

In my case I will end with an enum with nearly a hundred of manually managed declarations because I can not ask if a class is an extension of a parent.

I’m also hitting case with switch where it seems to loss the relation and end-up with sub-cases that does not belong to parent anymore but are not linted.

In large representations you rely fully on the language modeling to support the growth and maintenance or else you get lost really quickly.

1 Like

Agreed, if you want to express real world relationships via inheritance that’s pretty difficult and other languages have type systems that are way more powerful for this. I only wonder can’t you more leverage polymorphism if you use inheritance like this?

I come mainly from the Scala / Kotlin school for this kind of stuff.
Dart is taking a lot of hints from them and we’re in a very good path.

But the work those teams accomplish to get the whole package together,
few people get what it takes, it’s amazing!

Right now I’m in a very complexe case. The hierarchy is deep and wide and I should be able to switch composed classes at runtime. Thus my request to identify the parent to switch with last provided child or any kind of helpers.

I’m leveraging already a lot but I also generate a lot of stranges case where I broke the analyzer or need to declare Mixin that copy generics of the classes they apply on and then it become difficult to work with generic and instance types as we miss tools to match them ¯_(ツ)_/¯

I also face my own limitations of what can really be done with dart and best practices for advanced modelisation as most devs are not concerned with that.

The moment we move from phone size screen to desktop app this will be requirement but for now Java and C# take the cake in industry apps and surely it’s not a priority for google.

But this is the best place to get more stuff rolling.

2 Likes

I was extremely impressed by the type system of f#.
If I had to choose a language for a backend po with complex business models I would go for f#

F# and Scala are way to go if you can assemble a senior team and have some rules to not play the one liner game :slight_smile:

Kotlin is more permissive and the ramp-up is easier but much more importantly the tooling win over anything else. It’s another planet talking about productivity.

A lot of Dart success and Flutter by extension rely on the Dev Experience.

3 Likes

Opened a ticket

2 Likes