Question

for example, something like this in Ada (if it were to support this):

type Type_Name is range bottom .. top;

where "bottom" and "top" are variables.

or something like this in C (if it were to support this):

struct s {
    int a;
    if (z<3) {
        char b;
        int c;
    } else {
        struct ss {
            int b;
            char c;
        }
    }
} v;

or, if c were to have the types after the variable identifiers in a declaration:

var if (z<3) int else char;

or something like that.

this is just out of curiosity. i don't even know if it would be useful, and i know it would create many problems like having to check whether something like s.ss.b existed before using it.

oh, perhaps i should restate the question from the title: are there any statically-typed languages with types decided at run-time? if so, what are they?

btw, perhaps this would mean that the language would not be statically typed. but i mean languages where the type of a variable must be stated before use (can't do this otherwise of course).

Was it helpful?

Solution

There's very little point in a statically-typed language which decides types at runtime. The point of static typing is to detect all type errors statically, which is to say, at compile time.

If you're going to decide types at runtime, you might as well discard the concept of "type" as such, and use a duck-typing approach, where what matters is whether or not an operation is supported.

The only reason to have a statically-typed language which decides types at runtime is to deal with cases which are undecidable at compile time. There are probably some languages like this, but I really don't pay attention to the language development scene any more, so I can't tell you.

OTHER TIPS

You can emulate dynamic usage in statically typed languages. There will be some limitation of course but you can do it

Some examples:

in c# the following code is a valid one.

object variant;

if(a<5)
     variant = new int();
else
     variant = new double();

and in c++

boost::variant< std::vector<int> , std::list<int> > v;

if (n > 10000)
   v=std::vector();
else
   v=std::list();

is valid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top