Hasklig icon indicating copy to clipboard operation
Hasklig copied to clipboard

λ to indicate anonymous functions

Open ShalokShalom opened this issue 4 years ago • 13 comments

\ is meant to indicate a λ

I know this is a little bit of a stretch since it is one character, that is converted into a ligature and not two.

But I think it is worth thinking about this since it makes the language prettier and it makes the symbol more clear in its intention.

Thanks a lot to consider this ☺

ShalokShalom avatar Nov 27 '20 10:11 ShalokShalom

Might be difficult, since \ can occur in other places as well and those probably shouldn't be rendered as λ, but I'm not super familiar with how fonts like these works, so maybe it's doable.

anka-213 avatar Dec 28 '20 15:12 anka-213

Where does \ appear on its own without an additional character and does not mean an anonymous function?

ShalokShalom avatar Dec 29 '20 10:12 ShalokShalom

Mainly in strings. I don't know if that would count as "on its own", but this is an anonymous function

\n ->  n + 1

and this is a newline

"\n -> n + 1"

and I don't know if it is possible for a font to differentiate between these cases? Maybe we just ignore that case and make a ligature out of backslash followed by whitespace?

anka-213 avatar Dec 29 '20 12:12 anka-213

and I don't know if it is possible for a font to differentiate between these cases? Maybe we just ignore that case and make a ligature out of backslash followed by whitespace?

Sorry to jump in, but wouldn't it be incorrect? Lambdas' definitions will usually follow the \ with the variable name, right?

ivanopagano avatar Dec 29 '20 12:12 ivanopagano

Yes, most of the time people won't have a whitespace between \ and the variable name, so it would cripple the feature significantly. But it is the only way I can think of to differentiate between lambdas and string escapes without knowing more context. But maybe I'm overthinking this.

anka-213 avatar Dec 29 '20 12:12 anka-213

Yes, most of the time people won't have a whitespace between \ and the variable name, so it would cripple the feature significantly. But it is the only way I can think of to differentiate between lambdas and string escapes without knowing more context. But maybe I'm overthinking this.

No, I think your worries are reasonably founded.

ivanopagano avatar Dec 29 '20 13:12 ivanopagano

Well, the language can differentiate between strings and operators, while idk what Haskling can do.

Is it possible to make an exception for \n? Is this enough, covers it all edge cases?

That would only disallow the anonymous function with the variable name n, all other cases could be covered.

ShalokShalom avatar Dec 29 '20 15:12 ShalokShalom

I would like to add, that /n for a varible name that comes after a lambda, would be confusing.

\n kinda indicates a newline, if within a string or not, so this is a bit 'bad style' and is advised to be avoided anyway.

So if we can avoid \n for that reason, is there anything else that could be considered?

ShalokShalom avatar Dec 31 '20 20:12 ShalokShalom

I would like to add, that /n for a varible name that comes after a lambda, would be confusing.

\n kinda indicates a newline, if within a string or not, so this is kinda bad style.

No, I disagree. n is a common variable name, regardless of if it is used in a lambda or not. Furthermore, we would have to disqualify any name starting with n as well and that is starting to get ridiculous.

So if we can avoid \n for that reason, is there anything else that could be considered?

Yes, \t, \x1234, \r, \ and probably a few more I have forgot about.

I am starting to think that, unless the font can render differently in strings and outside strings, the easiest way to implement this is to simply accept that escape codes in strings get rendered as lambdas, despite it being kind of wrong.

anka-213 avatar Dec 31 '20 21:12 anka-213

Yeah, I would fully agree on that

ShalokShalom avatar Jan 01 '21 10:01 ShalokShalom

I did a little bit of research, and according to Tonksy, who is the developer of the contextual alternative code, which is used in Hasklig, is this possible to differentiate between comments and functions via the syntax highlighting.

https://github.com/tonsky/FiraCode/issues/1207#issuecomment-790748580

So you can turn ligatures off in comments, which is sensical anyway, in my opinion.

ShalokShalom avatar Mar 04 '21 16:03 ShalokShalom

(and in strings)

Sweet!

anka-213 avatar Mar 05 '21 10:03 anka-213

So can we do this now? :smile:

ShalokShalom avatar Mar 05 '21 17:03 ShalokShalom