Strong/static typing in Java and C/C++

C

Chris Smith

Christopher said:
Right, but that is because Java does not support subtypes other than
subclasses. In languages with more general type systems, such as Curl, you
cannot get away with using the terms "type" and "class" to distinguish between
runtime and static types. I suppose you can say the "variable type" is the
static type and the "value type" is the dynamic type, and I think that is
something like what you are suggesting, but I think it is clearer to speak of
the "static" or "compile-time" type vs. the "runtime" or "dynamic" type and
thus avoid having to distinguish the difference between a variable and a
value.

Well, you're saying the same thing, but using different words. I guess
all this goes to show is that terminology differs. It would be nice if
"type" could mean one thing to everyone, but your attempt to find a
common definition between languages has led to rendering the term
useless. I guess you just have to keep the language in mind, and use
"type" as it's used in that language. In Java and most other languages
I've worked in, a type is, by definition, a compile-time concept.
Variables and expressions have types, not values.

If Curl uses different terminology, so be it. Who am I to say that
people who use "type" in a different way when talking about Curl are
wrong? I don't even know the language! I can only say that they are in
the minority. Using "type" when you mean "class" in Java or C++, for
example, is definitely wrong, and somewhat confusing to boot.
I think of dynamic typing as referring to the ability to make decisions based
on the runtime type of values.

Really? That's nothing like the way "dynamic typing" is used anywhere
else that I've seen. Generally, "dynamic typing" means that the
compiler doesn't track types. A language where there's tracking of
types and that type information is used to determine the meaning of code
is statically typed, regardless of the existence of a command to get the
kind of value held by a variable (which is not a type anyway under the
normal definition).

It's rather unfortunate that the term "dynamic typing" ever existed, but
we can track it back to human tendency to not want to be without
anything (even if it's not necessarily beneficial), so that designers
and programmers in languages without types had to make up some reason
that they really do have it... thus eroding the concept in general.
The Curl language has an abstract type called 'any', which is the supertype of
all other types. Omitting a type declaration in Curl is equivalent to
explicitly declaring it to be of type 'any'. Such variables do have a fixed
static type of 'any', but may hold a value with any runtime type. So the
above definition would not work for Curl or other languages with a similar
type system.

Based on your later description, it appears that Curl's 'any' type is an
explicit renunciation of types. Too bad it has to be called a type just
to confuse people.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Christopher Barber

Chris Smith said:
Well, you're saying the same thing, but using different words. I guess
all this goes to show is that terminology differs. It would be nice if
"type" could mean one thing to everyone,

Yes, this is a pervasive problem in software engineering. Similar problems
arise from attempting to pin down the definition of "Object-Oriented" in a way
that is not specific to a few languages.
In Java and most other languages
I've worked in, a type is, by definition, a compile-time concept.

Well, then you need to expand your horizons a little bit! In many purely
dynamically typed languages, such as Python, the term 'type' is used but is
strictly a run-time concept.
Variables and expressions have types, not values.

No, they have both. For example, lets say we have a variable 'obj' with a
declared compile-time type of Object. At run-time, the variable may contain
different values (i.e. object references) during the execution of the program.
Each value has a run-time type which is a subtype of the compile-time type
(actually, in Java this might not be true since 'null' is not a subtype of
Object). In this terminology, a class is just a special kind of type.

I know this is not the approved Java terminology, but it is not really all
that hard to understand. It is also true that compile-time and run-time types
aren't quite the same thing, but they are similar enough that it is worthwhile
to use similar terminology. Especially, when talking about other programming
languages.
Using "type" when you mean "class" in Java or C++, for
example, is definitely wrong, and somewhat confusing to boot.

You might be right about Java, but I beg to differ about C++. The language
standard even includes support for what it calls "Run-Time Type
Identification" (RTTI) (which does only work for virtual classes, of course).
Really? That's nothing like the way "dynamic typing" is used anywhere
else that I've seen. Generally, "dynamic typing" means that the
compiler doesn't track types.

No that is not what it means. Dynamic typing is not defined by the absence of
static typing, although it is implied by it (except for languages with no
typing at all!).
A language where there's tracking of
types and that type information is used to determine the meaning of code
is statically typed, regardless of the existence of a command to get the
kind of value held by a variable (which is not a type anyway under the
normal definition).

I think that is fair to say. What I am saying is that static and dynamic are
not mutually exclusive.
Based on your later description, it appears that Curl's 'any' type is an
explicit renunciation of types. Too bad it has to be called a type just
to confuse people.

I am not sure why you say that. If for no other reason, 'any' is a type for
language consistency. It would be quite strange if everything except for
'any' were a type.

- Christopher
 
J

James Rogers

Yes, and this is true of C++ and Java as well.

Not at all. C++ and Java allow the assignment of values meeting an
is-a relationship for Classes. Neither language even provides a
mechanism for derivation of primitive types.

The implicit conversion provided in C, C++, and Java for values of
numeric types is not based upon an is-a relationship. It is based
upon structural compatibility.
I still think you are using a strange definition of static typing.
Static typing just means that variables have type information known at
compile-time which can be used to do compile-time type checking and
allow the compiler to perform a number of useful optimizations. Under
this definition, clearly the Curl programming language supports static
typing. How does your definition differ from this?

This is what I would call weak static typing.
This discussion has not mixed up the concepts of strong typing with
static typing and weak typing with dynamic typing.

It appears to me that Curl employs weak static typing. By this I
mean that there is a form of static typing. Variable types are known
at compile time. At the same time there is a weak relationship between
variable types and the values those variables "contain".

Compare this with Ada, which has both static typing and strong typing.
In Ada I can define two different numeric types:

type foo is range 1..10;
type bar is range -10..10;

Both types are integer types. Type 'foo' has a valid range of values
from 1 through 10 inclusive. Type 'bar' has a valid range of values
from -10 through 10 inclusive.

Let's create a variable of type foo and initialize it to the value 5.

foovar : foo := 5;

Now let's create a value of type bar and assign foo to it:

barvar : bar;

barvar := foovar;

This last assignment is prohibited by the Ada compiler. Types foo and bar
are separate types. Their values are not implicitly convertable from
one to the other. This is true even though both types can validly represent
the value 5.

Jim Rogers
 
C

Christopher Barber

James Rogers said:
Christopher Barber said:
Static typing just means that variables have type information known at
compile-time which can be used to do compile-time type checking and
allow the compiler to perform a number of useful optimizations. Under
this definition, clearly the Curl programming language supports static
typing. How does your definition differ from this?

This is what I would call weak static typing.
This discussion has not mixed up the concepts of strong typing with
static typing and weak typing with dynamic typing.

It appears to me that Curl employs weak static typing. By this I
mean that there is a form of static typing. Variable types are known
at compile time. At the same time there is a weak relationship between
variable types and the values those variables "contain".
[example omitted]

Curl is only somewhat weak, in that only some implicit conversions are
allowed, and like Java, no conversions implicit or explicit are permitted that
would violate language integrity (i.e. reinterpret bits in memory in an
arbitrary fashion).

- Christopher
 
D

Dale King

James Rogers said:
Compare this with Ada, which has both static typing and strong typing.
In Ada I can define two different numeric types:

type foo is range 1..10;
type bar is range -10..10;

Both types are integer types. Type 'foo' has a valid range of values
from 1 through 10 inclusive. Type 'bar' has a valid range of values
from -10 through 10 inclusive.

Let's create a variable of type foo and initialize it to the value 5.

foovar : foo := 5;

Now let's create a value of type bar and assign foo to it:

barvar : bar;

barvar := foovar;

This last assignment is prohibited by the Ada compiler. Types foo and bar
are separate types. Their values are not implicitly convertable from
one to the other. This is true even though both types can validly represent
the value 5.


You are wrong that Ada prohibits the assignment, just that it requires you
to be explicit and tell the compiler that you want it to do the conversion
and you didn't just accidently type that. It allows conversion it just never
does it implicitly. This is actually a very good thing. It takes a lot more
work to get a program to compile in a language like this (my experience is
with Modula instead of Ada, but the idea is the same), but once you get it
to compile it is usually correct.
 
R

Roedy Green

This last assignment is prohibited by the Ada compiler. Types foo and bar

Ever since Pascal days I wished I could invent subclasses of indexes
so that certain indexes could only be used with certain arrays.

I remember writing a crash proof Btree implementation. The bugbear was
were all the indexing variables, and sometimes going to sleep and
using the wrong one.

I solved the problem in my own language Abundance, by inventing the
implicit index. Since you rarely specify it, you can't very well get
it wrong. The other thing I did was name the array and the implicit
index the same thing. There was no way you could cross them.
 
J

James Rogers

Dale King said:
You are wrong that Ada prohibits the assignment, just that it requires
you to be explicit and tell the compiler that you want it to do the
conversion and you didn't just accidently type that. It allows
conversion it just never does it implicitly. This is actually a very
good thing. It takes a lot more work to get a program to compile in a
language like this (my experience is with Modula instead of Ada, but
the idea is the same), but once you get it to compile it is usually
correct. --

You are correct that Ada allows explicit conversions and then assignment.
That is not the same as the assignment shown above. The explicit
conversion syntax for the above would be:

barvar := bar(foovar);

Note that explicit conversions can result in invalid values. Ada also
gives the ability to detect invalid values without generating exceptions.

foovar := foo(barvar);
if foovar'Valid then
-- Do your calculations here
else
-- Handle your error conditions here
end if;

The value in foovar would be invalid if the value in barvar was
less than 1.

Jim Rogers
 
J

James Rogers

Ever since Pascal days I wished I could invent subclasses of indexes
so that certain indexes could only be used with certain arrays.

I remember writing a crash proof Btree implementation. The bugbear was
were all the indexing variables, and sometimes going to sleep and
using the wrong one.

I solved the problem in my own language Abundance, by inventing the
implicit index. Since you rarely specify it, you can't very well get
it wrong. The other thing I did was name the array and the implicit
index the same thing. There was no way you could cross them.

Ada also allows you to define the index type for arrays.
The Ada array declaration syntax includes a definition of the array
index range as well as the type of the array element.

type Balanced_Index is range -100..100;

type Centered_Array is array(Balanced_Index) of Float;

In the above example I first defined an integer type with a range
of values from -100 through 100. I then defined an array type
using that integer type for its index.

The purpose of this capability is to allow the programmer to directly
model the problem in the code. In C I would need to define roughly
equivalent array as

float foo[101];

I would then need to write a set of programs to map the values from
-100 through 100 to the index values 0 through 100.

Jim Rogers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top