Skip to content

No Current Way to Specify Return Type Without a Default Value #7

@DanRathbun

Description

@DanRathbun

No Current Way to Specify Return Type Without a Default Value.

As discussed in the last PR comments, when a coder passes nil or otherwise does not specify a default value (the Textbox class constructor uses nil as a default,) and the result returned is a String object by default.
This is the same behavior had the coder purposefully passed "" (an empty String) as the argument for the input's default. But in this latter case (the purposeful empty String) is an explicit directive that the coder wants a String object returned by type_convert().

But a coder may not want a String result but also may wish for the input to be blank, so they cannot pass a default value.

In those previous comments, I had entertained the idea that specific subclasses of Textbox such as TextboxLength, TextboxFloat, TextboxInteger, TextboxTime, etc. But this would be clunky, and the easiest solution came to me whilst sitting on the "thinking throne". ;)

1 - There needs to be a documentation note that informs coders that no default or nil for a default will produce a String (just as if they had passed "".)

2 - The answer for result typing without a default value is simple. (It's a "Doh!" forehead smacker.)

We make changes that allow coders to pass class identifiers in place of a default value. Ie ...

  def self.prompt_without_options
    title = 'Tell me about yourself'
    prompts = ['What is your name?', 'What is your age?', 'Pet?']
    defaults = ["", Integer, 'None']
    results = HtmlUI.inputbox(prompts, defaults, title)
    p results
  end

Advanced inputs ...

  def self.prompt_advanced
    options = {
      title: 'HtmlDialog Options',
      accept_button: 'Ok',
      cancel_button: 'Cancel',
      inputs: [
        HtmlUI::Textbox.new('Name'), # will be a blank field, String assumed
        HtmlUI::Textbox.new('Age', Integer),
        HtmlUI::Dropdown.new('Pet', 'Cat', [
          'None', 'Cat', 'Dog', 'Parrot (Resting)', 'Other'
        ]),
        HtmlUI::Listbox.new('Profession', 'Architect', [
          'None', 'Architect', 'Urban Planner', 'Model Railroad Designer', 'Other'
        ]),
      ]
    }
    dialog = HtmlUI::InputBox.new(options)
    results = dialog.prompt
    p results
  end

If the end user leaves these "typed" fields blank, then the value will be 0 (for Integer), 0.0 (for Float) and 0.000" (for Length).

The coder is then responsible for dealing with the zero values.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions