Draco-lang / Language-suggestions

Collecting ideas for a new .NET language that could replace C#
75 stars 5 forks source link

Colon as operator for tuples #22

Open WhiteBlackGoose opened 2 years ago

WhiteBlackGoose commented 2 years ago

While we all can probably agree that tuples will be created as (a, b) or (a, b, c) etc., I suggest (as a crazy idea) operator : for creating tuples.

Definition

Type

In fact it's neither ValueTuple nor Tuple. It's ColonTuple. It's implicitly convertable from/to ValueTuple and Tuple though. And it's immutable.

The reason I want it to be a different type is because in many cases it makes sense to use what was created by colon, but not by regular tuple (for example sequences and slicing, see below).

ColonTuple<T1, T2>

ColonTuple<int, int> impl IEnumerable<int>

ColonTuple<float, float> impl IEnumerable<float>

ColonTuple<double, double> impl IEnumerable<double>

ColonTuple<T1, T2, T3>

ColonTuple<int, int, int> impl IEnumerable<int>

ColonTuple<float, float, float> impl IEnumerable<float>

ColonTuple<double, double, double> impl IEnumerable<double>

ColonTuple<T1, T2, T3, T4>

ColonTuple<T1, T2, T3, T4, T5>

ColonTuple<T1, T2, T3, T4, T5, T6>

Syntax

E. g.

// (1, 2)
val tup = 1 : 2 

// (1, 2, 3)
val tup = 1 : 2 : 3

// ((1, 2), 3)
val tup = (1 : 2) : 3

// (1, unit)
val tup = (1 : )

// (unit, unit)
val tup = ( : )

// (unit, 1, unit)
val tup = (:1:)

It's low priority, so e. g. 1 * 2 : 3 would be (1 * 2, 3) and 1 : 3.Method() would be (1, 3.Method()).

But it's higher priority than comman, so Method(1 : 3, 5) would be Method((1, 3), 5)

What problems can be solved

DSL

This definitely helps with writing GUI, css, json, and probably many more. It'd create great opportunities for whatever looks like range or map. E. g.

css [
    "margin": "5pt"
    "top": "100px"
]

Similarly,

json [
    "property": [
        "aaa": 5
    ]
]

Dictionaries

They can be created from a sequence of tuples of TKey and TValue, so

val dict = [
    "Peter": "LPeter1997"
    "WhiteBlackGoose": "WhiteBlackGoose"
]

As sequences

Numeric colon types can be interpreted as sequences!

val squares1to100 = (1 : 10) map (x -> x ** 2)
val evenSquares1to100 = (1 : 2 : 100) map (x -> x ** 2)
for i in (1 : 3 : 10) do
    print i

As ranges for slicing

We can create overloads for BCL types by adding indexers for ColonTuple:

val list = [1, 2, 3, 4, 5, 6, 7][0 : 2 : 6][2 : 3]

(what those numbers mean in particular to indexers is yet to decide)

yamin8000 commented 2 years ago

If we're going to define explicit types using colon then using colons for separating tuple members is going to be confusing.

Consider this:

val greeting : String = "hello there"
val greetings : String[] = "hello there" : "general kenobi"
//or even worse for custom classes
val myCars : Vehicles = GMC() : AUDI()
//or enums
val results : Result = Finished : Started : Working

It's very easy to miss the equal sign and more importantly, I think naturally colon (:) as punctuation is used for describing something, iterating a list, quoting someone in other words it's always about describing equal of something in other ways not as a separator, for example: See that!? I just used colon. here's a list of colors: red, green, blue, etc. GNU: GNU Not Unix Even the JSON example that you provided is using a colon for defining the relationship between key and value not for showing the relationship between different members of a group.

WhiteBlackGoose commented 2 years ago

Yeah, I agree. We're also considering #28 to solve this.

here's a list of colors: red, green, blue, etc.

But note, that what you said is actually a tuple in one of the interpretations. ("colors", [red, green, blue]). With colon it is much more readable, I think, than listOfColors(red, green, blue) in the mentioned cases.