Num / IsNum / Int / Float / Str

Propose new features and changes
User avatar
jeeswg
Posts: 6373
Joined: 19 Dec 2016, 01:58
Location: UK

Num / IsNum / Int / Float / Str

07 May 2019, 04:43

ISNUM AND NUM

- 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

;==================================================

;e.g.:
;MsgBox(IsNum(1))
;MsgBox(IsNum("a"))

IsNum(vNum)
{
	local
	try vTemp := vNum + 0
	catch
		return 0
	return !(vTemp = "")
}

;==================================================

;e.g.:
;MsgBox(Num(1))
;MsgBox(Num("a", 0))
;MsgBox(Num("a"))
;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
{
	local
	try
	{
		vTemp := vNum + 0
		if !(vTemp = "")
			return vNum
	}
	if oDefault.HasKey(1)
		return oDefault.1
	throw Exception("Type mismatch.", -1)
}

;==================================================

INT AND STR

- (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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 6367
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Num / IsNum / Int / Float / Str

11 May 2019, 23:21

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".
User avatar
jeeswg
Posts: 6373
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Num / IsNum / Int / Float / Str

12 May 2019, 02:13

- 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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 5 guests