Group "typedef" language feature


(Elijah Johnson) #1

I don’t know if this feature has yet existed in any languague, but its
pretty simple.

Let say you are preparing data for a JSON encoder. Right now, you would add
to an Dictionary or Array like this one:

let data = [String:Any]();

but what one is really saying is that “Any” is from a number of distinct
types:

group typedef JSONValueType : String, Double, Int, [String:JSONValueType],
[JSONValueType], Bool

So from the compiler’s point of view, this group typedef would be just like
the “Any” type, only it would refuse to compile unless the proper type was
passed. So the result would be, as an example, that there could be created
an (almost) fail-safe JSON encoder:

let data = [String:JSONValueType]();
data[“key] = “value”;

I’m sure this has plenty of uses outside of JSON encoding and would be
pretty simple to do. One problem that it would solve it the fact that in
Swift, everything is included by Any, including optionals, ex:

let b : String? = nil;

let a : Any = b; // result is warning: “Expression implicitly coerced from
String? to Any"

and so there is no way to include value types and objects without also
including optionals, though this “Any” (like Object and void* in Java/C)
syntax makes for poorly documented code when the type isn’t really “Any”,
but a collection of known types.

There already exist protocols, but they are not useful for types that one
has not defined (like the basic system types), or for mixing particular
values, structs, and classes.


(David Sweeris) #2

What are your thoughts on enums for this use-case?

- Dave Sweeris

···

On Mar 12, 2017, at 7:52 PM, Elijah Johnson via swift-evolution <swift-evolution@swift.org> wrote:

I don’t know if this feature has yet existed in any languague, but its pretty simple.

Let say you are preparing data for a JSON encoder. Right now, you would add to an Dictionary or Array like this one:

let data = [String:Any]();

but what one is really saying is that “Any” is from a number of distinct types:

group typedef JSONValueType : String, Double, Int, [String:JSONValueType], [JSONValueType], Bool

So from the compiler’s point of view, this group typedef would be just like the “Any” type, only it would refuse to compile unless the proper type was passed. So the result would be, as an example, that there could be created an (almost) fail-safe JSON encoder:

let data = [String:JSONValueType]();
data[“key] = “value”;

I’m sure this has plenty of uses outside of JSON encoding and would be pretty simple to do. One problem that it would solve it the fact that in Swift, everything is included by Any, including optionals, ex:

let b : String? = nil;
let a : Any = b; // result is warning: “Expression implicitly coerced from String? to Any"

and so there is no way to include value types and objects without also including optionals, though this “Any” (like Object and void* in Java/C) syntax makes for poorly documented code when the type isn’t really “Any”, but a collection of known types.

There already exist protocols, but they are not useful for types that one has not defined (like the basic system types), or for mixing particular values, structs, and classes.


(Jaden Geller) #3

Swift already supports equivalent semantics to what you’re looking for, though conditional conformances are required for this to work with dictionaries and arrays too.

protocol JSONValueType { }
extension String: JSONValueType { }
extension Double: JSONValueType { }
extension Int: JSONValueType { }
extension Dictionary: JSONValueType where Key == String, Value: JSONValueType { }
extension Array: JSONValueType where Element: JSONValueType { }
extension Bool: JSONValueType { }

let data = [String : JSONValueType]()
data[key] = "value"

let b: String? = nil
let a: JSONValueType = b // error: value of optional type 'Optional<string>' not unwrapped

Cheers,
Jaden Geller

···

On Mar 12, 2017, at 7:52 PM, Elijah Johnson via swift-evolution <swift-evolution@swift.org> wrote:

I don’t know if this feature has yet existed in any languague, but its pretty simple.

Let say you are preparing data for a JSON encoder. Right now, you would add to an Dictionary or Array like this one:

let data = [String:Any]();

but what one is really saying is that “Any” is from a number of distinct types:

group typedef JSONValueType : String, Double, Int, [String:JSONValueType], [JSONValueType], Bool

So from the compiler’s point of view, this group typedef would be just like the “Any” type, only it would refuse to compile unless the proper type was passed. So the result would be, as an example, that there could be created an (almost) fail-safe JSON encoder:

let data = [String:JSONValueType]();
data[“key] = “value”;

I’m sure this has plenty of uses outside of JSON encoding and would be pretty simple to do. One problem that it would solve it the fact that in Swift, everything is included by Any, including optionals, ex:

let b : String? = nil;
let a : Any = b; // result is warning: “Expression implicitly coerced from String? to Any"

and so there is no way to include value types and objects without also including optionals, though this “Any” (like Object and void* in Java/C) syntax makes for poorly documented code when the type isn’t really “Any”, but a collection of known types.

There already exist protocols, but they are not useful for types that one has not defined (like the basic system types), or for mixing particular values, structs, and classes.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tony Allevato) #4

This strikes me as fairly equivalent to "union types", which are on the
frequently rejected proposals list:

···

On Mon, Mar 13, 2017 at 4:14 PM Elijah Johnson via swift-evolution < swift-evolution@swift.org> wrote:

I don’t know if this feature has yet existed in any languague, but its
pretty simple.

Let say you are preparing data for a JSON encoder. Right now, you would
add to an Dictionary or Array like this one:

let data = [String:Any]();

but what one is really saying is that “Any” is from a number of distinct
types:

group typedef JSONValueType : String, Double, Int, [String:JSONValueType],
[JSONValueType], Bool

So from the compiler’s point of view, this group typedef would be just
like the “Any” type, only it would refuse to compile unless the proper type
was passed. So the result would be, as an example, that there could be
created an (almost) fail-safe JSON encoder:

let data = [String:JSONValueType]();
data[“key] = “value”;

I’m sure this has plenty of uses outside of JSON encoding and would be
pretty simple to do. One problem that it would solve it the fact that in
Swift, everything is included by Any, including optionals, ex:

let b : String? = nil;

let a : Any = b; // result is warning: “Expression implicitly coerced
from String? to Any"

and so there is no way to include value types and objects without also
including optionals, though this “Any” (like Object and void* in Java/C)
syntax makes for poorly documented code when the type isn’t really “Any”,
but a collection of known types.

There already exist protocols, but they are not useful for types that one
has not defined (like the basic system types), or for mixing particular
values, structs, and classes.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Elijah Johnson) #5

Sounds like it would work if you don’t get bothered by the overhead of
using a wrapper, including the extra lines of code for it. Even for the
small example provided with the JSON, it would be a lot of extra
boilerplate code. Any works just fine as long as you know what to put in it
and what it contains. Interesting question though, what is the difference
between the two, except that there is a nice switch operator and some more
flexibility. I suppose they are rightly afraid that someone wil make a
function that returns Int | String.

···

On March 13, 2017 at 7:23:24 PM, David Sweeris (davesweeris@mac.com) wrote:

On Mar 12, 2017, at 7:52 PM, Elijah Johnson via swift-evolution < swift-evolution@swift.org> wrote:

I don’t know if this feature has yet existed in any languague, but its
pretty simple.

Let say you are preparing data for a JSON encoder. Right now, you would add
to an Dictionary or Array like this one:

let data = [String:Any]();

but what one is really saying is that “Any” is from a number of distinct
types:

group typedef JSONValueType : String, Double, Int, [String:JSONValueType],
[JSONValueType], Bool

So from the compiler’s point of view, this group typedef would be just like
the “Any” type, only it would refuse to compile unless the proper type was
passed. So the result would be, as an example, that there could be created
an (almost) fail-safe JSON encoder:

let data = [String:JSONValueType]();
data[“key] = “value”;

I’m sure this has plenty of uses outside of JSON encoding and would be
pretty simple to do. One problem that it would solve it the fact that in
Swift, everything is included by Any, including optionals, ex:

let b : String? = nil;
let a : Any = b; // result is warning: “Expression implicitly coerced from
String? to Any"

and so there is no way to include value types and objects without also
including optionals, though this “Any” (like Object and void* in Java/C)
syntax makes for poorly documented code when the type isn’t really “Any”,
but a collection of known types.

There already exist protocols, but they are not useful for types that one
has not defined (like the basic system types), or for mixing particular
values, structs, and classes.

What are your thoughts on enums for this use-case?

- Dave Sweeris


(Tony Allevato) #6

This strikes me as fairly equivalent to "union types", which are on the
frequently rejected proposals list:

(hit Send too soon)

···

On Mon, Mar 13, 2017 at 4:41 PM Tony Allevato <tony.allevato@gmail.com> wrote:

On Mon, Mar 13, 2017 at 4:14 PM Elijah Johnson via swift-evolution < > swift-evolution@swift.org> wrote:

I don’t know if this feature has yet existed in any languague, but its
pretty simple.

Let say you are preparing data for a JSON encoder. Right now, you would
add to an Dictionary or Array like this one:

let data = [String:Any]();

but what one is really saying is that “Any” is from a number of distinct
types:

group typedef JSONValueType : String, Double, Int, [String:JSONValueType],
[JSONValueType], Bool

So from the compiler’s point of view, this group typedef would be just
like the “Any” type, only it would refuse to compile unless the proper type
was passed. So the result would be, as an example, that there could be
created an (almost) fail-safe JSON encoder:

let data = [String:JSONValueType]();
data[“key] = “value”;

I’m sure this has plenty of uses outside of JSON encoding and would be
pretty simple to do. One problem that it would solve it the fact that in
Swift, everything is included by Any, including optionals, ex:

let b : String? = nil;

let a : Any = b; // result is warning: “Expression implicitly coerced
from String? to Any"

and so there is no way to include value types and objects without also
including optionals, though this “Any” (like Object and void* in Java/C)
syntax makes for poorly documented code when the type isn’t really “Any”,
but a collection of known types.

There already exist protocols, but they are not useful for types that one
has not defined (like the basic system types), or for mixing particular
values, structs, and classes.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tony Allevato) #7

This strikes me as fairly equivalent to "union types", which are on the
frequently rejected proposals list:

(hit Send too soon)

(My e-mail client hates me today; apologies for the spam)

https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md

I don’t know if this feature has yet existed in any languague, but its
pretty simple.

Let say you are preparing data for a JSON encoder. Right now, you would
add to an Dictionary or Array like this one:

let data = [String:Any]();

but what one is really saying is that “Any” is from a number of distinct
types:

group typedef JSONValueType : String, Double, Int, [String:JSONValueType],
[JSONValueType], Bool

So from the compiler’s point of view, this group typedef would be just
like the “Any” type, only it would refuse to compile unless the proper type
was passed. So the result would be, as an example, that there could be
created an (almost) fail-safe JSON encoder:

let data = [String:JSONValueType]();
data[“key] = “value”;

I’m sure this has plenty of uses outside of JSON encoding and would be
pretty simple to do. One problem that it would solve it the fact that in
Swift, everything is included by Any, including optionals, ex:

let b : String? = nil;

let a : Any = b; // result is warning: “Expression implicitly coerced
from String? to Any"

and so there is no way to include value types and objects without also
including optionals, though this “Any” (like Object and void* in Java/C)
syntax makes for poorly documented code when the type isn’t really “Any”,
but a collection of known types.

There already exist protocols, but they are not useful for types that one
has not defined (like the basic system types), or for mixing particular
values, structs, and classes.

You can retroactively extend any type to support a protocol, so that's one
way that you could achieve this. The catch is that you don't want arbitrary
types outside the set that you bless to be able to conform to that
protocol. For that case, I think being able to define a "closed" protocol
would be nice and achieve what you want,

···

On Mon, Mar 13, 2017 at 4:41 PM Tony Allevato <tony.allevato@gmail.com> wrote:

On Mon, Mar 13, 2017 at 4:41 PM Tony Allevato <tony.allevato@gmail.com> > wrote:
On Mon, Mar 13, 2017 at 4:14 PM Elijah Johnson via swift-evolution < > swift-evolution@swift.org> wrote:

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Elijah Johnson) #8

Well, as to "This type of constraint issomething that the type system
cannot and should not support.”” I would point out that it sounds very
feasible is the underlying type was “Any” since all that would be needed is
a few checks. I would definitely agree that making a new “union” type would
be pointless. I’m not so sure about the “should not” in this case, as we
already have the type “Any” and “AnyObject” which don’t give the slightest
hint as to what they contain.

If it is difficult, then of course the “Any” type is already sufficient. It
is weird that it can contain optionals, but the compile warnings seem to
make it workable.

This strikes me as fairly equivalent to "union types", which are on the
frequently rejected proposals list:

(hit Send too soon)

(My e-mail client hates me today; apologies for the spam)

https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md

I don’t know if this feature has yet existed in any languague, but its
pretty simple.

Let say you are preparing data for a JSON encoder. Right now, you would
add to an Dictionary or Array like this one:

let data = [String:Any]();

but what one is really saying is that “Any” is from a number of distinct
types:

group typedef JSONValueType : String, Double, Int, [String:JSONValueType],
[JSONValueType], Bool

So from the compiler’s point of view, this group typedef would be just
like the “Any” type, only it would refuse to compile unless the proper type
was passed. So the result would be, as an example, that there could be
created an (almost) fail-safe JSON encoder:

let data = [String:JSONValueType]();
data[“key] = “value”;

I’m sure this has plenty of uses outside of JSON encoding and would be
pretty simple to do. One problem that it would solve it the fact that in
Swift, everything is included by Any, including optionals, ex:

let b : String? = nil;

let a : Any = b; // result is warning: “Expression implicitly coerced
from String? to Any"

and so there is no way to include value types and objects without also
including optionals, though this “Any” (like Object and void* in Java/C)
syntax makes for poorly documented code when the type isn’t really “Any”,
but a collection of known types.

There already exist protocols, but they are not useful for types that one
has not defined (like the basic system types), or for mixing particular
values, structs, and classes.

You can retroactively extend any type to support a protocol, so that's one
way that you could achieve this. The catch is that you don't want arbitrary
types outside the set that you bless to be able to conform to that
protocol. For that case, I think being able to define a "closed" protocol
would be nice and achieve what you want,

···

On March 13, 2017 at 7:44:04 PM, Tony Allevato (tony.allevato@gmail.com) wrote:
On Mon, Mar 13, 2017 at 4:41 PM Tony Allevato <tony.allevato@gmail.com> wrote:

On Mon, Mar 13, 2017 at 4:41 PM Tony Allevato <tony.allevato@gmail.com> > wrote:
On Mon, Mar 13, 2017 at 4:14 PM Elijah Johnson via swift-evolution < > swift-evolution@swift.org> wrote:

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution