Live data from Hacker News

What Is Type-Level Programming?

blog.sulami.xyz

51–60 of 96 posts

Re: What Is Type-Level Programming?

#51
The author didn't show any type-level programming here.

The author called into library which may or may not use type-level programming.

Pin doesn't have set_high()? I don't see any type-level stuff here.

If it had been:

    Pin
on the other hand...

Re: What Is Type-Level Programming?

#52

As I don't know Rust, honest question here, just to help me understand.. The `pins.d13.into_input();` part just returns an object/reference/... of a type that only has input-specific methods, while using `.into_output()` would do the same with only output-specific methods, correct? That's nice, but why couldn't you do the same thing in C++? With a dependent type you could do `pins.d13.into(INPUT)` and get the input-s…

One unique aspect of Rust that's involved here is the ownership system. It's possible to create a library that will only ever allow one instance of `pins.d13` to exist, and once you call `into_input` that `d13` value gets consumed, so that you cannot use the `Pin, PB5>` value anymore, and is guaranteed to only have access to that pin via the `Pin` value.

In C++, there's no possibility of guaranteeing that a consumed value is not re-used in the same ergonomic way using the type system. I'd imagine you can still implement something like this in C++ with some kind of move semantics and asserts, but it will be a run time error and not a statically checked error.

Re: What Is Type-Level Programming?

#53
post #42

Earlier quoted context omitted.

> As long as your final handle has the correct type Come on. Ensuring that is the whole game!

Yeah, and it's trivial in C++, what's your point? The example looks like it's been custom tailored to show how awesome Rust is, but then the post fails to even make the point (however contrived). Why the hell would you want to change the direction of a pin anyway? Just declare it as Pin or Pin .

There's no way in C++ to _statically_ guarantee that you don't simultaneously have the floating pin handle and the output pin handle. You cannot do it.

Re: What Is Type-Level Programming?

#54
post #51

The author didn't show any type-level programming here. The author called into library which may or may not use type-level programming. Pin doesn't have set_high() ? I don't see any type-level stuff here. If it had been: Pin on the other hand...

There's a small difference between type-level programming and type-driven programming, Idris does the latter while Rust the former (at least in my opinion). The library uses types to make illegal states impossible and therefore should qualify as type-level programming.

Re: What Is Type-Level Programming?

#55
post #53
post #42

Earlier quoted context omitted.

Yeah, and it's trivial in C++, what's your point? The example looks like it's been custom tailored to show how awesome Rust is, but then the post fails to even make the point (however contrived). Why the hell would you want to change the direction of a pin anyway? Just declare it as Pin or Pin .

There's no way in C++ to _statically_ guarantee that you don't simultaneously have the floating pin handle and the output pin handle. You cannot do it.

I agree, I'm just waiting for someone to explain why I should care; what's the reason to not declare the pin statically as either or?

I mean, I could easily come up with a contrived example that Rust can't do as well, but what's the point?

Re: What Is Type-Level Programming?

#56
post #26

Earlier quoted context omitted.

It could, but the comparison to Arduino still makes more sense since that's very well known and is pretty much C. It makes very little use of C++ features. Especially advanced stuff like this - probably partly to avoid difficult error messages for beginners and partly because the Arduino developers (the original ones at least) were shit at API design.

Yeah, but then the example is anyway flawed, type level programming is also a thing in C++.

Sorry would that be any C++, modern C++ (as described 10 years ago), modern C++ (as described 5 years ago), modern C++ (as described today), a special C++ subset known to MCU folks or something else?

Re: What Is Type-Level Programming?

#57
post #55
post #53

Earlier quoted context omitted.

There's no way in C++ to _statically_ guarantee that you don't simultaneously have the floating pin handle and the output pin handle. You cannot do it.

I agree, I'm just waiting for someone to explain why I should care; what's the reason to not declare the pin statically as either or? I mean, I could easily come up with a contrived example that Rust can't do as well, but what's the point?

> what's the reason to not declare the pin statically as either or?

That for plenty uses cases, pins are not statically either or, but are switched at runtime. (and the article really fails IMHO by not showing that, because doing that while keeping compile-time checks is the hard part, and its limits would be really interesting)

Re: What Is Type-Level Programming?

#58
post #26

Earlier quoted context omitted.

Yeah, but then the example is anyway flawed, type level programming is also a thing in C++.

Sorry would that be any C++, modern C++ (as described 10 years ago), modern C++ (as described 5 years ago), modern C++ (as described today), a special C++ subset known to MCU folks or something else?

Sarcasm ignored.

Type level programming is possible in C++98.

Easy to reach for in most embedded compilers, when people actually use C++ compilers for C++, and not the C subset.

Re: What Is Type-Level Programming?

#59
post #12
post #9

Earlier quoted context omitted.

It’s not a contrived example. It’s the canonical embedded hello world, written against two different libraries. At any rate, the C++/Rust part can be a distraction, because the interesting point here is the technique of encoding program state in the type system.

>"At any rate, the C++/Rust part can be a distraction" that was exactly my point. >"interesting point here is the technique of encoding program state in the type system." It is "interesting" but there is nothing new about it.

Where was it claimed to be "new"? That's a different complaint than you had before... not sure how it's relevant to languages in the example.

Re: What Is Type-Level Programming?

#60
post #53
post #42

Earlier quoted context omitted.

Yeah, and it's trivial in C++, what's your point? The example looks like it's been custom tailored to show how awesome Rust is, but then the post fails to even make the point (however contrived). Why the hell would you want to change the direction of a pin anyway? Just declare it as Pin or Pin .

There's no way in C++ to _statically_ guarantee that you don't simultaneously have the floating pin handle and the output pin handle. You cannot do it.

You can do that by creating types for the pins, and using HKT via template metaprogramming when instantiating the classes that hold handles to the pins.

It the implementation code pretty?

Most likely not, but here we are discussing what is possible, not beauty.

Post reply on HN