I don't know that we've ruled out using literal suffixes, but it's not really relevant.
We already have a syntax for coercing a literal to a specific type; the problem is
that people are writing UInt8(7) anyway, and it's completely reasonable for them
to expect that to work. So even if we added user-defined literal suffixes as a
feature, we'd still have this problem.
I can imagine Swift adding literal suffixes, but only to enable a units library, not
to provide yet another way to specify type widths.
John.
···
On Jun 3, 2016, at 8:11 PM, Ben Rimmington <me@benrimmington.com> wrote:
-- Ben
On 4 Jun 2016, at 02:20, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 5:31 PM, Ben Rimmington via swift-evolution <swift-evolution@swift.org> wrote:
John McCall wrote:
I think that's a very promising way of thinking about literals. Writing
a literal creates a notional value whose type is the informal, infinite-
precise type of all integer/FP/collection/etc. literals, which (1) can be
implicitly converted to any type that implements the appropriate protocol
and (2) in fact *must* be converted to some such type (possibly the
default type for that literal) in order for the code to be executable.
Could you allow IntegerLiteralConvertible.IntegerLiteralType associatedtype
to override the default Swift.IntegerLiteralType typealias iff there's more
than one unlabelled init(_:) to choose from? Then you can call the "correct"
init(_:) instead of calling init(integerLiteral:) as a "special case".
This is essentially already how it works. The literal protocols are not invoked via
overload resolution; Swift always invokes the initializer that satisfies the protocol
requirement.
That is, you can provide ten different init(integerLiteral: T) initializers, but that will
just prevent the compiler from inferring the associated type, so you'll have to
declare it. Once you do, that associated type will determine the initializer that
satisfies the requirement, and that'll always be the initializer chosen.
I think our decision should be based upon which syntactic construct feels least inconsistent with other similar syntactic constructs and therefore feels the least arbitrary. Restricting the usage of a generic `Literal` type to literal convertible initializers feels a little bit less arbitrary than allowing the call site to omit a label, but it feels a little bit more arbitrary than introducing an attribute that has a very specialized context of applicability.
Honestly, I think `@literal` is only "consistent" with the rest of the language in that @s are used to mark compiler magic. `Literal<>` is attempting to *not* be much more magical than, say, `StringInterpolationConvertible`, where a particular syntax generates a certain pattern of calls.
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
I'm actually quite happy with the user-facing aspects of the current literal protocols and see zero reason to update them for this, so if that's the choice, I'll just leave this aside.
John.
···
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
I would be very interested in seeing your original proposal presented
for formal review.
Austin
···
On 6/3/16, John McCall via swift-evolution <swift-evolution@swift.org> wrote:
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution >>> <swift-evolution@swift.org> wrote:
Using an external parameter label in a declaration and allowing it to be
omitted at the call site does not feel like the right answer to me. I
think we can find a better way to present this syntactically.
+eleventy gajillion
I'm actually quite happy with the user-facing aspects of the current literal
protocols and see zero reason to update them for this, so if that's the
choice, I'll just leave this aside.
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
+eleventy gajillion
I'm actually quite happy with the user-facing aspects of the current literal protocols and see zero reason to update them for this, so if that's the choice, I'll just leave this aside.
Are you suggesting that preserving the current syntax of the literal protocols is more important than fixing this behavior so it works like most people seem to expect? Why not be open to other syntactic solutions?
···
Sent from my iPad
On Jun 3, 2016, at 6:23 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
I think fixing the behavior is good, and I stand by this proposal. There is, however, nothing glaringly wrong with the literal protocols. They do not need to redesigned simply because we found a syntactic interaction that needs to be cleaned up.
There are good reasons the protocols have evolved the way they have. The labels clearly mark the purpose of each initializer and distinguish one from another on types that support multiple literal kinds. The labels also clearly indicate that the initializers are not intended for general use. The argument(s) to the initializer do not always represent a single value of literal type. Some protocols provide multiple initializers, and it is quite possible that that will become gradually more common as we explore ways to grant more flexibility to code outside of the standard library. And we actually consider it good that you can invoke these initializers explicitly; it's only accidental use that we feel it's important to avoid, which labels provide excellent protection against.
If you have specific criticisms of any of the literal protocols, e.g. you find them limiting or non-performant for your own types, then you are welcome to raise them here. However, resemblance to actual literal syntax has never been a design goal for the literal protocols; and besides, this will always be a secondary literal syntax, one far less important than contextual typing.
John.
···
On Jun 3, 2016, at 5:13 PM, Matthew Johnson <matthew@anandabits.com> wrote:
On Jun 3, 2016, at 6:23 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
+eleventy gajillion
I'm actually quite happy with the user-facing aspects of the current literal protocols and see zero reason to update them for this, so if that's the choice, I'll just leave this aside.
Are you suggesting that preserving the current syntax of the literal protocols is more important than fixing this behavior so it works like most people seem to expect? Why not be open to other syntactic solutions?
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
+eleventy gajillion
I'm actually quite happy with the user-facing aspects of the current literal protocols and see zero reason to update them for this, so if that's the choice, I'll just leave this aside.
Are you suggesting that preserving the current syntax of the literal protocols is more important than fixing this behavior so it works like most people seem to expect? Why not be open to other syntactic solutions?
I think fixing the behavior is good, and I stand by this proposal.
I agree fixing it is good and we should do it. I’m just trying to explore the solution space to see if there are any alternatives that might be more appealing. Clearly you’ve thought about this a lot but it’s a new topic to consider for the rest of us. :)
There is, however, nothing glaringly wrong with the literal protocols. They do not need to redesigned simply because we found a syntactic interaction that needs to be cleaned up.
There are good reasons the protocols have evolved the way they have. The labels clearly mark the purpose of each initializer and distinguish one from another on types that support multiple literal kinds. The labels also clearly indicate that the initializers are not intended for general use. The argument(s) to the initializer do not always represent a single value of literal type. Some protocols provide multiple initializers, and it is quite possible that that will become gradually more common as we explore ways to grant more flexibility to code outside of the standard library. And we actually consider it good that you can invoke these initializers explicitly; it's only accidental use that we feel it's important to avoid, which labels provide excellent protection against.
If it is important that these initializers be callable directly I suppose the label is the only way to go. I can’t think of a reason why this is necessary though. I would consider it bad design for a type that expects to be initialized with an Int variable to require its callers to use the label, as opposed to providing an alternate initializer that doesn’t have a label with “literal” in its name. Are there specific use cases where you think this capability is important or is it just the principle that you should be able to call any initializer you write?
-Matthew
···
On Jun 3, 2016, at 8:11 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 5:13 PM, Matthew Johnson <matthew@anandabits.com> wrote:
On Jun 3, 2016, at 6:23 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
If you have specific criticisms of any of the literal protocols, e.g. you find them limiting or non-performant for your own types, then you are welcome to raise them here. However, resemblance to actual literal syntax has never been a design goal for the literal protocols; and besides, this will always be a secondary literal syntax, one far less important than contextual typing.
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
+eleventy gajillion
I'm actually quite happy with the user-facing aspects of the current literal protocols and see zero reason to update them for this, so if that's the choice, I'll just leave this aside.
Are you suggesting that preserving the current syntax of the literal protocols is more important than fixing this behavior so it works like most people seem to expect? Why not be open to other syntactic solutions?
I think fixing the behavior is good, and I stand by this proposal.
I agree fixing it is good and we should do it. I’m just trying to explore the solution space to see if there are any alternatives that might be more appealing. Clearly you’ve thought about this a lot but it’s a new topic to consider for the rest of us. :)
I'm sorry if I've come across as frustrated. It's easy for me to forget that things that I consider well-settled haven't always been covered in depth here. The community is not intentionally re-inventing things for no purpose, it's making a genuine effort to explore the constraints on a part of the language that it isn't familiar with the rationale for.
There is, however, nothing glaringly wrong with the literal protocols. They do not need to redesigned simply because we found a syntactic interaction that needs to be cleaned up.
There are good reasons the protocols have evolved the way they have. The labels clearly mark the purpose of each initializer and distinguish one from another on types that support multiple literal kinds. The labels also clearly indicate that the initializers are not intended for general use. The argument(s) to the initializer do not always represent a single value of literal type. Some protocols provide multiple initializers, and it is quite possible that that will become gradually more common as we explore ways to grant more flexibility to code outside of the standard library. And we actually consider it good that you can invoke these initializers explicitly; it's only accidental use that we feel it's important to avoid, which labels provide excellent protection against.
If it is important that these initializers be callable directly I suppose the label is the only way to go. I can’t think of a reason why this is necessary though. I would consider it bad design for a type that expects to be initialized with an Int variable to require its callers to use the label, as opposed to providing an alternate initializer that doesn’t have a label with “literal” in its name. Are there specific use cases where you think this capability is important or is it just the principle that you should be able to call any initializer you write?
Partly that principle, but partly the ability to forward literal-initialization. You can forward literal-initialization to, say, a BuiltinIntegerLiteralConvertible type by just appointing it your associated IntegerLiteralType, but if you're wrapping another type, that doesn't work. For example:
On Jun 3, 2016, at 7:08 PM, Matthew Johnson <matthew@anandabits.com> wrote:
On Jun 3, 2016, at 8:11 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 5:13 PM, Matthew Johnson <matthew@anandabits.com> wrote:
On Jun 3, 2016, at 6:23 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
+eleventy gajillion
I'm actually quite happy with the user-facing aspects of the current literal protocols and see zero reason to update them for this, so if that's the choice, I'll just leave this aside.
Are you suggesting that preserving the current syntax of the literal protocols is more important than fixing this behavior so it works like most people seem to expect? Why not be open to other syntactic solutions?
I think fixing the behavior is good, and I stand by this proposal.
I agree fixing it is good and we should do it. I’m just trying to explore the solution space to see if there are any alternatives that might be more appealing. Clearly you’ve thought about this a lot but it’s a new topic to consider for the rest of us. :)
I'm sorry if I've come across as frustrated. It's easy for me to forget that things that I consider well-settled haven't always been covered in depth here. The community is not intentionally re-inventing things for no purpose, it's making a genuine effort to explore the constraints on a part of the language that it isn't familiar with the rationale for.
No problem. I know it’s not fun to revisit hard-thought decisions that you’re happy with! :) And I can see how it feels like we’re trying to do that here. That said, these discussions are the best way for the community to become familiar with some of the rationale for the way things currently are. :)
There is, however, nothing glaringly wrong with the literal protocols. They do not need to redesigned simply because we found a syntactic interaction that needs to be cleaned up.
There are good reasons the protocols have evolved the way they have. The labels clearly mark the purpose of each initializer and distinguish one from another on types that support multiple literal kinds. The labels also clearly indicate that the initializers are not intended for general use. The argument(s) to the initializer do not always represent a single value of literal type. Some protocols provide multiple initializers, and it is quite possible that that will become gradually more common as we explore ways to grant more flexibility to code outside of the standard library. And we actually consider it good that you can invoke these initializers explicitly; it's only accidental use that we feel it's important to avoid, which labels provide excellent protection against.
If it is important that these initializers be callable directly I suppose the label is the only way to go. I can’t think of a reason why this is necessary though. I would consider it bad design for a type that expects to be initialized with an Int variable to require its callers to use the label, as opposed to providing an alternate initializer that doesn’t have a label with “literal” in its name. Are there specific use cases where you think this capability is important or is it just the principle that you should be able to call any initializer you write?
Partly that principle, but partly the ability to forward literal-initialization. You can forward literal-initialization to, say, a BuiltinIntegerLiteralConvertible type by just appointing it your associated IntegerLiteralType, but if you're wrapping another type, that doesn't work. For example:
What is the advantage of calling the literal JSONValue initializer here rather than a non-literal initializer that accepts JSONValue.IntegerLiteralType? Is there something in the implementation that makes this more efficient?
If forwarding is the intended use for explicit calls, wouldn’t the `@literal` attribute attached to the type be an alternative way to address the forwarding use case?
Are there any non-forwarding use cases for explicit calls?
If forwarding is the only intended use case of explicit calls and `@literal` could cover that use case then the question (in my mind) is which principle do we hold more sacred? The ability to call any initializer we write or the syntactic consistency that external argument labels are always required at the call site? I admit this is partly an aesthetic concern…
-Matthew
···
On Jun 3, 2016, at 9:30 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 7:08 PM, Matthew Johnson <matthew@anandabits.com> wrote:
On Jun 3, 2016, at 8:11 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 5:13 PM, Matthew Johnson <matthew@anandabits.com> wrote:
On Jun 3, 2016, at 6:23 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
+eleventy gajillion
I'm actually quite happy with the user-facing aspects of the current literal protocols and see zero reason to update them for this, so if that's the choice, I'll just leave this aside.
Are you suggesting that preserving the current syntax of the literal protocols is more important than fixing this behavior so it works like most people seem to expect? Why not be open to other syntactic solutions?
I think fixing the behavior is good, and I stand by this proposal.
I agree fixing it is good and we should do it. I’m just trying to explore the solution space to see if there are any alternatives that might be more appealing. Clearly you’ve thought about this a lot but it’s a new topic to consider for the rest of us. :)
I'm sorry if I've come across as frustrated. It's easy for me to forget that things that I consider well-settled haven't always been covered in depth here. The community is not intentionally re-inventing things for no purpose, it's making a genuine effort to explore the constraints on a part of the language that it isn't familiar with the rationale for.
No problem. I know it’s not fun to revisit hard-thought decisions that you’re happy with! :) And I can see how it feels like we’re trying to do that here. That said, these discussions are the best way for the community to become familiar with some of the rationale for the way things currently are. :)
I find It helps to read the docs that has been produced (not all up to date but still rich in background data). The source code is also a great place to do a reality chk on ideas.
On Jun 4, 2016, at 4:00 PM, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
On Jun 3, 2016, at 9:30 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 7:08 PM, Matthew Johnson <matthew@anandabits.com> wrote:
On Jun 3, 2016, at 8:11 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 5:13 PM, Matthew Johnson <matthew@anandabits.com> wrote:
On Jun 3, 2016, at 6:23 PM, John McCall <rjmccall@apple.com> wrote:
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:
There is, however, nothing glaringly wrong with the literal protocols. They do not need to redesigned simply because we found a syntactic interaction that needs to be cleaned up.
There are good reasons the protocols have evolved the way they have. The labels clearly mark the purpose of each initializer and distinguish one from another on types that support multiple literal kinds. The labels also clearly indicate that the initializers are not intended for general use. The argument(s) to the initializer do not always represent a single value of literal type. Some protocols provide multiple initializers, and it is quite possible that that will become gradually more common as we explore ways to grant more flexibility to code outside of the standard library. And we actually consider it good that you can invoke these initializers explicitly; it's only accidental use that we feel it's important to avoid, which labels provide excellent protection against.
If it is important that these initializers be callable directly I suppose the label is the only way to go. I can’t think of a reason why this is necessary though. I would consider it bad design for a type that expects to be initialized with an Int variable to require its callers to use the label, as opposed to providing an alternate initializer that doesn’t have a label with “literal” in its name. Are there specific use cases where you think this capability is important or is it just the principle that you should be able to call any initializer you write?
Partly that principle, but partly the ability to forward literal-initialization. You can forward literal-initialization to, say, a BuiltinIntegerLiteralConvertible type by just appointing it your associated IntegerLiteralType, but if you're wrapping another type, that doesn't work. For example:
What is the advantage of calling the literal JSONValue initializer here rather than a non-literal initializer that accepts JSONValue.IntegerLiteralType? Is there something in the implementation that makes this more efficient?
If forwarding is the intended use for explicit calls, wouldn’t the `@literal` attribute attached to the type be an alternative way to address the forwarding use case?
Are there any non-forwarding use cases for explicit calls?
If forwarding is the only intended use case of explicit calls and `@literal` could cover that use case then the question (in my mind) is which principle do we hold more sacred? The ability to call any initializer we write or the syntactic consistency that external argument labels are always required at the call site? I admit this is partly an aesthetic concern…
Using an external parameter label in a declaration and allowing it to be omitted at the call site does not feel like the right answer to me. I think we can find a better way to present this syntactically.
+eleventy gajillion
I'm actually quite happy with the user-facing aspects of the current literal protocols and see zero reason to update them for this, so if that's the choice, I'll just leave this aside.
Are you suggesting that preserving the current syntax of the literal protocols is more important than fixing this behavior so it works like most people seem to expect? Why not be open to other syntactic solutions?
I think fixing the behavior is good, and I stand by this proposal.
I agree fixing it is good and we should do it. I’m just trying to explore the solution space to see if there are any alternatives that might be more appealing. Clearly you’ve thought about this a lot but it’s a new topic to consider for the rest of us. :)
I'm sorry if I've come across as frustrated. It's easy for me to forget that things that I consider well-settled haven't always been covered in depth here. The community is not intentionally re-inventing things for no purpose, it's making a genuine effort to explore the constraints on a part of the language that it isn't familiar with the rationale for.
No problem. I know it’s not fun to revisit hard-thought decisions that you’re happy with! :) And I can see how it feels like we’re trying to do that here. That said, these discussions are the best way for the community to become familiar with some of the rationale for the way things currently are. :)
I find It helps to read the docs that has been produced (not all up to date but still rich in background data). The source code is also a great place to do a reality chk on ideas.
Agree. I have read most of the docs found here: https://github.com/apple/swift/tree/master/docs\. But docs can only cover so much. A lot of knowledge is often shared communally, but not explicitly documented.
···
On Jun 4, 2016, at 10:27 AM, LM <laurent.mihalkovic@gmail.com> wrote:
On Jun 4, 2016, at 4:00 PM, Matthew Johnson via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
On Jun 3, 2016, at 9:30 PM, John McCall <rjmccall@apple.com <mailto:rjmccall@apple.com>> wrote:
On Jun 3, 2016, at 7:08 PM, Matthew Johnson <matthew@anandabits.com <mailto:matthew@anandabits.com>> wrote:
On Jun 3, 2016, at 8:11 PM, John McCall <rjmccall@apple.com <mailto:rjmccall@apple.com>> wrote:
On Jun 3, 2016, at 5:13 PM, Matthew Johnson <matthew@anandabits.com <mailto:matthew@anandabits.com>> wrote:
On Jun 3, 2016, at 6:23 PM, John McCall <rjmccall@apple.com <mailto:rjmccall@apple.com>> wrote:
On Jun 3, 2016, at 4:07 PM, David Sweeris <davesweeris@mac.com <mailto:davesweeris@mac.com>> wrote:
On Jun 3, 2016, at 16:17, Matthew Johnson via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
There is, however, nothing glaringly wrong with the literal protocols. They do not need to redesigned simply because we found a syntactic interaction that needs to be cleaned up.
There are good reasons the protocols have evolved the way they have. The labels clearly mark the purpose of each initializer and distinguish one from another on types that support multiple literal kinds. The labels also clearly indicate that the initializers are not intended for general use. The argument(s) to the initializer do not always represent a single value of literal type. Some protocols provide multiple initializers, and it is quite possible that that will become gradually more common as we explore ways to grant more flexibility to code outside of the standard library. And we actually consider it good that you can invoke these initializers explicitly; it's only accidental use that we feel it's important to avoid, which labels provide excellent protection against.
If it is important that these initializers be callable directly I suppose the label is the only way to go. I can’t think of a reason why this is necessary though. I would consider it bad design for a type that expects to be initialized with an Int variable to require its callers to use the label, as opposed to providing an alternate initializer that doesn’t have a label with “literal” in its name. Are there specific use cases where you think this capability is important or is it just the principle that you should be able to call any initializer you write?
Partly that principle, but partly the ability to forward literal-initialization. You can forward literal-initialization to, say, a BuiltinIntegerLiteralConvertible type by just appointing it your associated IntegerLiteralType, but if you're wrapping another type, that doesn't work. For example:
What is the advantage of calling the literal JSONValue initializer here rather than a non-literal initializer that accepts JSONValue.IntegerLiteralType? Is there something in the implementation that makes this more efficient?
If forwarding is the intended use for explicit calls, wouldn’t the `@literal` attribute attached to the type be an alternative way to address the forwarding use case?
Are there any non-forwarding use cases for explicit calls?
If forwarding is the only intended use case of explicit calls and `@literal` could cover that use case then the question (in my mind) is which principle do we hold more sacred? The ability to call any initializer we write or the syntactic consistency that external argument labels are always required at the call site? I admit this is partly an aesthetic concern…