[Swift-devel] Making "type" more uniform
Michael Wilde
wilde at mcs.anl.gov
Wed Feb 4 07:55:03 CST 2009
In thinking through the question on "type file", I found that the
current "type" statement has several irregularities.
These are not of major consequence, but I want to verify my
understanding and propose that we document this in the user guide in the
short term, and in the longer term, consider changes to make "type" more
regular and useful.
Currently (as far as I can tel), the "type" statement can be used to
declare new data types of exactly 2 kinds:
1) named, mapped types representing a single file, and
2) named struct types.
You can not declare a new type that represents a (usable) simple type,
and you can not (as far as I can tell) declare a new type that
represents an array. While a few examples of declaring new simple scalar
types are accepted, they are of no practical use - you can not assign
values to such variables.
While you can declare a type whose representation is an int:
type Flag int;
Flag f;
You can not then say:
f = 99;
because f is a Flag and 99 is an int.
This is because of the following, as far as I can tell:
Creating new types whose representation is a "simple type" (int, string,
boolean, or float), while potentially useful, does not work, because you
can not create or assign any values of such types: you can not "cast"
constants or variables of the simple types to any named type declared
with the same representation, and there is no way to return such values
from any function, atomic or compound.
In contrast, creating multiple declared types whose representation is a
"marker" is useful, and works, because app() functions essentially
"cast" physical files as return values of any marker type. Thus, a
variable of type "image" can be assigned a value because, in the
userguide example, the convert command called by the rotate()
app-function "casts" its returned marker file type as an "image".
To enable the use of types that are represented as simple type values,
we would need to add a cast expression to the language.
Further, types are the only way to declare a struct: you can not declare
a struct in a variable declaration; you need a type to define the
struct. While you can only declare an array in a variable declaration,
and you can not, it seems, declare one as a type. (Although you can
declare array variables within struct types).
These are all restrictions which, while seemingly irregular, are not in
practice very limiting.
Its not clear how important this is, and I'm not proposing it at the
moment, but rather suggest we clarify some of these corners of the
language in the user guide, so users dont get tripped up trying to do
things that seem natural in, e.g., C.
More information about the Swift-devel
mailing list