Start of variadic printf post
This commit is contained in:
parent
109165b80e
commit
bdceab7652
4 changed files with 67 additions and 0 deletions
|
@ -18,6 +18,7 @@ authors = "Nathan McCarty"
|
|||
-- modules to install
|
||||
modules = Idris
|
||||
, Posts.HelloWorld
|
||||
, LessMacrosMoreTypes.Printf
|
||||
|
||||
-- main file (i.e. file to load at REPL)
|
||||
-- main =
|
||||
|
|
45
projects/Idris/src/LessMacrosMoreTypes/Printf.md
Normal file
45
projects/Idris/src/LessMacrosMoreTypes/Printf.md
Normal file
|
@ -0,0 +1,45 @@
|
|||
# Type Safe Variadic printf
|
||||
|
||||
```idris hide
|
||||
module LessMacrosMoreTypes.Printf
|
||||
|
||||
%default total
|
||||
```
|
||||
|
||||
While C can provide convenient string formatting by having hideously memory
|
||||
unsafe variadics, and dynamic languages, like python, can do the same while
|
||||
being memory safe by not being type safe, many type safe languages, such as
|
||||
Rust, are forced to provide such functionality through the use of a macro.
|
||||
Dependently typed languages, like Idris, can provide a printf like formatting
|
||||
interface, while maintaining both memory and type saftey, without the need for
|
||||
the macro. We will explore this by implementing a simplified version of `printf`
|
||||
in Idris from scratch.
|
||||
|
||||
This article is inspired by an exercise from chapter 6 of [Type Driven
|
||||
Development with
|
||||
Idris](https://www.manning.com/books/type-driven-development-with-idris), and is
|
||||
written as a literate Idris file.
|
||||
|
||||
## Gameplan
|
||||
|
||||
Our goal is to provide a printf function that can be called, much like it's C equivlant, like so:
|
||||
|
||||
> [!NOTE]
|
||||
> As this is a literate Idris document, and we haven't defined our `printf`
|
||||
> function yet, we have to use a `failing` block to ask the compiler to check
|
||||
> that this code parses, and syntax highlight it for us, but not attempt to
|
||||
> actually compile it.
|
||||
|
||||
```idris
|
||||
failing
|
||||
example_usage : String
|
||||
example_usage = printf "%s %d %02d" "hello" 1 2
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Parsing a Format String
|
||||
|
||||
## Calculating a Type From a Format String
|
||||
|
||||
## printf
|
Loading…
Add table
Add a link
Reference in a new issue