Replies: 8 comments
-
Checkout how alanlang or haskell does this. |
Beta Was this translation helpful? Give feedback.
-
While we are discussing types, are there plans to support algebraic data types? |
Beta Was this translation helpful? Give feedback.
-
Absolutely, I believe that falls under the TypeScript area of inspiration |
Beta Was this translation helpful? Give feedback.
-
One thing to note with them is that apparently there's soundness in having them be open ended. |
Beta Was this translation helpful? Give feedback.
-
What's challenging here is that TypeScript offers an immensely flexible type system due to how practically everything is structurally evaluated. This is something I'd like to preserve the essence of but it's an interesting conceptual challenge. I would perhaps be in favour structs replacing type literals and having classes and structs coexist. struct Person {
name: string
}
class PersonCollection {
private _people: Person[]
constructor() {
this._people = People[]
}
public add(p Person): void { // move p to PersonCollection.add()
this._people.push(p) // move p to PersonCollection._people
}
}
interface IPersonCollection {
add(Person): void
} Where the compiler might be able to move values from one struct to another if the new struct contains at least as many values struct Cat {
name: string
color: string
}
struct Monkey {
name: string
color: string
height: int
}
const c: Cat = Cat{"Herbert", "Ginger"} // pointer to Cat{}
const m: Monkey = c // a new Monkey is initialized and the properties of cat are copied to it, c is deallocated |
Beta Was this translation helpful? Give feedback.
-
My biggest issue with this so far in my implementation is the ordering of fields in anonymous structs |
Beta Was this translation helpful? Give feedback.
-
Haha, perhaps the lexer will just complain if you don't order the struct properties alphabetically yourself. This will enforce property ordering as a styling convention, perhaps implemented later as part of the code formater |
Beta Was this translation helpful? Give feedback.
-
Not a bad idea, could give a warning or something but it doesn't fix the second example with extra properties before the expected ones |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a discussion for a longer term feature inclusion - just putting my thoughts here for discussion. The priority is building a compiler for a basic form of the project
Structs are useful types as they describe simple property collections. In JavaScript we have duck typed objects and in TypeScript we can describe those objects as type literals.
They are great for use cases like describing the values coming in from an external API or the properties of a constructors/method.
Classes are great for exposing functionality through methods within a container that has dependencies injected into it (via arguments - let's not talk about DI frameworks at this stage 😆). They are cumbersome to use to describe API responses.
To me, both are useful and I would like to include them.
Examples of usage
To extend this further, what if we simply allowed the ability to instantiate type definitions?
That way we could use type logic to programatically describe structs
Beta Was this translation helpful? Give feedback.
All reactions