How do you represent a newline in a String?

1,478 views
Skip to first unread message

William Sewell

unread,
Nov 28, 2014, 1:59:26 PM11/28/14
to elm-d...@googlegroups.com
I want to write a "toString" function for one of my types. It is a record, and I want to write the fields on separate lines. I tried appending the string "\n", but this appears as "\n" when displayed, instead of an actual new line.

In the standard library, I couldn't find any function that would give me this character either.

Is there a way to do this? Thanks.

Max Goldstein

unread,
Nov 28, 2014, 3:23:06 PM11/28/14
to elm-d...@googlegroups.com
You're looking for '\n' in single quotes, which is a Char.

The String library doesn't provide an obvious way to do this, and maybe it should. You can try

String.join (String.fromList ['\n']) [my, strings, from, the, record]

This still prints with literal \n in the REPL and with asText, but if you use Text or (presumably) send it out of a port, it should have newlines.

Will Sewell

unread,
Nov 29, 2014, 6:33:27 AM11/29/14
to elm-d...@googlegroups.com
Thank you. Judging by what you said I'm assuming this isn't possible, but is there any way to have the newlines appears as newlines in Debug.watch output? Currently they show as "\n".

--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/3BHRox_Ph8A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Goldstein

unread,
Nov 29, 2014, 4:21:05 PM11/29/14
to elm-d...@googlegroups.com
I would guess "no" as well, though I don't know for sure.

Evan Czaplicki

unread,
Nov 29, 2014, 4:46:18 PM11/29/14
to elm-d...@googlegroups.com
I think it's not possible at the moment. Here are a few approaches I'd like to allow that'd help:
  • Have a version of Debug.watch that displays Text
  • Have a version of Debug.watch that displays Element
  • Have a version of Debug.watch that is able to show ints and floats and pairs thereof as graphs
The first one would cover you. Not sure when it'll be feasible to work on that stuff, but I think it'd be pretty great :)

On Sat, Nov 29, 2014 at 1:21 PM, Max Goldstein <maxgol...@gmail.com> wrote:
I would guess "no" as well, though I don't know for sure.

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.

William Sewell

unread,
Nov 29, 2014, 7:56:19 PM11/29/14
to elm-d...@googlegroups.com
That would be very useful. In fact, I would love to help out if you think it wouldn't be too complex, and you could point me in the direction of where to get started.

Another thing that would be nice to add: I'm currently using John Mayer's elm-linear-algebra library. When I debug a record with a field of type Vec2, the value is printed as something like "internal data structure", presumably because it is native JS variable. It would be nice if the debugger also printed these native JS values.

Max Goldstein

unread,
Nov 30, 2014, 3:44:03 PM11/30/14
to elm-d...@googlegroups.com
Debug.log also prints with a literal \n, so that might be a thing to fix as well.

You're correct that "internal data structure" is the fallback of show but John's vector library just uses records, so that shouldn't happen. In 0.13 this excerpt works how you think it would:


type Vec2Ext a = { a | x : Float, y : Float }

type Vec2 = Vec2Ext {}

origin : Vec2
origin = { x = 0, y = 0 }

main = asText origin

If that doesn't work in master, it's a regression.

In general, show is implemented as a long else-if chain, so adding more cases involves a third-party library modifying the core. I'm going to start talking about that to avoid the perennial type class discussion.

William Sewell

unread,
Nov 30, 2014, 4:57:58 PM11/30/14
to elm-d...@googlegroups.com
I'm actually using his other vector library: https://github.com/johnpmayer/elm-linear-algebra

And it sounds like it would be nice if there was some way for third party library to add show instances in a clean way.

Max Goldstein

unread,
Nov 30, 2014, 5:11:03 PM11/30/14
to elm-d...@googlegroups.com
Ahh. That makes more sense.

One possibility would be for third-party libraries to register a handler with the core before execution begins. Right before the default case, "<internal structure>", the core calls all registered handlers and sees if any return a string, and if so use that. Problems: (1) the handlers must be very well-behaved and able to recognize values of the types their library defines, (2) order could possibly matter. The best way to handle it is probably last import, first handler, so you can always override a handler you don't like.

But that's probably left for another day, and possibly handled by some more general pattern already on the roadmap.

Evan Czaplicki

unread,
Nov 30, 2014, 6:49:15 PM11/30/14
to elm-d...@googlegroups.com
William, cool, this could be an awesome project :)

Looking at the type of Debug.watch it seems like the public API can stay exactly the same, which is awesome. So perhaps it is a matter of recognizing Elements and Text and showing them. There is already custom code for doing that, so it may be a matter of augmenting that a bit?

My major concerns would be around UI. If text is shown, how do we make sure it looks non-awful? If Elements are shown, do we scale them to fit in the sidebar? This sort of stuff. I think the technical part is reasonably straight forward though :)

--

Evan Czaplicki

unread,
Nov 30, 2014, 6:55:39 PM11/30/14
to elm-d...@googlegroups.com
I found some other leads, I'll start another thread for this.

William Sewell

unread,
Nov 30, 2014, 6:57:53 PM11/30/14
to elm-d...@googlegroups.com
Sounds intriguing. If you let me know of any ways I can help out, I'd be happy to look into it tomorrow :)
Reply all
Reply to author
Forward
0 new messages