Page 1 of 1

Num / IsNum / Int / Float / Str

Posted: 07 May 2019, 04:43
by jeeswg

- IsNum(Num) would be a shorthand for (Num is "number"). I.e. 'looks numeric'.
- (An extra Options parameter could have various letter options: e.g. looks like a Float (F), looks like an Integer (I), possibly has a specific type.)
- (Generally, I find some 'is' operator types a bit ambiguous, possibly, I might prefer a StrIsType(string, type) function, and a more specific StrIsNum(string) function.)

- Num(Num, Default:=null) would be similar to Float/Integer, but more general.
- Something that looked like a Float would be returned as a Float.
- Something that looked like an Integer, would be returned as an Integer.
- Something that looked non-numeric, would raise an error, or, if the Default value was specified, the Default value would be passed (which could be a string).
- Perhaps something already of Float/Integer type would maintain its type.

- A Num function would convert a string to a Float/Integer as appropriate. Otherwise some possibly verbose check must be done to decide whether to use the Float function or the Integer function.
- The Default parameter could be omitted, you'd do instead:
var := IsNum(var) ? Num(var) : default
var := (var is "number") ? Num(var) : default

- I'd be happy to have these remain as custom functions in my general library, but I thought they'd be potential candidates for being built-in.

- Some code for IsNum and Num that attempts to be AHK v1/v2 compatible.

Code: Select all



	try vTemp := vNum + 0
		return 0
	return !(vTemp = "")


;MsgBox(Num("a", 0))
;vNum := "a"
;MsgBox(vNum + 0)

;if vNum looks numeric, return it
;if vNum doesn't look numeric, error, unless a default value is specified
Num(vNum, oDefault*) ;vNum, vDefault:=null
		vTemp := vNum + 0
		if !(vTemp = "")
			return vNum
	if oDefault.HasKey(1)
		return oDefault.1
	throw Exception("Type mismatch.", -1)



- (AHK v2 currently has Integer/Float/String.)
- Perhaps Integer and String should be shortened to Int and Str.
- The shorter names increase readability in long concatenated expressions.
- Int is quite common, Integer seems unnecessarily long.
- AHK already has StrXXX functions.
- Str is used by Python and possibly other languages.
- It's possible that the longer names were chosen to match what Type returns, but I think that this consideration is outweighed by Int being common and Int / Str being more readable/practical.

Re: Num / IsNum / Int / Float / Str

Posted: 11 May 2019, 23:21
by lexikos
Int is quite common
DllCall has int, short and char; these names come from C and are shared by many languages based on or influenced by C. AutoHotkey's integer type is not int, but Int64 (or __int64 in MSVC++). It could be changed to Int64, but it's unlikely that support for any other integer types will be added.

AutoHotkey's floating-point type is double, but I think "double" is a bit more cryptic than "float".

Re: Num / IsNum / Int / Float / Str

Posted: 12 May 2019, 02:13
by jeeswg
- Ah yes, there is casting in various languages, e.g. (int)mystring in C++.
- And AHK's Integer and Float types, could be called Int64 and Double (but those names are awkward).
- I had in mind Int() from Excel and Python, and parseInt() from JavaScript.
- And also, the idea that: if something's too long, people are more reluctant to use it.
- Alternatively, Trunc() is available in Excel/Python/JavaScript. Excel has an additional parameter, to specify the order of magnitude.