bonobo-lang / bonobo

Strongly-typed, safe, opinionated systems language that compiles to C.
https://bonobo-lang.github.io
Apache License 2.0
14 stars 1 forks source link

Replace current Maps with Objects #52

Open thosakwe opened 6 years ago

thosakwe commented 6 years ago

An object literal makes Bonobo very expressive, and allows users to return typed data without having to instantiate a class.

At least for now, I'm hoping we can get away entirely without having to add classes. Especially when compiling to C or LLVM, that adds quite a bit of complication that frankly can and should be avoided.

But, we'll see.

Anyways, all keys should be simple identifiers. I see no advantage of having maps built in to the syntax itself. Those can be part of the standard library.

type MyRecord {name: String, size: Int32}

fn recordFromString(str: String): MyRecord {
  ret {name: str, size: str.length}
}

Note that though this is just an object of a user-defined, anonymous type, the analyzer can easily verify that it matches the type aliased to MyRecord.

thosakwe commented 6 years ago

Maps could just be part of the stdlib:

final map = Map<Int, Int>();
map[2] = 34;
thosakwe commented 6 years ago

An object literal can have three kinds of members:

Respectively:

{ foo: bar, baz, ...quux }
tejainece commented 6 years ago

We could still keep Map literals while still introducing Object initialization. We can decipher that it is object initialization from the context.

thosakwe commented 6 years ago

I’m not 100% sure that we could decipher the type from the context. In cases where return types are not present, it would be ambiguous whether an object is an object or a map.

I think we’ll be able to figure something out.

Do any languages other than Dart have Map literals?

My personal opinion is that the only types that should have literal representations are primitives, including arrays, and then object types so the language can be expressive, but also very clear.