Live data from Hacker News

What Is Type-Level Programming?

blog.sulami.xyz

31–40 of 96 posts

Re: What Is Type-Level Programming?

#31
post #8

Correct me if I'm wrong, but I see nothing in the Rust example that couldn't just as well be implemented in C++. A comparison to C would have made more sense.

The article compares types-assisted programming to ‶raw″ programming.

It dumps on the Arduino stdlib, not C++ itself. The example is probably in Rust simply because that's what the author is using.

Re: What Is Type-Level Programming?

#33
post #4

>"This looks fine, but what happens if you do not get the pin mode right, for any of many possible reasons?" This is such a contrived and pathetic example. None of it has anything to do with C++ or Rust. It was a decision of whomever wrote pin access libraries. In either of the languages mentioned there is absolutely no problem creating an interface that would return particular pin in "right" state ready to be operat…

> None of it has anything to do with C++ or Rust. It was a decision of whomever wrote pin access libraries.

Which is why the article title is ‶What is Type-level programming?″ and not ‶C++ suckz Rust r0x lol″

Re: What Is Type-Level Programming?

#34
post #8

Correct me if I'm wrong, but I see nothing in the Rust example that couldn't just as well be implemented in C++. A comparison to C would have made more sense.

I program MCUs in C++ as a hobby, and I'm not sure how I would do this in C++. Idiomatically I believe you could use move semantics to invalidate a "handle" to a pin when switching its type, but that wouldn't fail at compile-time. With ownership you really have something in your execution that "is" the pin by virtue of only one value of that pin existing at a time. See also https://docs.google.com/document/d/e/2PACX-…

As long as your final handle has the correct type, I see nothing mentioned in the article than couldn't be done just as easy in C++.

Ownership is a different beast, and definitely more manual/error prone in other languages; but you definitely pay a price for that.

Re: What Is Type-Level Programming?

#35
post #4

>"This looks fine, but what happens if you do not get the pin mode right, for any of many possible reasons?" This is such a contrived and pathetic example. None of it has anything to do with C++ or Rust. It was a decision of whomever wrote pin access libraries. In either of the languages mentioned there is absolutely no problem creating an interface that would return particular pin in "right" state ready to be operat…

Ok, that was harsh, but I was excited and wanted to get to the meat of the article but then it just ended. I was thinking, "that's not how I would design a C++ interface". I still don't understand what these Rust types are good for, but I want to know. I miss how in Ada you can define which values an int can have. Can you do that in Rust?

You can do with a sum type (enum) that is represented by a u32 (repr(u32)), or converts into one. Though I don’t know Ada so can’t speak to how accurately I’m creating the analogy.

Re: What Is Type-Level Programming?

#36
post #4

>"This looks fine, but what happens if you do not get the pin mode right, for any of many possible reasons?" This is such a contrived and pathetic example. None of it has anything to do with C++ or Rust. It was a decision of whomever wrote pin access libraries. In either of the languages mentioned there is absolutely no problem creating an interface that would return particular pin in "right" state ready to be operat…

In C++ (or another language of your choice without strict linear types) how would you design a pin library to prevent someone from using a pin as both input and output enforced at compile time ?

As long as it is static, that is, it doesn't change during execution; you would do it by declaring the pin as either or using the type system.

Re: What Is Type-Level Programming?

#37
post #34

Earlier quoted context omitted.

I program MCUs in C++ as a hobby, and I'm not sure how I would do this in C++. Idiomatically I believe you could use move semantics to invalidate a "handle" to a pin when switching its type, but that wouldn't fail at compile-time. With ownership you really have something in your execution that "is" the pin by virtue of only one value of that pin existing at a time. See also https://docs.google.com/document/d/e/2PACX-…

As long as your final handle has the correct type, I see nothing mentioned in the article than couldn't be done just as easy in C++. Ownership is a different beast, and definitely more manual/error prone in other languages; but you definitely pay a price for that.

> As long as your final handle has the correct type

Come on. Ensuring that is the whole game!

Re: What Is Type-Level Programming?

#38
The example ignores the fact that pinmode is not just an initialization kind of method that you need to call once, but that there examples where you have to use it in the 'loop'. The most common is the bidirectional serial communication over a single wire, as is used with the DHT22 digital temperature and humidity sensor.

Re: What Is Type-Level Programming?

#39
post #27

"In C++, if I do: import numpy as np np.linspace(2.0, 3.0, num=5) nothing happens. In fact, it's a compile error. But if I do the same in Python, it works and works very well! Python > C++" Sorry for the troll post, but this really is ridiculous.

I normally appreciate a good troll post, but I don't even understand what you are getting at here.. :/

Re: What Is Type-Level Programming?

#40
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-specific type, but that seems to not be something Rust could do?

Post reply on HN