4

Optional IDF and Workspace Object

I put this code as measure writing guide :

new_sch = "
    Schedule:Compact,
    Return rate 95, !- Name
    Fractional,        !- Schedule Type Limits Name
    Through: 12/31,          !- Field 1
    For: AllDays,            !- Field 2
    Until: 24:00,            !- Field 3
    0.95;                    !- Field 4
    "
    new_sch_idfobj = OpenStudio::IdfObject::load(new_sch)
    puts "compact schedule #{new_sch_idfobj}"
    new_sch_idfobj1 = new_sch_idfobj.get
    sch_returnrate = workspace.addObject(new_sch_idfobj1)
    puts "compact schedule1 #{sch_returnrate}"

it returns in screen :

compact schedule #<OpenStudio::OptionalIdfObject:0x000000062ceda8>
compact schedule1 #<OpenStudio::OptionalWorkspaceObject:0x000000062ceb50>

What difference between Optional and Non-Optional Object? Have any difference with standard objects when we need to use them?

ngkhanh's avatar
2.2k
ngkhanh
asked 2016-02-08 11:05:58 -0500
Julien Marrec's avatar
29.7k
Julien Marrec
updated 2017-02-02 01:54:19 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

3

An Optional is basically something that gets returned when you can't know for sure if something will work on not. It allows you to write code that won't crash, you just have to make sure you test the objects before you do get them

new_sch_idfobj = OpenStudio::IdfObject::load(new_sch)
if new_sch_idfobj.empty?
    # Decide what to do if it didn't work
   puts "Couldn't load the schedule. Double check your input string"
else
    # If it did work, get it
    new_sch_idfobj = new_sch_idfobj.get
    # Do something with it...
end

You could read up more on the boost documentation if you are interested in knowing exactly why it's a good thing and what advantages it offers.

Julien Marrec's avatar
29.7k
Julien Marrec
answered 2016-02-08 14:45:51 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments