0

I started learning Dart and was reading a critique of some of it's design choices here: https://medium.com/@krossovochkin/dart-language-bad-design-choices-6e35987dc693

The last point that is made is about the poor type system and the author cited this code snippet which prints null:

void main() {
  String s = null;
  if (s is String) {
    print("string");
  } else if (s is Null) {
    print("null");
  } else {
    print ("none");
  }
}

The is keyword was new to me but "The Dart Programming Language" by Gilad pointed out that is checks the interface implemented by an object's class and not the actual class of an object.

However this didn't help me much because I would think that the variable s is an instance of String and therefore implements String, but the evidence is to the contrary.

I get that the class is not required when defining objects/variables in Dart, and thus I started to wonder if putting the class in the definition just serves as sugar and has little functional purpose. But instead the class of an object/variable is completely determined by its value, and since the default value for all variables in Dart is null, then it would make sense that String is not implemented, but Null is. Is this the case? Am I way of base? Maybe someone could help me wrap my head around this.

2 Answers 2

1

The reason is that is checks the interface of the current object itself and not the reference to this object. So yes, s can point to a String object but also allowed to point to null which are a instance of Null: https://api.dart.dev/stable/2.7.2/dart-core/Null-class.html

Since Null does not implement the String interface, this will return false (null is String). This is also mentioned in the article.

The problem the article are trying to focus on are more the fact you are allowed to set the String variable to null value but Null does not implement String.

Well, in the future, this problem are going to be fixed with non-nullable types which are in development right now. When this is implemented you can actually define variables where you can be sure the value will never be null.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah I see, that clears it up for me. My theory of it checking the interface of the actual value was not wholly incorrect. Thanks.
0

So I continued my Dart reading and I came to a better understanding, and that is that Dart is truly optionally typed and that means 2 things:

  1. Type are syntactically optional.
  2. Type has no impact on runtime semantics.

Therefore the actual type annotation of a variable in Dart only serves documentation purposes and it cannot be assumed that a type annotation is true. The actual type of a variable is wholly determined by the value stored at this variable, and in this case it is null.

In truth the variable that I defined in my example is not a String variable or an implementer of the String interface. It is just annotated that it may be/should be/most likely is a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.