eclipse-archived / ceylon

The Ceylon compiler, language module, and command line tools
http://ceylon-lang.org
Apache License 2.0
395 stars 62 forks source link

replace 'smaller', 'larger', 'equal' with 'Comparison.smaller', 'Comparison.larger', 'Comparison.equal' #7409

Open gavinking opened 5 years ago

gavinking commented 5 years ago

I've always hated that the cases of Comparison (smaller, larger, equal) pollute the toplevel namespace of ceylon.language.

I could change the definition of Comparison to this:

"The result of a comparison between two [[Comparable]]
 objects: [[larger]], [[smaller]], or [[equal]]."
see (interface Comparable)
tagged("Comparisons")
shared class Comparison
        of larger | smaller | equal {

    shared actual String string;

    "The value is exactly equal to the given value."
    shared new equal {
        string => "equal";
    }

    "The value is smaller than the given value."
    shared new smaller {
        string => "smaller";
    }

    "The value is larger than the given value."
    shared new larger {
        string => "larger";
    }

    "The reversed value of this comparison."
    since("1.2.0")
    shared Comparison reversed
        => switch (this)
        case (equal) this
        case (larger) smaller
        case (smaller) larger;

}

So you would write Comparison.smaller, Comparison.larger, Comparison.equal.

This would be a breaking change, of course.

WDYT, folks?

xkr47 commented 5 years ago

Couldn't you still import them from inside the class?

import ceylon.language { Comparison { ... } }
xkr47 commented 5 years ago

Also, were you planning of keeping the toplevels as deprecated until say 1.5?

gavinking commented 5 years ago

Couldn't you still import them from inside the class?

Yes, of course.

gavinking commented 5 years ago

Also, were you planning of keeping the toplevels as deprecated until say 1.5?

I guess we could do that.

gavinking commented 5 years ago

Also, were you planning of keeping the toplevels as deprecated until say 1.5?

I guess we could do that.

Except 1.4 is the release that is breaking stuff. I would rather break everything cleanly at the same time....

xkr47 commented 5 years ago

I recall the renamed or similar annotation in some other issue.. It could be used here, if not to maintain compatibility then to help IDEs fix your code with minimum fuss.. Like an action in a Quick-Fix or similar popup:

`smaller` was renamed to `Comparison.smaller` - apply change
gavinking commented 5 years ago

Yes, there is an aliased annotation already.

xkr47 commented 5 years ago

:heart_eyes:

Would it propose importing Comparison and replace code with Comparison.smaller OR would import directly similar to the import statement in my earlier comment?

jvasileff commented 5 years ago

Not a big deal either way, but I like them being toplevels; less hassle and clutter with imports.

Is there any practical benefit to requiring the import?

What has really bugged me is the top-level function smallest(), which is ungrammatical for smaller(). If we're going to change things up, I think it would be worth exploring other words to resolve the overload, like lesser and greater.

someth2say commented 5 years ago

+1 I don't feel comparison to be a feature of the language itself, so I like comparison related objects having its own scope.

dlkw commented 5 years ago

In the case of smaller/equal/larger being in the language module, they did not yet interfere with my usage for them. But in general, I dislike things in the global namespace with general names that interfere with variable or method names I'd like to use. (Most painful occurrence is the name value, of course). So I'd tend to put as few things into the global namespace as possible.

@someth2say given the fact that ceylon defines its comparison operators by the Comparable interface, I'd indeed say they belong to the language. But nevertheless, I (slightly) tend to put them in the Comparison namespace.

gavinking commented 5 years ago

What has really bugged me is the top-level function smallest(), which is ungrammatical for smaller().

Right, well that's the thing, I always wanted to call that function smaller(). But I couldn't because that name was taken.

gavinking commented 5 years ago

@someth2say given the fact that ceylon defines its comparison operators by the Comparable interface, I'd indeed say they belong to the language. But nevertheless, I (slightly) tend to put them in the Comparison namespace.

Right, the general principle is that anything used to define the syntax of the language goes in ceylon.language. (Otherwise we would have split all metamodel stuff out into its own module a long time ago.)

gavinking commented 5 years ago

You can try this out on the 7409 branch.