Re: [misc] Is Dynamic type or not? Allows Dart to have two types at the same time?

33 views
Skip to first unread message
Message has been deleted
Message has been deleted

Ladislav Thon

unread,
Apr 9, 2012, 8:52:57 AM4/9/12
to andrew mezoni, General Dart Discussion
Is Dynamic type or not? Allows Dart to have two types at the same
time?

Everytime there is inheritance (well, subtyping, but I won't dwell into typesystems), it looks like an object can have more types. Imagine this:

class A {}
class B extends A {}
class C extends B {}

main() {
  var c = new C();
  if (c is C) print("C");
  if (c is B) print("B");
  if (c is A) print("A");
}

What do you think the output is?

The only thing that is special to Dynamic in this situation is the fact that Dynamic is a supertype (an ancestor) of all types. That's pretty much all there is.

LT
Message has been deleted

Ladislav Thon

unread,
Apr 9, 2012, 9:57:59 AM4/9/12
to andrew mezoni, General Dart Discussion
If type annotated but variable not initialized the variable has
unknown type?

If you declare a variable but do not initialize it, it is initialized to null automatically. This expression: null is String is obviously false.

LT

Srdjan Mitrovic

unread,
Apr 9, 2012, 12:42:11 PM4/9/12
to Ladislav Thon, andrew mezoni, General Dart Discussion
And from the spec:

Also note that null is T is false unless T = ObjectT = Dynamic or T = Null. Since the class Null is not exported by the core library, the latter will not occur in user code. The former is useless, as is anything of the form e is Object.  Users should test for a null value directly rather than via a type test on Object. 

Bob Nystrom

unread,
Apr 9, 2012, 1:48:26 PM4/9/12
to Ladislav Thon, andrew mezoni, General Dart Discussion
On Mon, Apr 9, 2012 at 6:57 AM, Ladislav Thon <lad...@gmail.com> wrote:
If type annotated but variable not initialized the variable has
unknown type?

This expression: null is String is obviously false.

Playing Devil's advocate here, why is this obviously false? null is a valid value for a variable whose type is String so it seems to me that in Dart, null is a String, right?

- bob

Gilad Bracha

unread,
Apr 9, 2012, 1:57:30 PM4/9/12
to Bob Nystrom, Ladislav Thon, andrew mezoni, General Dart Discussion
One could define things that way, but it serves very little purpose.  Typically, the goal of a test 

o is T

is to be allow one to use o as a T by invoking a method on it. If you make this answer true for null, you need to test for null separately, which is painful and error prone.  On the other hand, we do allow assignment, because of the general need for placeholder when a useful value is unknown.

 

- bob




--
Cheers, Gilad

Ladislav Thon

unread,
Apr 10, 2012, 3:08:06 AM4/10/12
to Bob Nystrom, andrew mezoni, General Dart Discussion
This expression: null is String is obviously false.

Playing Devil's advocate here, why is this obviously false? null is a valid value for a variable whose type is String so it seems to me that in Dart, null is a String, right?

Well, because it really doesn't behave like String. Can you call any of String's method on null? You can't (with the exception of toString, but you can call toString on anything).

You can assign null to String-typed variable, but that's a problem on its own, as you are well aware of (the billion dollar mistake and only recent "invention" of non-nullable types).

LT

Jay Young

unread,
Apr 10, 2012, 12:57:02 PM4/10/12
to mi...@dartlang.org, Ladislav Thon, andrew mezoni
My understanding (which I'd love to be corrected if I'm wrong) is that type annotations define the set of types that are assignable to a variable (some bucket in memory).  On the other hand, the "is" operator tests the value IN the bucket, not the bucket itself.  `String name;` means that the "name" bucket can hold a String OR null.  That doesn't mean that null is a string.  I think the "bucket type" vs "thing in bucket type" is the important distinction.

Is that right?

Gilad Bracha

unread,
Apr 10, 2012, 1:25:45 PM4/10/12
to Jay Young, mi...@dartlang.org, Ladislav Thon, andrew mezoni
On Tue, Apr 10, 2012 at 9:57 AM, Jay Young <jayyou...@gmail.com> wrote:
My understanding (which I'd love to be corrected if I'm wrong) is that type annotations define the set of types that are assignable to a variable (some bucket in memory).  On the other hand, the "is" operator tests the value IN the bucket, not the bucket itself.

I can't say I understood that, but what's important is: 
 

 
`String name;` means that the "name" bucket can hold a String OR null.  That doesn't mean that null is a string.

This is correct.
 
--
Cheers, Gilad

Bob Nystrom

unread,
Apr 10, 2012, 2:02:23 PM4/10/12
to Ladislav Thon, andrew mezoni, General Dart Discussion
On Tue, Apr 10, 2012 at 12:08 AM, Ladislav Thon <lad...@gmail.com> wrote:
This expression: null is String is obviously false.

Playing Devil's advocate here, why is this obviously false? null is a valid value for a variable whose type is String so it seems to me that in Dart, null is a String, right?

Well, because it really doesn't behave like String. Can you call any of String's method on null? You can't (with the exception of toString, but you can call toString on anything).

No, you can't. That means calling String methods on null will cause a type error. To me, that means I should get a static type warning if I try to assign null to a variable typed String.

But, I would love to have non-nullable types, so of course I feel that way. The fact that in practice almost all of my type errors are actually null reference errors just reinforces my opinion. :)
 

You can assign null to String-typed variable, but that's a problem on its own, as you are well aware of (the billion dollar mistake and only recent "invention" of non-nullable types).

I think non-nullable types are actually older than NULL, but I could be wrong.

- bob

Louis Wasserman

unread,
Apr 10, 2012, 2:10:25 PM4/10/12
to Bob Nystrom, Ladislav Thon, andrew mezoni, General Dart Discussion
The first quotation in http://en.wikipedia.org/wiki/Tony_Hoare#Quotations suggests that they were invented more or less simultaneously, I think?

Bob Nystrom

unread,
Apr 10, 2012, 2:56:05 PM4/10/12
to Louis Wasserman, Ladislav Thon, andrew mezoni, General Dart Discussion
On Tue, Apr 10, 2012 at 11:10 AM, Louis Wasserman <wasserm...@gmail.com> wrote:
The first quotation in http://en.wikipedia.org/wiki/Tony_Hoare#Quotations suggests that they were invented more or less simultaneously, I think?

I was thinking of in constrast to ML where variables are all implicitly non-nullable. But it looks like you're right, I think ML came around in the 70's which makes it newer than ALGOL W.

- bob
 
Reply all
Reply to author
Forward
0 new messages