r/programming • • Oct 26 '26

I'm Building My Own Programming Language

https://medv.io/blog/i-am-building-my-own-programming-language
0 Upvotes

11 comments sorted by

2

u/ketralnis Oct 26 '26 edited Oct 26 '26

This is cool:

func area(s: Shape): float64 {
  if s is Circle { 3.14 * s.radius * s.radius }
  else if s is Rect { s.width * s.height }
}

Inside the branch, s is a Circle [...]

So once a subtype has annotated you can just go on using it without a separate cast which feels very natural. But what is the type of s if I ask if s is Circle || s is Rect?

Also:

Notice there is no final else. The checker knows Circle and Rect are all the cases. If I add Triangle to Shape later, area stops compiling until I handle it

Exhaustiveness checking for enum/union types is fairly common (off of the top of my head, Rust in the C family and the entire ML family) but usually that comes in a separate match keyword or some other destructuring system because if can also be used in ways like

print "You win!"
if score > 100 { print "and you're super great!" }

where most of the time you don't need it. So do all of your ifs require exhaustiveness? (Looking at your other examples no.) Or just if I happen to inquire about a type, and now it's not a normal if but some other if-looking thing that's semantically unrelated but syntactically identical? Is if s is Circle || is_sky_blue() one of these type-destructuring exhaustive ifs? Since there are two separate if systems I think that context-sensitivity means that if I copy-paste some code it could have different rules in the new place than it did in the old place, right?

Along similar lines:

  var account = accounts[id] ?? return Result.err(NotFound{ id })

[...] Below that line, account is just an Account

I guess that is syntax sugar for something like

var account = accounts[id] // account is Account|null
if account is null {
    return Result.err(NotFound{ id })
}
// the type of account has changed(!) to Account

Which is very surprising, that inserting an if statement can change the type for the rest of the scope. Even if it's not and it's instead sugar for

var account = accounts[id] // account is Account|null
if account is null {
    return Result.err(NotFound{ id })
} else {
    // the type of account has changed(!) to Account
    ... the rest of the function goes here
}

then it still means that changing earlier parts of a function has surprising impacts on the later parts, and also raises questions about what if the type of account is SystemAccount | UserAccount | null, that this sort of check doesn't scale to?

I'd really recommend spending some time looking at other programming languages beyond Typescript and Go since this is clearly inspired by those but the rest of the PL world has taken these and other ideas much further and with more rigour. e.g. the "no methods" and "no coloured functions" sections describe problems nearly exclusive to Javascript. Not making those specific choices isn't an advantage, it's just not making the specific mistakes that Javascript and nobody else makes

1

u/Elfet Oct 26 '26

Thanks for a review!

I save the match keyword for later if i decide to add pattern matching.

 In the example with if and union, it works because the return type of the function is string. If a new union member is added, the if type will be string | void which can’t be assigned to string.

Other examples with if and early return is one of my favorite features of TS: narrowing and control flow analysis. 

If type cannot be narrowed because of ||, type stays full.

1

u/Elfet Oct 26 '26

  what if the type of  account  is  SystemAccount | UserAccount | null ,

In this case only null is stripped away. This is control flow analysis.

2

u/WriteCodeBroh Oct 26 '26 edited Oct 26 '26

“Inserting an if statement can change the type for the rest of the scope” sounds like type narrowing in TS.

let account: SystemAccount | UserAccount | null;

account = accounts[id];
if (account === null) {
return Result.err(NotFound(id));
}
// compiler treats account as type SystemAccount | UserAccount now, null narrowed

1

u/Deranged40 Oct 26 '26

But what is the type of s if I ask if s is Circle || s is Rect?

As best I understand the example given, the type of s is Shape. It would therefore stand to reason to my object-oriented mind that Circle and Rect both extend Shape.

At least, that's how this would work in C#. I could definitely have a List<Shape>, and add to that list a Circle, a Rect, a Trapezoid if i wanted to, etc.

1

u/ketralnis Oct 26 '26

Sure, but now you have an if statement that sometimes does type voodoo, and sometimes does exhaustiveness checking, and other times does not

2

u/Deranged40 Oct 26 '26 edited Oct 26 '26

now you have an if statement that sometimes does type voodoo, and sometimes does exhaustiveness checking, and other times does not

Absolutely you do. Is that a good practice? That's debatable for sure. But this functionality is fully supported in C# and Typescript at the very least. Should in theory be supported by any OOP language.

Personally, I'd prefer shape to be either an abstract class or an interface, and it'd require implementing a CalculateArea() method or something. That way it doesn't matter if it's a Circle or a Rect, it's a shape, and it has an area (which, in theory, could be zero), and I don't have to put any branching logic in for what type of shape each item in a collection is.

But again, this is the difference between what is capable in a language vs what is a "good practice".

→ More replies (1)

1

u/devraj7 Oct 26 '26 edited Oct 26 '26

Which is very surprising, that inserting an if statement can change the type for the rest of the scope.

This is called flow typing and has existed for 20+ years (e.g. Groovy and Kotlin have it, even Java adopted it recently).

2

u/badpotato Oct 28 '26

I guess as long as you don't license the ant programming language as apache, it's alright.

1

u/Primary_Ads Oct 26 '26 edited Oct 26 '26

hope you can find users but i find nothing interesting about your proposed language. it reads like someone who used exclusively javascript and go and nothing else. maybe the experience on expr doesnt translate well to PL work, idk.