<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx</link><description>I thought I might spend a few days talking about the JScript and JScript .NET type systems, starting with some introductory material. There is a lot of terminology associated with type systems. What exactly is weak typing? What is a subtype? Just what</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53337</link><pubDate>Thu, 06 Nov 2003 12:40:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53337</guid><dc:creator>Dan Shappir</dc:creator><description>One thing that really bugs me about JavaScript, a.k.a. JScript Classic, is the myriad yet incompatible methods to determine object type. The main problem being the various ways to define what constitutes a unique JavaScript type. For example, one could argue that the definition you have presented is to weak in that the Object group is not sufficiently specific.

The obvious way to determine object type is the typeof operator. This operator returns a different set of values than the one you enumerated: &amp;quot;number,&amp;quot; &amp;quot;string,&amp;quot; &amp;quot;boolean,&amp;quot; &amp;quot;object,&amp;quot; &amp;quot;function,&amp;quot; and &amp;quot;undefined.&amp;quot; That is, null is identified as an object, yet JScript functions are magically identified as being something more than just any old object.

Also, a few may know that JScript's typeof can also return &amp;quot;unknown&amp;quot; in certain weird cases.

Add to this confusion that typeof(3) is &amp;quot;number&amp;quot; but typeof(new Number(3)) is &amp;quot;object&amp;quot;. I can understand why this is, but is can be confusing. For example, what would be the output from the following code:

Number.prototype.showType = function() { alert(typeof(this)); }
(3).showType();

Automatic boxing, I know.

Another way to determine types is using the constructor property. This allows you to determine if an object is an Array for example:

if ( x.constructor === Array ) ...

or you can use the instanceof operator.

You might also care about the subclass of a particular type, as determined by it's prototype. In this case you would use the isPrototypeOf method.

Finally, since JavaScript uses duck-typing, what might actually be relevant is not an object's exact type, but which properties it implements. In this case you would use the in operator, or maybe even propertyIsEnumerable.

The situation is even worse when you involve objects that are external to JScript. For example, DOM methods are not identified as functions and DOM collections may look a bit like arrays but aren't.</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53338</link><pubDate>Thu, 06 Nov 2003 17:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53338</guid><dc:creator>Rudi Schlatte</dc:creator><description>I've seen slightly different uses of the vocabulary that your article uses.  Type systems can usefully be classified along two axes, namely static / dynamic and strong / weak.

Static typing means variables have associated (compile-time) type information and can only be bound to values of some type.  By your examples, JScript classic is dynamically typed, since a variable can be bound to values of different types during its lifetime.  This would not be possible in a statically typed language, such as C.

Strong typing essentially means that there's a typeof operator - i.e., a value (not the variable, but the thing it is bouund to) holds information about its type.  C is weakly typed: a value is just a bit pattern in memory without any explicit type information, and there's nothing forbidding anyone from reinterpreting a `long' value as a pointer to a structure.  This would not be possible in a strongly typed language.</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53339</link><pubDate>Thu, 06 Nov 2003 17:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53339</guid><dc:creator>Michael Feathers</dc:creator><description>I'll second Rudi on this.  The definitions that he mentions are becoming more pervasive.  If Strong/Weak and Static/Dynamic aren't seen as orthogonal then you have the situation where Smalltalk and early C are considered to have the same kind of type system, and that's nearly a criminal misrepresentation.  It equates having well defined errors detected at runtime with silent memory corruption that crashes a system if you are lucky, or just makes things randomly and silently wrong over the life of a system, if you aren't.</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53340</link><pubDate>Thu, 06 Nov 2003 20:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53340</guid><dc:creator>Isaac</dc:creator><description>&amp;quot;implies that JScript supports no types at all&amp;quot;
Much of the confusion and ambiguity in the terms we use seems to stem from fear of negative connotations. 

It's unclear to me why we cannot work with the terms defined by Luca Cardelli (in Handbook of Computer Science and Engineering, Chapter 103. CRC Press, 1997; http://citeseer.nj.nec.com/cardelli97type.html
) .

   typed (untyped)
&amp;quot;A program variable can assume a range of values during the execution of a program. An upper bound of such a range is called a type of the variable. Languages that do not restrict the range of variables are called untyped languages.&amp;quot;

   safe (unsafe)
&amp;quot;A program fragment is safe if it does not cause untrapped errors to occur. Languages where all program fragments are safe are called safe languages.&amp;quot;

So, Smalltalk is untyped and safe (no untrapped errors), and C is typed and unsafe.

(Of course, he also defines explicit/implicit types, static checking/dynamic checking, strongly checked/weakly checked...)

</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53341</link><pubDate>Fri, 07 Nov 2003 04:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53341</guid><dc:creator>Just some guy</dc:creator><description>You describe a subtype in terms of a subset of the possible values of a super type. But in practice (in most all exaples I have seen , in textbooks and so forth) the opposite is true... This convention is also enforced by some language rules (which I will get to).

For example the class java.util.zip.JarFile extends java.util.zip.ZipFile, but it represents both a zip file AND a jar file, so clearly it's set of possible values is larger. But it is also a subtype of ZipFile.... how can this be? does it violate the rules of subtypeing? and if it did, why does the  compiler not reject it?

Because most all languges have subtypes inheret attributes of the supertpe,  it is easy for developers to make these kinds of mistakes.     

</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53342</link><pubDate>Fri, 07 Nov 2003 08:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53342</guid><dc:creator>Michael Feathers</dc:creator><description>&amp;gt;It's unclear to me why we cannot work with the terms defined by Luca &amp;gt;Cardelli (in Handbook of Computer Science and Engineering, Chapter 103. &amp;gt;CRC Press, 1997; http://citeseer.nj.nec.com/cardelli97type.html
&amp;gt;typed (untyped)
&amp;gt;&amp;quot;A program variable can assume a range of values during the execution of a &amp;gt;program. An upper bound of such a range is called a type of the variable. &amp;gt;Languages that do not restrict the range of variables are called untyped &amp;gt;languages.&amp;quot;

It sounds like he is talking about variables, and that's fine, but in some OO languages you don't have data variables, you have references to objects.   Calling languages like these untyped doesn't make much sense because the objects themselves obviously have types.  In other words, the languages features are at a level of abstraction where that definition isn't very relevant.

Another example.  In some languages you don't have to be particularly concerned if you attempt to increment an integer beyond its range.  The object adjusts its range dynamically.  Now which is the variable?  The object or the reference that holds it?  If you say it is the reference, then all references have the same type, but if you say it is the object, well, what is its type really?  And, do you ever assign an object to another?  In many OO languages you can't you can only assign references.  </description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53343</link><pubDate>Fri, 07 Nov 2003 08:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53343</guid><dc:creator>Michael Feathers</dc:creator><description>&amp;gt;It's unclear to me why we cannot work with the terms defined by Luca &amp;gt;Cardelli (in Handbook of Computer Science and Engineering, Chapter 103. &amp;gt;CRC Press, 1997; http://citeseer.nj.nec.com/cardelli97type.html
&amp;gt;typed (untyped)
&amp;gt;&amp;quot;A program variable can assume a range of values during the execution of a &amp;gt;program. An upper bound of such a range is called a type of the variable. &amp;gt;Languages that do not restrict the range of variables are called untyped &amp;gt;languages.&amp;quot;

It sounds like he is talking about variables, and that's fine, but in some OO languages you don't have data variables, you have references to objects.   Calling languages like these untyped doesn't make much sense because the objects themselves obviously have types.  In other words, the languages features are at a level of abstraction where that definition isn't very relevant.

Another example.  In some languages you don't have to be particularly concerned if you attempt to increment an integer beyond its range.  The object adjusts its range dynamically.  Now which is the variable?  The object or the reference that holds it?  If you say it is the reference, then all references have the same type, but if you say it is the object, well, what is its type really?  And, do you ever assign an object to another?  In many OO languages you can't you can only assign references.  </description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53344</link><pubDate>Fri, 07 Nov 2003 09:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53344</guid><dc:creator>Eric Lippert</dc:creator><description>&amp;gt; You describe a subtype in terms of a subset of the possible values of a super type. But in practice (in most all exaples I have seen , in textbooks and so forth) the opposite is true
&amp;gt; For example the class java.util.zip.JarFile extends java.util.zip.ZipFile, but it represents both a zip file AND a jar file, so clearly it's set of possible values is larger.

If JarFile is a subtype of ZipFile then every instance of JarFile is also an instance of ZipFile.  But that's the definition of &amp;quot;subset&amp;quot; -- if every member of X is a member of Y then X is a subset of Y.  

Therefore, I'm not following your train of thought here.  The set of all objects that are ZipFiles contains all JarFiles plus all the non-JarFile instances of ZipFiles, so how could JarFiles be a larger set?</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53345</link><pubDate>Fri, 07 Nov 2003 11:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53345</guid><dc:creator>Curt Sampson</dc:creator><description>You seem to have confused the &amp;quot;set of possible values.&amp;quot; With type systems here we're concerned about the number of possible values a variable can hold. A variable of type ZipFile can reference any ZipFile object, and any JarFile object. But a variable of type JarFile can reference only JarFile objects, not ZipFile objects that are not JarFile objects. Thus, a JarFile variable holds a subset of the values that can be held in a ZipFile variable.</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53346</link><pubDate>Fri, 07 Nov 2003 23:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53346</guid><dc:creator>Just some guy</dc:creator><description>Eric Lippert Writes:
&amp;quot;I'm not following your train of thought here...if every member of X is a member of Y then X is a subset of Y. &amp;quot;

Thank you for clearing me up. My confusion was in thinking that increasing the functionality in the subclass increases the values it can represent... like a TitledBorder with a Border superclass. 

I just assumed that the Border type does not include a border with a title, because  it does not properly handle the title, ( has no operators for title)  it shold not include values with titles....  only the TitledBorder type  should include that valeu.  But a TitledBorder also can handle a border with not title, as it is also a border. So I just assumed that TitledBorder will represnt a superset..

But as you note, the Border type should include borders that have titles. Even if it can not represent thoese titles properly.  It just seemed silly that a border without a title is not a valid TitledBorder  (or a zipfile without a manifes is a valid jar file).. but the reverse is ture, even though i have no way of representing/manipulating the title/manifest.

But i see my fault. :)
</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53347</link><pubDate>Sat, 08 Nov 2003 01:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53347</guid><dc:creator>Isaac</dc:creator><description>&amp;gt; It sounds like he is talking about variables, and that's fine
Yes, the definition is in terms of the range of values that can be assigned to a variable. 

&amp;gt; but in some OO languages you don't have data variables,
&amp;gt; you have references to objects
The distinction between value types and reference types is unimportant to this definition. In a typed language we may restrict a variable to hold references to a particular type - such as POINTER TO ZipFile. In an untyped language we don't do that. 

&amp;gt; Now which is the variable? The object or the reference that holds it? 
&amp;gt; If you say it is the reference, then all references have the same type
I'm unsure what you mean. Let's be more concrete (Smalltalk):
   | myVariable |
   myVariable := OrderedCollection new.
Now myVariable holds a reference to an instance of OrderedCollection.
   myVariable := Dictionary new.
Now myVariable holds a reference to an instance of Dictionary.

myVariable is &amp;quot;the variable&amp;quot;. Any value can be assigned to myVariable. The language is untyped.

In some other language
   ArrayList myVariable = new ArrayList();
Now myVariable holds a reference to an instance of ArrayList.
   myVariable = new HashMap();
***Fails - the range of values that myVariable may hold is restricted to references to ArrayList instances. The language is typed.

This kind of definition has been called misleading because languages without typed variables do have values of different types.
OTOH being *untyped* is the easy explanation for why we have ad-hoc polymorphism and loose-coupling in these languages. They got rid of type restrictions - proud to be untyped and safe.

In that universe, &amp;quot;JScript Classic&amp;quot; would be untyped, and &amp;quot;JScript .NET&amp;quot; would be typed (presumably &amp;quot;untyped&amp;quot; variables are &amp;quot;restricted&amp;quot; to some universal type).



</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53348</link><pubDate>Sat, 08 Nov 2003 01:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53348</guid><dc:creator>Michael Feathers</dc:creator><description>&amp;gt;This kind of definition has been called misleading because languages &amp;gt;without typed variables do have values of different types.
&amp;gt;OTOH being *untyped* is the easy explanation for why we have ad-hoc &amp;gt;polymorphism and loose-coupling in these languages. They got rid of type &amp;gt;restrictions - proud to be untyped and safe.

Except they didn't.  

myVariable := Dictionary new.
myVariable floggle.

Sending floogle to myVariable is a type error, it is just one detected at run-time.
</description></item><item><title>RE: The JScript Type System, Part One</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#53349</link><pubDate>Sat, 08 Nov 2003 05:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53349</guid><dc:creator>Isaac</dc:creator><description>Can we find a better forum for this discussion?

&amp;gt;&amp;gt; They got rid of type restrictions
&amp;gt; Except they didn't. 

Wasn't it clear from the context that &amp;quot;type restrictions&amp;quot; refered to type restrictions on variables? Ain't none of those in Smalltalk ;-)

I'm a bit surprised that there's anything controversial about this - back in 1990, Justin Graver &amp;amp; Ralph Johnson were quite clear that &amp;quot;Smalltalk is untyped&amp;quot;, see &amp;quot;A Type System for Smalltalk&amp;quot;.
http://citeseer.nj.nec.com/graver90type.html

&amp;gt; Sending floogle to myVariable is a type error
It might be if Dictionary was a type, but it isn't - it's a class. Implementation is inherited, specification is ignored (notably in Dictionary being a subclass of Set - see the paper). 
So &amp;quot;Sending floogle to myVariable&amp;quot; is just messageNotUnderstood ;-)
</description></item><item><title>Re:[HS Total]  [Objet ou pas?] - Page 2 | hilpers</title><link>http://blogs.msdn.com/ericlippert/archive/2003/11/05/53336.aspx#9368867</link><pubDate>Thu, 22 Jan 2009 19:15:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9368867</guid><dc:creator>Re:[HS Total]  [Objet ou pas?] - Page 2 | hilpers</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.hilpers.fr/526142-hs-total-objet-ou-pas/2"&gt;http://www.hilpers.fr/526142-hs-total-objet-ou-pas/2&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>