Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec and implement structs in the parser #134

Closed
mohammadfawaz opened this issue Jul 26, 2023 · 3 comments
Closed

Spec and implement structs in the parser #134

mohammadfawaz opened this issue Jul 26, 2023 · 3 comments
Assignees
Labels

Comments

@mohammadfawaz
Copy link
Contributor

Basically named tuples really. Start with simple records. We can later evaluate if we need more complex features such as impl self.

@mohammadfawaz
Copy link
Contributor Author

mohammadfawaz commented Jul 28, 2023

From the design meeting, we agreed that named tuples + type aliases are the way to go. For example:

type MyType = { x: int, y: int, z: real };

would represent a struct with a name.

The only downside I can now think of is that the following two "structs" are basically the same type which may be undesirable:

type Price = { value: int };
type Weight = { value: int };

Note:
According to spec changes I made in #138, changing the field name is enough to change the type, but that's not something the user may want to rely on in this situation.

These two types do not have the same meaning and the user probably doesn't want implicit conversion between them. That is, we don't want the following to work:

let p: Price = { value: 5 }; 
let w: Weight;
constraint w == p; // or just `let w: Weight = p`

For enums, we ended up going with an enum keyword to avoid ambiguity with single variant enums. Should we go back and introduce the keyword struct as well to force type safety? as in:

struct Price = { value: int };
struct Weight = { value: int }; // Now this is entirely different from `Price`
enum Colour = R | G | B;

One final alternative here is to make type aliases actually different types, which is not the case in Rust. But that also mean someone could create two different aliases to int and have them be completely different types.

@otrho what are your thoughts here?

@otrho
Copy link
Contributor

otrho commented Jul 31, 2023

So we could consider type to be an alias or a 'newtype', the latter being a distinct type which does not unify with its RHS.

And then we can ask how useful is a type alias in a language without generics? 99% of the uses of typedef in C is for typedef struct _S { ... } S;. In languages with generics is usually to improve specificity so type Result = std::result::Result<Vec<(u64, bool)>, SpecialError<String, Blah = Bool>>;

I dont' think we need aliases and should just use 'newtypes' and then all of the above problems go away.

@mohammadfawaz
Copy link
Contributor Author

Closing in favour of #139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants