[swift-evolution] Ad hoc enums / options

L Mihalkovic laurent.mihalkovic at gmail.com
Thu Jun 2 00:35:21 CDT 2016


> On Jun 1, 2016, at 11:20 PM, Vladimir.S <svabox at gmail.com> wrote:
> 
> OK, I understand most of your points and feel the same regarding the proposed feature... but:
> 
> > .. the compiler does not like when we define the same thing twice, and thse short form amount to doing what he does not let us do. ..
> > .. if we use an annotation that lets it know that "it is not a mistake..
> > I really dont want to write that enum myself, even though I am using the
> > same abbreviation twice". Otherwise, the compiler would let us know that
> > the second time could be a mistake because there is already something
> > with the same name...
> 
> I just wanted to understand how such enums are different than tuples in meaning of "I don't want to write the struct myself", "Yes, I'm using the same tuple twice", "Compiler will let us know that we used the same tuple again and probably mistake as has already the same". (Not about differences in implementation/representation on compiler level, but on level of one who is writting the code)
> 
> Should we use the same arguments to propose the special annotation when we have the same tuple more than once? No. So I believe there should be no such limitation and special annotation for proposed enums.
> 

I am not sure the parallel can be made. A tuple is characterized by its structure, BUT the scope of its use is the VARIABLE where it is being used (I am simplifying by only referring to var/let cases). So from the compiler’s point of view, there is not a giant Tuples hash table where it can look for a given tuple definition based on its intrinsic name. So even if you repeat the same tuple definition every other line in your source code, the compiler never thinks that they are the same type because they ARE NOT the same. Each one is a different variable (the compiler would complain that the variable is defined multiple times though). To simplify the model, you could almost think that a tuple is a degenerate struct that is internally given the name of its usage site. It is not quite true but it is a close-enough approximation of what it really is.

The only rational for allowing a @I_know_what_I_am_doing_so_please_dont_complain annotation is to allow (and I am not saying it should be allowed)


func scaleAndCropLargeImage(
    image: UIImage,
    toSize size: CGSize,
    operation: (.Fit | .Fill) = .Fit
    ) -> UIImage {
  whoCaresThereIsReallyASingleTypeOfImagesAnyhow()
}

func scaleAndCropSmallImage(
    image: UIImage,
    toSize size: CGSize,
    operation: (.Fit | .Fill) = .Fit
    ) -> UIImage {
  whoCaresThereIsReallyASingleTypeOfImagesAnyhow()
}


private 
func whoCaresThereIsReallyASingleTypeOfImagesAnyhow(
    image: UIImage,
    toSize size: CGSize,
    operation: (.Fit | .Fill) = .Fit
    ) -> UIImage {

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160602/cea0c45b/attachment.html>


More information about the swift-evolution mailing list