dotlin-org/dotlin

Throw error if inherting a Dart class with only factory constructors

eggnstone opened this issue ยท 3 comments

Kotlin:

class DartFormatException(message: String) : Exception(message)

Generated Dart:

import "package:meta/meta.dart";

@sealed
class DartFormatException extends Exception {
  DartFormatException(String message) : super(message);
}

Error:
error: The class 'DartFormatException' can't extend 'Exception' because 'Exception' only has factory constructors (no generative constructors), and 'DartFormatException' has at least one generative constructor. (no_generative_constructors_in_superclass at [untitled6] lib\dart_format_exception.dt.g.dart:4)

Yep, indeed that's wrong, thank you ๐Ÿ‘ In the future Dotlin will throw an error as well if a class cannot be inherited because of that. If you use Exception(Interface) it will work ๐Ÿ‘

If you use Exception(Interface) it will work

Could you give an example code?
I don't fully understand.

It's the special Dotlin syntax for inheriting any Dart class as mentioned here. It allows you to choose how to implement a class, just like in Dart with extends, implements or with.

Unfortunately my advice was a bit too soon, you can't do this yet with the Exception class specifically ๐Ÿ˜… But in the future this would be the way to do go about it ๐Ÿ‘