Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alt-Code Characters in F#

Tags:

vb.net

f#

Edit/Update: Thank you all for responding. I understand I was being too vague, but wasn't sure if posting naked lines of code would be useful in this case.

In my .vb file I have a pulldown control with its validation values as:

TempUnit.DataSource = {"°C", "°F", "°R", "K"}

...which is stored in a variable:

Dim unit As String = TempUnit.SelectedItem.ToString

...which gets passed into a function along with other variables:

Function xxx(..., ByVal unitT As String) As Double

... which finally calls the .fs file and gets evaluated using:

let tempConv t u =
match u with
|"°C" -> t * 9.0 / 5.0 + 32.0
|"°R" -> t - 459.67
|"K" -> t * 9.0 / 5.0 - 459.67
|_ -> t

If any temperature unit other than Kelvin is selected, the match fails and defaults to the else case (which is Fahrenheit in this context). I ended up bypassing the degree symbol entirely by evaluating the substring instead:

Dim unit As String = TempUnit.SelectedItem.ToString.Substring(1)

The program is working again, but I have no idea what I changed, if anything, to make the string match stop working. The first thing I tried was to copy/paste from one file to other to ensure they were identical strings, in addition to trying other symbols, but to no avail. The degree symbol is what caught my attention, but then I checked the pressure units and found the exact same issue with the micro prefix.

Thank you, Hans Passant, I had unicode in mind as a possible solution, but it didn't seem like an easy fix in the heat of the moment. I appreciate your link.

Original Post: I have a VB program referencing a function stored in an F# library file whose arguments include unit of measure strings containing special characters (e.g. "°C" "µBar").

The strings are identical in the .vb and .fs files; and there was no issue until the F# library file stopped recognizing the Alt-Code characters for reasons unbeknownst to me.

The program works as intended if I remove the offending Alt-Code character from the string definitions in the F# and VB files.

What would cause a match to fail between two identical strings that happen to contain an Alt-Code character?

What is the proper way to handle Alt-Code characters in F# (and VB for that matter)?

like image 258
J. Hopkins Avatar asked Dec 24 '22 00:12

J. Hopkins


1 Answers

The µ glyph is a bit infamous. Unicode has two codepoints that look like that: U+03BC = "Greek small letter Mu" and U+00B5 = "Micro sign". One is a letter in the Greek alphabet, the other is a symbol that often appears in math and units.

Compare μ and µ. Looks almost identical in most fonts (you can see the difference with Segoe UI) and very easily fools the human eye. Typographers insist they are not the same, particularly if they are Greek I'd imagine. Nor does a computer, the problem you are surely dealing with.

Copy/paste or re-type to fix. The Charmap.exe applet in Windows is very handy to get this right.

like image 194
Hans Passant Avatar answered Jan 07 '23 11:01

Hans Passant