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鈥檒l 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

michalrus 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鈥檚 not exactly convenient. 馃槢

Thanks!

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Sep 25, 2017

Collaborator

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

Collaborator

Gabriel439 commented Sep 25, 2017

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

@michalrus

This comment has been minimized.

Show comment
Hide comment
@michalrus

michalrus Sep 25, 2017

Incredible! 馃挌 Thanks so much, I didn鈥檛 think this would be possible.

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

michalrus commented Sep 25, 2017

Incredible! 馃挌 Thanks so much, I didn鈥檛 think this would be possible.

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

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Sep 25, 2017

Collaborator

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

Collaborator

Gabriel439 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.

Show comment
Hide comment
@michalrus

michalrus 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鈥檇 just use ${name}, but here?

michalrus 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鈥檇 just use ${name}, but here?

@Gabriel439

This comment has been minimized.

Show comment
Hide comment
@Gabriel439

Gabriel439 Sep 25, 2017

Collaborator

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 ++ "!"
Collaborator

Gabriel439 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.

Show comment
Hide comment
@michalrus

michalrus 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鈥檛 like having the possibility of defining Text/replace in a configuration language.

Thank you!

michalrus 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鈥檛 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.

Show comment
Hide comment
@Gabriel439

Gabriel439 Sep 25, 2017

Collaborator

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

Collaborator

Gabriel439 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment