Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Reading a file to Text #139

Closed
michalrus opened this issue Sep 25, 2017 · 7 comments

Comments

Projects
None yet
2 participants
@michalrus
Copy link

commented Sep 25, 2017

Hey!

How can I read a file into a : Text value? Like let text = builtins.readFile ./some-file.txt in Nix.

I could wrap it in ''s and then do a standard import, but it’s not exactly convenient. 😛

Thanks!

@Gabriel439

This comment has been minimized.

Copy link
Collaborator

commented Sep 25, 2017

Like this: ./some-file.txt as Text

@michalrus

This comment has been minimized.

Copy link
Author

commented Sep 25, 2017

Incredible! 💚 Thanks so much, I didn’t think this would be possible.

Maybe it should be added to Dhall.Tutorial? I can’t see any as mentioned there. =)

@Gabriel439

This comment has been minimized.

Copy link
Collaborator

commented Sep 25, 2017

Oops! For some reason I thought that the tutorial did mention this feature, but I now see that it's not there. I'll update the documentation

@michalrus

This comment has been minimized.

Copy link
Author

commented Sep 25, 2017

One last question: can I do substitution/templating on such Text in pure Dhall easily? Or am I left with Haskell later? For example:

  • hello.txt:
    Hello, {name}!
    
  • config.dhall:
    let helloRaw = ./hello.txt as Text in
    {
      hello = Text/replace "{name}" "Gabriel439" helloRaw
    }
    

Normally, if this Text was inlined, I’d just use ${name}, but here?

@Gabriel439

This comment has been minimized.

Copy link
Collaborator

commented Sep 25, 2017

The best you can do in Dhall is to go back to hello.txt being Dhall code, like this:

-- hello.dhall

λ(name : Text)  ''
    Hello, ${name}!
''

... and then in config.dhall you do:

{ hello = ./hello.dhall "Gabriel439"
}

... or if you want to pass named arguments to hello.dhall then you can pass a record like this:

-- hello.dhall

λ(args : { name : Text })  ''
    Hello, ${args.name}!
''

... and then reference that like this:

{ hello = ./hello.dhall { name = "Gabriel439" }
}

However, you can't retroactively modify a value of type Text. The only thing you can actually do with Text values in Dhall is concatenate them, but there is no way to inspect, slice, compare, or otherwise modify them in Dhall. Other than concatenation, Text values are opaque values (like Integer)

In fact, the string templating syntax in Dhall is syntactic sugar for Text concatenation. This Dhall expression:

λ(name : Text)  ''
    Hello, ${name}!
''

... is the same as this Dhall expression:

λ(name : Text)  "Hello, " ++ name ++ "!"
@michalrus

This comment has been minimized.

Copy link
Author

commented Sep 25, 2017

Thank you for this detailed answer. 😻 Amazing support! 💙

Lack of retroactive modification makes a lot of sense (do $x well from the beginning), but still I had this slight hope. 😅 But then, after a while of initial happiness, I wouldn’t like having the possibility of defining Text/replace in a configuration language.

Thank you!

@michalrus michalrus closed this Sep 25, 2017

@Gabriel439

This comment has been minimized.

Copy link
Collaborator

commented Sep 25, 2017

You're welcome! Also, I will reopen this issue to remind myself to fix the documentation to mention support for as Text

@Gabriel439 Gabriel439 reopened this Sep 25, 2017

Gabriel439 added a commit that referenced this issue Sep 25, 2017

@bosu bosu closed this in #140 Sep 26, 2017

bosu added a commit that referenced this issue Sep 26, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.