icerpc / slicec

The Slice compiler library
Apache License 2.0
13 stars 5 forks source link

Change capitalization for Sequence, Dictionary and Result #644

Closed bernardnormier closed 1 year ago

bernardnormier commented 1 year ago

We currently spell sequence, dictionary and the proposed result in lowercase, like most other keywords.

I propose to use instead Pascal case for these keywords: Sequence, Dictionary and Result.

Rationale

In many languages, including Slice, type names--including generic type names--are Pascal case. Sequence and Dictionary can be used as types, and syntactically Result is used as a return type. Pascal case is the expected capitalization for this usage in Slice.

Note that in Ice, sequence and dictionary could not be used as types. They were more like struct, with:

// Old slice

struct Point { int x; int y; }

sequence<Point> PointSeq;   // sequence is used as "struct" here, to build a new constructed type
InsertCreativityHere commented 1 year ago

I'm fine with any kind of capitalization, we should just pick one and be consistent about it.

Buttt, to play devil's advocate, I've always felt the rationale is that keywords should be lowercase and constructed types should be PascalCase. It just so happens that in many languages dictionary and sequence types are backed by constructed types: Sequence, HashMap, IDictionary, Vec, IList, etc. So I'd bet good money that if C# were to add a keyword version for List, it would be lowercase.

Take Java for example, which has both:


Or far interestingly, Rust. One of the core types in Rust is Box. It's used to store data on the heap. In old Rust, it was so special that it was given a keyword:

let x = ...;
let boxed = box x;

But after some time, they changed their mind and made Box a normal constructed type like anything else:

let x = ...;
let boxed = Box::new(x);

When it was a keyword, they used lowercase, when they made it a constructed type they used PascalCase. The issue where they phased out the old keyword version: https://github.com/rust-lang/rust/issues/49733

bernardnormier commented 1 year ago

While it's true that keywords are usually lowercase, it's not a strict rule. For keywords used as special type names, it's arguably common to see them in Pascal case.

Take Self in Rust, or Any, Protocol, Self and Type in Swift.

With your example, the old box didn't use a normal type syntax. We use the "normal" type reference syntax for Dictionary etc. (putting aside the non-standard <...>).

Another advantage of changing the spelling of these keywords is you can more easily use them as parameter names and field names, per the usual Slice naming conventions:

op(sequence: Sequence<int32>) // doesn't work without escaping if sequence is keyword

Maybe they could be soft keywords (https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords) but this sounds overkill. It's easy to escape keywords.

InsertCreativityHere commented 1 year ago

This was implemented in a3f3463175d9873681a319d22aa91248c31a06a7