Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7663253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:00:42+00:00 2026-05-31T14:00:42+00:00

How do you provide default arguments to a Tcl procedure that are evaluated at

  • 0

How do you provide default arguments to a Tcl procedure that are evaluated at call-time?

Here’s an example of what I’ve tried:

> tclsh
% proc test { {def [expr 4 + 3]} } { puts $def }
too many fields in argument specifier "def [expr 4 + 3]"
% proc test { {def {[expr 4 + 3]}} } {puts $def}
% test
[expr 4 + 3]
% test 5
5
% proc test { {def {[expr 4 + 3]}} } {puts [$def]}
% test
invalid command name "[expr 4 + 3]"
% proc test { {def {[expr 4 + 3]}} } {puts [eval $def]}
% test
invalid command name "7"

The example is just to simplify code. Of course in this simple example one would just use {def 7} to set the proc’s default value.

However, the goal is to be able to call some more complex function that delivers a good default value whenever the procedure test is being called. So the defaults can vary.

My current solution is to default to the empty string and check that:

% proc test { {def {}} }  {  if {$def == {}} { set def [expr 4 + 3] } ; puts $def }
% test
7
% test 5
5

However I consider this not elegant enough: There ought to be a way to put declarations where they belong: In the header.

Also, possibly, the empty string might be a perfectly fine value given by a caller that is not to be replaced with the default call.

Another workaround could be to just use args as a parameter and then inspect that one. But that provides even less explicit declarative style.

Any ideas how I can incorporate the eval into the declarative proc header?

(I’m on Tcl8.4 with no way to upgrade because of use in a commercial tool environment. But for the sake of this site I’d also encourage answers for more modern tcl versions)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:00:43+00:00Added an answer on May 31, 2026 at 2:00 pm

    What about this one (which is just slightly different from your tries):

    % proc test {{def {[expr 4+3]}}} {eval puts "$def"}
    % test
    7
    % test 5
    5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why do I have to provide default ctor if I want to create an
When creating a designable .NET component, you are required to provide a default constructor.
Should I provide these things by default or only on client request (if they
I'm building Ruby on Rails 2.3.5 app. By default, Ruby on Rails doesn't provide
Can anybody confirm or deny that I can modify default styles provided by StyleCop
are there any additional WPF themes from microsoft except that default ones provided in
I have to confess that I hate membership provider. The default implementation is not
I recently noticed a class in C++0x that calls for an explicit default constructor.
C++ doesn't allow a class containing an array of items that are not default
I have an interface IFoo for which I want to provide a default implementation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.