5

Is it possible to define a default value for a record type or generally any user defined type?

Something like (pseudo VHDL):

type t_foo is record
    a : integer := 4;
    b : std_logic := '0';
end record;

or

subtype glarp is integer range 0 to 10 := 5;

EDIT: changed glarp from type to subtype definition.

5
  • 1
    No. type declarations are not object declarations. Note type glarp can't be declared from another type. You could declare subtypes for you element types (or glarp) where the 'LEFT value of the subtype provides the default initial value. Commented Mar 30, 2019 at 21:21
  • IEEE Std 1076-2008 5. Types, 5.1 General para 2 (in part) "A type is characterized by a set of values and a set of operations." 6.4 Objects, 6.4.1 General para 1 (in part) "An object is a named entity that contains (has) a value of a type. An object is one of the following: — An object declared by an object declaration (see 6.4.2) ..." Commented Mar 30, 2019 at 21:43
  • You could define an object (say of class constant) with a default value prior to any object declarations with a desired default value, wherein the object with explicit default initial values can be used to provide an initial value for further objects of the same type. This can be done for subelements of composite object types as well, with objects of the subelement type. Note synthesis tools don't universally support using non-static objects (variables, signals) providing initial values while the standard does. Commented Mar 30, 2019 at 21:53
  • See What is the XY problem?. Instead of shotgunning around your actual problem you could provide a specific programming question with a minimal reproducible example illustrating the actual problem you're trying to solve. The number of references from the LRM that could be needed to answer open ended questions can exceed the posting size limit here. Commented Mar 30, 2019 at 22:03
  • Thanks for showing that the VHDL LRM has no provisions for setting default values of user defined (record) types. It is seems a little crude to have the 'LEFT value as the implicit default value, especially if this means that a scalar type will require the default value to be identical to the lower or upper bound of the range of the scalar type. But that's what we got. Commented Mar 31, 2019 at 9:47

1 Answer 1

2

I take the liberty of turning a comment into an answer. The initial value for a record type could be defined by a constant of that type.

type t_foo is record
    a : integer;
    b : std_logic;
end record;

constant INIT_T_FOO : t_foo := (a => 4, b => '0');

signal bar : t_foo := INIT_T_FOO;

A downside of this approach is that the user has to ensure the correct initial value is set every time an object of type t_foo is defined. Using a constant to define an initial value can save some typing and makes it easier to change the initial value later. But again, it is not possible to enforce a specific initial value this way, it all comes down to coding discipline and human error, thus it's a suboptimal solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.