Hi all,
I have a syntax question which might be out of left field, or violate some syntax rule or compiler constraint I am not aware of, but here goes:
type someFunc = proc(x: int, f : float, s : string) : string
var sf1 : someFunc = proc(x: int, f : float, s : string) : string =
return s & "," & $x & "," & $f
var sf2 : someFunc =
return s & "," & $x & "," & $f
var sf3 = someFunc =
return s & "," & $x & "," & $f
The type def and the var sf1 compile, but sf1 seems needlessly redundant. It would be nice to be able to express the same proc using the syntax in sf2 or sf3, but those are not valid Nim at this point. The one advantage of using the sf1 syntax I can think of is if you want to switch around the argument names from the type def. Would it be possible in some future release for both sf1 and sf2 (or sf3) to be correct, or is there some reason to only allow the sf1 syntax?
You might like the lambda syntax macro introduced by the future module:
import future
type someFunc = proc(x: int, f : float, s : string) : string
var sf1: someFunc = (x: int, f: float, s: string) => s & ',' & $x & $f
var sf2 = (x: int, f: float, s: string) => s & ',' & $x & $f
Thanks V. I am aware of the future module, and I like to use it, particularly when declaring short procs within proc call argument lists. However, it does not solve the problem of having to repeat the entire argument list, which can get tiresome with long argument lists and/or long descriptive argument names.
Is there a way to use a macro to avoid repeating the arg list? For example how about
var sf4 : somefunc = ibid =
return s & ',' & $x & ',' & $f
where 'ibid' is a macro that inserts the definition of the type to the left of the equals sign at it's location in the AST?
Ibid comes from the term used in footnoting and is derived from the latin word meaning 'in place'. There have been a few times that I have wanted such an operator. For instance, in Java you can have long typedefs for variables which need to be repeated verbatim on the other side of the equals sign after a "new". And Go also has a syntax that requires the programmer to repeat function argument lists outside of the type def.
Is this the idea?
type someFunc = proc(x: int, f : float, s : string) : string
procHelper("sf1", someFunc):
return s & "," & $x & "," & $f
echo sf1(1, 2.0, "three")
Interesting idea. Worth it? I'm not sure. Object and tuple initializers could be used instead. In their generated code, they are passed by pointer rather than by value, which is only a small penalty.
With an object:
type SomeFuncArgs1 = object
x: int
f: float
s: string
proc sf1(args: SomeFuncArgs1): string =
return args.s & ',' & $args.x & ',' & $args.f
echo sf1(SomeFuncArgs1(x:1, f:2.2, s:"three"))
With a tuple:
type SomeFuncArgs2 = tuple[x:int, f:float, s:string]
proc sf2(args: SomeFuncArgs2): string =
return args.s & ',' & $args.x & ',' & $args.f
echo sf2((x:1, f:2.2, s:"threeX"))
I think I get what you are doing, cdun2001, but I think that is making the syntax a bit too complicated.
Is there any way to get access to AST of the entire compilation unit, even if it is read only? For example to retrieve the definition of an identifier? Otherwise I think the type def needs sent to any macro or template, which limits the syntax.