Live data from Hacker News

What Is Type-Level Programming?

blog.sulami.xyz

61–70 of 96 posts

Re: What Is Type-Level Programming?

#61
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 .

> Yeah, and it's trivial in C++, what's your point?

I see lots of assertions of that fact, but no examples. Care to link some actual code?

> Why the hell would you want to change the direction of a pin anyway? Just declare it as Pin or Pin.

i2c uses the same pin for sending and receiving and changes the mode of the pin for the purposes of communication with peripherals.

Re: What Is Type-Level Programming?

#62
post #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.

But there's no programming at the type level. The programming in the article is at the level of values (insofar as what is shown.).

Re: What Is Type-Level Programming?

#63
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++.

C++ inherits from C the flaw that it is as Dennis Ritchie put it "Strongly typed but weakly checked".

In this case that hurts really badly because it means your C++ compiler is likely to let you do stuff that's nonsense, because checking isn't part of the job description, if you claim this Duck is a Goose the C++ compiler just rolls its eyes, sure whatever, however rustc says that is a Duck, you said it's a Goose but it is not, that's an error.

That checking means a hard left shift for such bug finding, which can mean a substantial direct financial saving on QA or an improvement in feature velocity.

Re: What Is Type-Level Programming?

#64
post #35

Earlier quoted context omitted.

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.

In Ada/Pascal/Modula-2/Modula-3/.... it would be something like,

    type
      Subrange = 56..100;
      Colors = (Red, Green, Blue);
      Pixel = array [Colors] of byte; 

With the plus that it is a compiler error if an invalid value is assigned, and can be validated at compile time (or it will be a range runtime error otherwise).

Re: What Is Type-Level Programming?

#65
post #50

Earlier quoted context omitted.

Exactly. In other words, the post could have just continued with C++ and shown a better way to do it in the same language.

And maybe, just maybe, the guys knows Rust better than C++ and just want to take an example from their daily work rather than take every single precaution not to hurt the fragile sensibilities of HN reader unable to see the point if their life depended on it?

Then they should have presented the case that way, instead of showing C like code and assuming there is no way to do that in C++.

Re: What Is Type-Level Programming?

#66
post #26

Earlier quoted context omitted.

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

C++ inherits from C the flaw that it is as Dennis Ritchie put it "Strongly typed but weakly checked". In this case that hurts really badly because it means your C++ compiler is likely to let you do stuff that's nonsense, because checking isn't part of the job description, if you claim this Duck is a Goose the C++ compiler just rolls its eyes, sure whatever, however rustc says that is a Duck, you said it's a Goose but…

Yeah, copy-paste compatibility is a blessing for adoption, and a curse for herding cats into better coding practices.

Same can be told about TypeScript, Groovy or any other language that follows the same adoption model.

Re: What Is Type-Level Programming?

#67
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 .

> Yeah, and it's trivial in C++, what's your point? I see lots of assertions of that fact, but no examples. Care to link some actual code? > Why the hell would you want to change the direction of a pin anyway? Just declare it as Pin or Pin . i2c uses the same pin for sending and receiving and changes the mode of the pin for the purposes of communication with peripherals.

Ignoring the fact that nobody is bit bashing I2C (there are other protocols that you would bit bash), you don't normally change the mode in that case either. You set it to an output with drive-low and float-high. It's essentially an InputOutput mode.

Re: What Is Type-Level Programming?

#68
post #65
post #50

Earlier quoted context omitted.

And maybe, just maybe, the guys knows Rust better than C++ and just want to take an example from their daily work rather than take every single precaution not to hurt the fragile sensibilities of HN reader unable to see the point if their life depended on it?

Then they should have presented the case that way, instead of showing C like code and assuming there is no way to do that in C++.

> Then they should have presented the case that way

That's exactly what they did.

> assuming there is no way to do that in C++

You are extrapolating things that are nowhere to be found in the article.

Re: What Is Type-Level Programming?

#69
post #68
post #65

Earlier quoted context omitted.

Then they should have presented the case that way, instead of showing C like code and assuming there is no way to do that in C++.

> Then they should have presented the case that way That's exactly what they did. > assuming there is no way to do that in C++ You are extrapolating things that are nowhere to be found in the article.

C like code was presented as C++, while Rust code using an existing library was presented as something not available in C++ (type level programming).

Re: What Is Type-Level Programming?

#70

Earlier quoted context omitted.

> Yeah, and it's trivial in C++, what's your point? I see lots of assertions of that fact, but no examples. Care to link some actual code? > Why the hell would you want to change the direction of a pin anyway? Just declare it as Pin or Pin . i2c uses the same pin for sending and receiving and changes the mode of the pin for the purposes of communication with peripherals.

Ignoring the fact that nobody is bit bashing I2C (there are other protocols that you would bit bash), you don't normally change the mode in that case either. You set it to an output with drive-low and float-high. It's essentially an InputOutput mode.

I'm sure you mean "nobody" not nobody, otherwise this doesn't exist: https://github.com/bitbank2/BitBang_I2C (and plenty of others, this was just the first github link google gave me). There's quite a few articles suggesting to bit bang i2c on tiny MCUs also for various memory/code size/etc reasons.

I suspect trivial to do in C++ really should be "trivial" too.

Post reply on HN