I am obviously missing something here regarding optional defaulted parameters
using templates:
# Defaulting is supported in the following (even when there is an untyped
parameter)
var lastGreeting: string
template greet*(lastGreeting: untyped, text = "Hello", who = "Bob") =
lastGreeting = text & " " & who
echo lastGreeting
greet lastGreeting
greet(lastGreeting, "Hi")
greet(lastGreeting, "Salute", "Fred")
greet(lastGreeting, who = "Tom")
echo "\nLastGreeting: ", lastGreeting
# Generates the expected output:
# Hello Bob
# Hi Bob
# Salute Fred
# Hello Tom
#
# LastGreeting: Hello Tom
echo "---"
# Now for my *failing* example template with optional parameters
let Chrome = "Chrome"
template webSession*(
sessionInfo: untyped,
baseURL: string,
browser = Chrome,
headless = true,
actions: untyped
) =
# Mock open session
var sessionInfo =
"open Session(" &
"baseURL=" & "\"" & baseURL & "\", " &
"browser=" & $browser & ", " &
"headless=" & $headless & ")"
actions
# Mock session cleanup
echo "close Session"
# The following works fine and outputs:
# open Session(baseURL="https://mysite.com", browser=Chrome,
headless=false)
# close Session
webSession(mySite, "https://mysite.com", Chrome, false):
echo mySite
# But actually trying to use default parameters:
# webSession(mySite, "https://mysite.com"):
# echo mySite
# Fails to compile with the following error:
#
# Expression: webSession(mySite, "https://mysite.com") do:
# echo [mySite]
# [1] mySite: string
# [2] "https://mysite.com": string
# [3]
# echo [mySite]: void
#
# Expected one of (first mismatch at [position]):
# [3] template webSession(sessionInfo: untyped; baseURL: string; browser =
Chrome;
# headless = true; actions: untyped): untyped
Run
I can't see what is different from the top example and the failing one. What
silly thing am I missing :-)